Python 代码陷入无限循环

Python 代码陷入无限循环,python,Python,这是我的代码片段。它会问你一个等级,当你得到答案时,你会继续写。例如,如果你输入的是light而不是light,那么代码会回到语句的顶部 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 ra

这是我的代码片段。它会问你一个等级,当你得到答案时,你会继续写。例如,如果你输入的是light而不是light,那么代码会回到语句的顶部

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')
我是用A,B和C做的,所以当你带上一个手提包的时候

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')
还有C

我的代码是错误的,在你输入答案C后,它会问你第二个问题。 例如:选择B或C。当选择C时,问另一个问题。选择B或C

这是错误的代码

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')
我希望你能理解。
谢谢大家!

您的代码似乎会继续循环,因为您的
get\u question2
函数没有返回,因此它会运行,而q1\u c是
None

尝试在其上添加退货:

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
另外,我推荐这个关于如何查询用户输入的惊人答案。它确实有助于避免一些常见错误并验证您的答案


我想这个问题可以用更少的台词来回答。这将使像我这样想回答的人更容易。你能提供你得到的错误吗?没有错误消息。它不断循环