好的,原来的问题得到了解答(多行代码循环和代码无法识别,或者,如果您需要帮助,请在下面查找这些答案),但这是一个新问题:我的游戏无法识别正确的数字 这是 v3 代码:
while True:
answer = randint(1,10)
input1 = input('choose a number between 1 and 10.')
if answer == input1:
print('you got it, good job.')
input2 = input('do you want to play again?')
if input2 != 'yes' and input2 != 'ys' and input2 != 'y' and input2 != 'ye' and input2 != 'yeah' and input2 != 'yup' and input2 != 'sure' and input2 != 'alright':
print("ok, stopping")
break
else:
print ("wrong answer, the answer was")
print(answer)
input2 = input('do you want to play again?')
if input2 != 'yes' and input2 != 'ys' and input2 != 'y' and input2 != 'ye' and input2 != 'yeah' and input2 != 'yup' and input2 != 'sure' and input2 != 'alright':
print("ok, stopping")
break
这次是游戏部分的问题:L, 仍然使用 python 3.12.3 IDLE。 这是问题的图片: 我得到了正确的答案,代码仍然认为是错误的?!?!?
我重复使用该帖子的原因是我直到明天才能提出另一个问题
要继续执行某些操作直到用户想要停止,请将整个代码包装在
while True:
循环中,并在用户准备退出时跳出循环。
while True:
# game code goes here
answer = input("Do you want to play again?")
if answer != "yes":
break
您的 if 语句的条件逻辑有缺陷。因为您使用 or 检查响应是否不等于,所以它始终为真。 (F 或 T 或 T 或 T 始终为真)。
正确的条件逻辑是检查输入是否不等于all响应 - 因此使用and而不是or。
while True:
input1 = input('choose a number between 1 and 10.')
if input1 == answer:
print('you got it, good job.')
input2 = input('do you want to play again?')
if input2 != 'yes' and input2 != 'ys' and input2 != 'y' and input2 != 'ye' and input2 != 'yeah' and input2 != 'yup' and input2 != 'sure' and input2 != 'alright':
print("ok, stopping")
break
elif input1 != answer:
print ("wrong answer, the answer was")
print(answer)
input2 = input('do you want to play again?')
if input2 != 'yes' and input2 != 'ys' and input2 != 'y' and input2 != 'ye' and input2 != 'yeah' and input2 != 'yup' and input2 != 'sure' and input2 != 'alright':
print("ok, stopping")
break
除了您编写的内容之外,您还可以将代码编辑为:
input_continue = ['yes', 'ys', 'y', 'ye', 'yeah', 'yup', 'sure', 'alright']
while True:
input1 = input('choose a number between 1 and 10.')
if input1 == answer:
print('you got it, good job.')
input2 = input('do you want to play again?')
if input2 not in input_continue:
print("ok, stopping")
break
elif input1 != answer:
print ("wrong answer, the answer was")
print(answer)
input2 = input('do you want to play again?')
if input2 not in input_continue:
print("ok, stopping")
break
这将 1) 产生所需的条件检查,2) 更易于阅读。