0

I'm a total newbie learning Python and I'm making a small text-based Fallout clone. However, I'm having issues with one set of code:

def PCgender():
    gender = input("Is your character male or female? \n")
    gender = gender.lower()
    gender = str(gender)
    if gender=="male":
        print("Atta, cowboy! ")
    elif gender=="female":
        print("You go, girl! ")
    else:
        #Triple quotes here because there's both single and double quotes in this sentence and to make it go over multiple lines. 
        print("""Aw, you special snowflake, you! But I'm fraid I haven't coded for anything 
        except "male" and "female", so please try one of those! """)
        PCgender()
PCgender()

Even when the input is male or female, it still returns the else function. I have a very similar block further up which works perfectly, but this doesn't and I have no idea why.

Update: I tried commenting out all of this code, but VS still ran it, so I restarted the program. Same problem. I made a new file, copied it over. Still ran the code as if it wasn't commented out. As per some nice people's suggestions, I tried running print(repr(gender) and it returned 'male'. Strange.

I then decided to remove the .lower() function and it worked! When I re-added it, it kept working, so IDK what I did wrong. The original code is up there, so please tell me what I did wrong, if you know!

Cheers!

804R
  • 23
  • 3
  • 5
    `gender = gender.lower` is missing parentheses, and therefore assigns *the method itself* to the name, rather than the return value. Had you tried e.g. `print(repr(gender))` as part of your debugging, you would have noticed this. – jonrsharpe Oct 06 '15 at 12:46
  • Using recursion to repeat a request for user input can cause your program to eventually crash. See [Asking the user for input until they give a valid response](http://stackoverflow.com/q/23294658/953482) for more information. – Kevin Oct 06 '15 at 12:48

1 Answers1

0

Parentheses missing:

gender = gender.lower()
hsfzxjy
  • 1,242
  • 4
  • 14
  • 22
  • Thanks, but that wasn't it; same result, still. I was actually typing "male" and "female" with lower case, so it shouldn't have been an issue regardless. – 804R Oct 06 '15 at 12:53
  • print out the gender variable and see what you have got – hsfzxjy Oct 06 '15 at 12:55
  • 1
    @804R No you weren't. You were comparing the string representation of the function `` to a string `'male'` – Cory Kramer Oct 06 '15 at 12:55
  • @CoryKramer He has corrected it. – hsfzxjy Oct 06 '15 at 12:56
  • @hsfzxjy, Tried that, too, before posting here. The string appears, as far as I can tell, to be exactly the same. – 804R Oct 06 '15 at 12:57
  • @804R Delete the first 3 lines and use `gender = input("Is your character male or female? \n").lower()` – Cory Kramer Oct 06 '15 at 12:58
  • any spaces or other invisible characters? – hsfzxjy Oct 06 '15 at 12:59
  • @CoryKramer Thanks for the tip, but still nothing. The output is the same (so thanks for helping me make it shorter!), but it's still not recognized. – 804R Oct 06 '15 at 13:01
  • @hsfzxjy Nope, I've tried rewriting everything several times. – 804R Oct 06 '15 at 13:02
  • Amazing, I will try it out. – hsfzxjy Oct 06 '15 at 13:14
  • @hsfzxjy Thank you! I tried redoing again, for good measure, so now every character is verified, but still nothing. :/ – 804R Oct 06 '15 at 13:19
  • @804R I've copied your code directly into my editor, run it with python3, and I got "Atta cowboy!" when I typed male. – hsfzxjy Oct 06 '15 at 13:24
  • Show me more details, like python version, operating system as well as character set. – hsfzxjy Oct 06 '15 at 13:26
  • @hsfzxjy Yeah, I just tried commenting out everything and it still ran the code! Restarted visual studio, still got the same result! WTH? Python shouldn't even be able to see the code it now spouts at me! – 804R Oct 06 '15 at 13:29
  • @hsfzxjy I believe I have the very latest Python version (3.5.#?); where can I check that? I'm running Visual Studio on a W8.1 machine. Where do I find the character set? – 804R Oct 06 '15 at 13:32
  • If you are using English version of windows , the character set will ve ascii – hsfzxjy Oct 06 '15 at 13:38
  • so are you using cmd to debug or others like console in visual studio(if there is)? – hsfzxjy Oct 06 '15 at 13:40
  • what about print(repr(gender))? – hsfzxjy Oct 06 '15 at 13:42
  • @hsfzxjy VS has a built-in debugger, but there's no error code. It goes to the "else" option and back again. – 804R Oct 06 '15 at 13:43
  • print(repr(gender)), whats the result? – hsfzxjy Oct 06 '15 at 13:45
  • @hsfzxjy It says 'male' in single quotes. But I removed the .lower function and now it's working for some reason. Thank you! :D – 804R Oct 06 '15 at 13:47
  • amazing... update your previous version to the question, I wanna find out why.. – hsfzxjy Oct 06 '15 at 13:49
  • and also, accept the answer if your problem has been settled :) – hsfzxjy Oct 06 '15 at 14:03
  • @hsfzxjy Yes, done, thanks a lot for your help! – 804R Oct 06 '15 at 14:04
  • It seems that vs hasnt update the pyc binary file.. – hsfzxjy Oct 06 '15 at 14:13
  • For python I prefer sublime text and bash to be the developing environment. I don't know how vs work with python but it will be terribly awful if it won't recompile the file per running. – hsfzxjy Oct 06 '15 at 14:17