问题在于:我的while循环没有重复,它只是在Python启动器中返回一个空白行并且不会继续执行代码,我添加了一个打印功能at at最后检查它是否刚刚跳过。
我使用了几种方法来重复代码,例如def merge[A](l: List[A])(implicit m: Monoid[A]) = l.foldLeft(m.zero)(m.append)
merge(List(Bar(2), Bar(4), Bar(2)))
res: Bar = Bar(10)
和While True:
,所以我开始认为代码中的其他地方存在问题。有问题的代码是:
While 0 == 0
This当我尝试重复时会发生什么 我非常感谢帮助,我只想说我是编码的新手,所以我知道它可能不会被优化,但是如果评论说我如何优化它会有所帮助。
在开始时,变量定义如下:
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
答案 0 :(得分:1)
你需要在&#34;和#34;之后将i设置为1,并将input_answer设置为&#34; unknown&#34;在第二个内循环之前。更正后的代码:
while True:
i = 1
while i <= dice_amount:
dice_output = random.randint(0,dice_range)
print("dice number ", i, " is ", dice_output)
i +=1
input_answer = "unknown"
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
答案 1 :(得分:0)
我终于意识到了这个问题,在阅读了其他评论和答案之后,我想出了这个问题。首先,我意识到如果没有任何答案是Y或N,我不需要使用input_answer,我只是使它成为重复循环,如果既不是Y也不是N它只是重复了问题,Y打破了循环, N结束程序。如果答案是Y,我加上i = 1,以便重复。
这是固定代码:
import random
import time
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?"))
i = 1
while True:
while i <= dice_amount:
dice_output = random.randint(0,dice_range)
print("dice number ", i, " is ", dice_output)
i +=1
while True:
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":
i = 1
break