0

I'm doing a code that will ask you a rank. you have multiple answers. what i'm trying to do is when the user puts in an answer and the answer was correct then the code will carry on, and when the answer is wrong then the code will go back to the start of the if statement.

what my code does at the moment is that it will go to the top of the statement no matter what. I want it so that the code will skip when the answer is right.

here is my code!

print ('what rank do you want')
print ('light')
print ('heavy')
print ('soldier')
print ('ninja')
print('if you want to pick a rank again than type "retake"')#ignore this line#


print ('light')
print ('heavy')
print ('soldier')
print ('ninja')

invalid_input = True
def start() :
    invalid_input = True
    rank = input('pleese pick a rank!\n')

    if rank == ('light'):               
        print ("you have chosen light")
        invalid_input = False           


    elif rank == 'heavy':
        print ('you have chosen heavy')
        invalid_input = False


    elif rank == ('soldier'):
        print ('you have chosen soldier')
        invalid_input = False


    elif rank == ('ninja'):
        print ('you have chosen ninja')
        invalid_input = False


    else:
        print ('Sorry, that was an invalid command!')


while invalid_input :
    start()

print ('well done you have picked your rank') #the bit where the code will carry on#
  • You don't have other `if` statements. You have `elif` blocks, part of the first `if` statement. – Martijn Pieters May 27 '14 at 12:17
  • 2
    I think what you mean is that you want `invalid_input` to be a global; `global invalid_input` in the function would solve that. – Martijn Pieters May 27 '14 at 12:18
  • [Asking the user for input until he gives a valid response](http://stackoverflow.com/questions/23294658/asking-the-user-for-input-until-he-gives-a-valid-response) may be useful to you. – Kevin May 27 '14 at 12:25

2 Answers2

0

Inside of the start() function, invalid_input is a local variable, separate from the global invalid_input you defined.

If you want invalid_input to be treated as a global inside of start(), you need to tell the interpreter so:

def start():
    global invalid_input
    invalid_input = True

However, you'd best avoid globals altogether, and have your function return True or False instead:

def start() :
    rank = input('please pick a rank!\n')

    if rank in ('light', 'heavy', 'soldier', 'ninja'):
        print('You have chosen {}'.format(rank))
        return False

    else:
        print('Sorry, that was an invalid command!')
        return True

invalid_input = True
while invalid_input:
    invalid_input = start()

or just use an endless loop and break out if there was valid input:

while True:
    if not start():
        break

I'd personally would return True for valid input instead though.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
0

You can remove a lot of code duplication:

ranks = ['light', 'heavy', 'soldier', 'ninja']

print('what rank do you want')
for rank in ranks:
    print(rank)

def getRank():
    rank = input('please pick a rank!\n')
    if rank in ranks:           
        print("you have chosen", rank)
        return rank

rank = getRank()
while rank is None:
    print('Sorry, that was an invalid command!')
    rank = getRank()

print('well done you have picked your rank')

The idea here is to make a function which returns the special value None if the input is no good. It does this by "falling off the end" with no explicit return statement needed.

John Zwinck
  • 239,568
  • 38
  • 324
  • 436