-2

this is a snippet of my code. it will ask you for a rank and when you get the answer write is will carry on E.G - if you typed ligght instead of light then the code will go back to the top of the statement.

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')

I have done this with an A, B and C, so when you put in A carry on going.

print('''A:go up to him and try to fight!
B:run to the phone and call 911!
C:talk to him to make him stop!''')


q1s = ('a','b',)

for q1 in q1s:
    print('')

def get_question():
    q1 = input('pick A B or C!\n')
    if q1 in q1s:           
        if q1 == 'a':
            print("""he's too quick. he twist your arm around when
you try to take a punch. you struggle,
but you cant move. He's too strong.
then you feel your sholdier hurt with
a sharp pain. you start to feel yourself
drop. then blackness.""")


        elif q1 == 'b':
           print("""you run to the phone in the room
on the other side of the house and close
the door behind you.then you reach for
the phone and call 911. 'beeep...beeep...beeep'
goes the phone' beeep...hello how can I help
you'says a woman on the other side of the line""")
        return q1 
q1 = get_question()
while q1 is None:
    print('Sorry, that was an invalid command!')
    q1 = get_question()


print ('carry on')

also with C.

my code is wrong in the bit where it asks you the second question after you put in the answer C. for example:pick A B or C.when picked C ask another question.pick A B or C.

here is the code that is wrong.

elif q1 == 'c':
         print('what do you say')
         q1s_c = ('a', 'b' , 'c')
         for q1 in q1s_c:
             print('')

         def get_question2():
             q1_c = input('pick A B or C!\n')
             if q1_c in q1s_c:           
                 if q1_c == 'a':
                     print('bla bla bla')
         q1_c = get_question2()
         while q1_c is None:
            print ('Sorry, that was an invalid command!')
            q1_c = get_question2()

             return q1_c  #i think its got to do with this line#

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

print ('carry on')

I hope you understand. thank you!

Eugen Constantin Dinca
  • 8,994
  • 2
  • 34
  • 51

1 Answers1

0

It seems like your code will keep looping because your get_question2 function is not returning, so it runs and q1_c is None.

Try adding a return on it:

def get_question2():
    q1_c = input('pick A B or C!\n')
    if q1_c in q1s_c:           
        if q1_c == 'a':
            print('bla bla bla')
        return q1s_c

Also, I recommend this amazing answer on how to query the user for inputs. It does help avoinding a few common mistakes and validating your answer.

Asking the user for input until they give a valid response

Community
  • 1
  • 1
pekapa
  • 881
  • 1
  • 11
  • 25