gpt4 book ai didi

模拟掷 6 面骰子并将每次掷骰的结果相加直到掷出 1 的 Python 程序

转载 作者:行者123 更新时间:2023-11-28 18:07:49 26 4
gpt4 key购买 nike

所以这是我写的代码,试图回答标题中的问题:

import random
print("Well, hello there.")
while True:
a = random.randint(1,6)
sum = 0
if(a==1): #If a one is indeed rolled, it just shows rolled a 1 and 'Pigs out' and makes the score equal to 0(player score that is) and is a sort of a GameOver
print("Pigged out!")
break #To get out of the loop
else:
while(sum<=20):
sum += a
print(sum)

程序应保持分数直到达到 20(或更多)并显示它。它本质上代表了“ pig ”的单转。我无法弄清楚我哪里出了问题?任何建议都会有所帮助。

示例输出示例:

-掷出 6
-掷出 6
-掷出 5
-掷出 6
- 回合得分为 23

最佳答案

如果我理解正确,那么你可以简化很多,像这样:

import random
print("Well, hello there.")
score = 0
while score < 20:
a = random.randint(1,6)
print("A {} was rolled".format(a))
score += a
if a == 1:
print("Pigged out!")
score = 0
break
print("Turn score is {}".format(score))

关于模拟掷 6 面骰子并将每次掷骰的结果相加直到掷出 1 的 Python 程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52572593/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com