Python while循环在每个cas中中断

2024-10-03 02:41:23 发布

您现在位置:Python中文网/ 问答频道 /正文

我试着做一个程序,掷骰子并检查用户是否想继续掷骰子,如果不想,程序应该停止。不过,不管你输入什么,程序都会跳出循环。有人能解释一下为什么,给我一些提示,使程序更简单,工作?谢谢

import random
sideNumber = int(input("Enter the number of sides in the die: "))
print("Dice numbers: ")

while True:
 print(random.randint(0, sideNumber))
 print("Do you want to continue?")
 response = input()
 if response == "n" or "no":
  break

Tags: oftheinimport程序numberinputresponse
2条回答
if response == "n" or "no":

使您的代码失败。这将检查"no"的布尔值是否为真,并且始终为真。替换为:

if response == "n" or response == "no":

那是因为"no"这个语句仍然是真的。你知道吗

你应该做:

if response == "n" or response == "no":

或更好:

if response in ["n", "no"] : 

相关问题 更多 >