【发布时间】:2020-09-18 00:53:42
【问题描述】:
我是编程新手,我有一个自己无法完成的任务。
任务是编写一个程序,让您决定掷多少个骰子,从 1 到 5,并检查您是否输入错误。 每次滚动后,将显示骰子的数量和到目前为止的总数。 如果骰子掷出 6,则它不包括在总数中,您将获得更多的掷骰。这就是我卡住的地方。如果骰子变成 6,我希望我的程序重新启动循环,并且仅将 2 掷到 sumDices 并继续循环,但我无法让它工作。
这是我的代码:
import random
numDices=0
total = 0
print("------------")
print("DICE ROLLING")
print("------------")
print()
exit=False
reset=False
while True:
while True:
numDices=int(input("How many dices to throw? (1-5) "))
if numDices<1 or numDices>5:
print("Wrong input, try again")
break
while True:
if reset==True:
break
for i in range(numDices):
dicesArray = list(range(numDices))
dicesArray[i] = random.randint(1, 6)
print(dicesArray[i])
total += dicesArray[i]
if dicesArray[i] == 1:
print("You rolled a one, the total is: ",str(total))
elif dicesArray[i] == 2:
print("You rolled a two, the total is: ",str(total))
elif dicesArray[i] == 3:
print("You rolled a three, the total is: ",str(total))
elif dicesArray[i] == 4:
print("You rolled a four, the total is: ",str(total))
elif dicesArray[i] == 5:
print("You rolled a five, the total is: ",str(total))
elif dicesArray[i] == 6:
total-=6
numDices+=2
print("You rolled a six, rolling two new dices")
reset=True
print("The total sum is",str(total),"with",numDices,"number of rolls.")
print()
restart=(input("Do you want to restart press Enter, to quit press 9. "))
if restart=="9":
exit=True
break
else:
print()
break
if exit==True:
break
【问题讨论】:
-
你遇到了什么错误?
-
我没有收到错误,问题是如果骰子掷出六,它不会再掷两次,它只是加到总和中并从总数中减去,而不掷两次更多次。如果骰子掷出六,则需要再掷两次,加上原来的掷数。对不起我的英语不好
-
为什么是第 35 行
total-=6? -
因为如果骰子掷出六,那不应该计入总数,而是多掷两次。新卷应添加到总数中,除非其中之一是六,然后重新滚动计数。
标签: python python-3.x list random dice