【发布时间】:2016-10-28 06:04:21
【问题描述】:
这就是问题所在:我的 while 循环根本不重复,它只是在 Python 启动器中返回一个空行并且不继续执行代码,我在最后添加了一个打印函数来检查它是否只是跳过。
我使用了几种方法让代码重复,例如While True: 和While 0 == 0,所以我开始认为代码中的其他地方存在问题。有问题的代码是:
while True:
while i <= dice_amount:
dice_output = random.randint(0,dice_range)
print("dice number ", i, " is ", dice_output)
i +=1
while input_answer == "unknown":
input_answer = input("would you like to throw the dice again? Y/N")
if input_answer == "N":
time.sleep(2)
os.exit
elif input_answer != "Y":
input_answer = "unknown"
print("code is broken") #to see if code just skips loop
This 是我尝试重复时发生的情况 非常感谢您的帮助,我只想说我是编码新手,所以我知道它可能没有被优化,但是说我如何优化它的评论会很有帮助。
一开始变量的定义如下:
dice_amount = int(input("how many times would you like to roll the dice?"))
dice_range = int(input("how many numbers would you like on the dice?"))
input_answer = "unknown"
i = 1
【问题讨论】:
-
i、dice_amount和input_answer定义在哪里?它们在循环之前的值是多少? -
抱歉,编辑了我的问题以表明这一点
标签: python loops while-loop python-3.5