我已经编写了这些代码,但它不起作用。
import random
count=0
while True:
x=random.randint(1,6)
y=random.randint(1,6)
print(x,y)
if x!=6 and y!=6:
count+=1
else:
print('Attempt number is {}.'.format(count))
break
However, I have nonsense outputs such as : 6 2
Attempt number is 0.
另外,如果可能的话,请您使用函数来编写这段代码吗?
最佳答案
import random
def guess_count():
count=0
x,y=0,0
while True:
if x==6 and y==6:
print('Attempt number is {}.'.format(count))
break
x=random.randint(1,6)
y=random.randint(1,6)
print(x,y)
count+=1
guess_count()
希望这能达到您的预期。
关于python - 一个Python程序,掷两个骰子并计算尝试次数,直到这两个骰子的数量都为6,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58871885/