-1

This is the start of an RPG I am going to make, and It runs smoothly until I try to change the gender by saying yes or any other of the answers that activate the if statement. Is there something I am forgetting? P.S. Sorry about this. I am an amateur.

import random
import time
def Intro():
    print('Hit enter to begin.')
    input()
    print('Please choose a name for your character.')
    PlayerName=input()

    def KeepName():
        print('You have chosen "' + PlayerName + '" as your character\'s name.') 
        print('Do you wish to keep this as your name? (yes or no)')
        return input().lower().startswith('n')
    while KeepName():
        print('Please choose a name for your character.')
        PlayerName=input()
    planet = 'Sykahrox VII' #Useless as of this point but I kept it in so I can remember the name of the planet.
    def gender():
        print('Do you want to be Male, or Female?')
        choice = input().lower()
        if choice in ('m', 'male', 'boy', 'guy', 'dude'):
            return 'Male'
        if choice in ('f', 'female', 'girl', 'woman'):
            return 'Female'
    ChangeGen = 'y'
    while ChangeGen in ('y', 'yes', 'yeah', 'yup'):
        genderchoice = gender()
        print ('You have chosen ' + genderchoice + ' as your gender. Do you wish to change this?')
        ChangeGen = input().lower
        if ChangeGen in ('y', 'yes', 'yeah', 'yup'):
            gender()

Intro()
John Campbell
  • 65
  • 1
  • 7
  • @Duniyadnd, sorry about that, I am not sure why I had typed in 14, I have edited the post in response. Sorry for the inconvenience. – John Campbell May 03 '13 at 23:36
  • If you type `male` on the first prompt, `gender()` will successfully return `Male` to its caller… but since you ignore the return value, that will never be visible. The only way you can get anything printed is if you type something invalid the first time, in which case `gender` will recursively call `gender()`, and then print out whatever the inner call returns. (And then, in that case, it won't return anything.) So… what are you actually _trying_ to do here? – abarnert May 03 '13 at 23:42
  • @abarnert I am trying to make it so that it will continue to give you the opportunity to change your mind until you respond with something other than y, yes, yeah, or yup. – John Campbell May 04 '13 at 00:06
  • OK, I think I've got it. But I'm curious: your `KeepName` function and loop work correctly, so why not just do the same thing again for `gender`, instead of trying to come up with differently-structured code that does the exact same thing? – abarnert May 04 '13 at 00:19
  • 1
    As a side note: Titles like "if statement not working correctly" sound a little like you're saying "Python is broken" instead of "my code is broken", which can cause some people to ignore your question or downvote it instead of answering you. (I don't know if that's why someone downvoted you this time, because whoever did so didn't bother to explain themselves.) – abarnert May 04 '13 at 00:26

2 Answers2

2

The then statement of your if clause returns from the function gender(). So you never reach your second print statement in gender.

A.E. Drew
  • 2,097
  • 1
  • 16
  • 24
1

I'm not sure which sequence of inputs you're having problems with, but there are at least two ways it could go wrong.

First, if you give a gender that's in neither 'm', 'male', 'boy', 'guy', 'dude' nor 'f', 'female', 'girl', 'woman', you just fall off the end of the gender function, so it returns None, and then you get a TypeError: Can't convert 'NoneType' object to str implicitly from the print call, because you try to add that None to the string 'You have chosen '.

Second, in this line:

ChangeGen = input().lower

… you aren't calling the lower() function, you're setting ChangeGen to the actual function itself. Since a function never matches any of the strings 'y', 'yes', 'yeah', 'yup', this if will never be true, and the while condition will also never be true.

Then, if you fix that, the if statement is calling gender again, and doing nothing with the result. So, it will ask you whether you want to be male or female, ignore whatever you type, go back to the top of the while loop, and ask again. In fact, you don't need this if statement at all.

Fixing all three problems:

while ChangeGen in ('y', 'yes', 'yeah', 'yup'):
    genderchoice = gender()
    if gender is not None:
        print ('You have chosen ' + genderchoice + ' as your gender. Do you wish to change this?')
        ChangeGen = input().lower()
abarnert
  • 354,177
  • 51
  • 601
  • 671