这是我的代码片段。它会询问你的等级,当你得到答案时,写入将继续执行E.G - 如果你键入了灯而不是光,那么代码将返回到语句的顶部。
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做了这个,所以当你把A继续进行时。
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后问你第二个问题。 例如:选择A B或C.当选择C提出另一个问题时。选择A 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')
我希望你理解。 谢谢!
答案 0 :(得分:0)
似乎您的代码将继续循环,因为您的get_question2
函数未返回,因此它运行且q1_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
另外,我建议如何查询用户的输入这个惊人的答案。它确实有助于避免一些常见错误并验证您的答案。