我是编程新手,我有一个我自己无法解决的任务。
任务是制作一个程序,让您确定输入多少个骰子(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
您可以通过以下方式进行操作,稍微修改代码,计算可用的骰子。我也减少了嵌套循环
import random
numDices=0
total = 0
print("------------")
print("DICE ROLLING")
print("------------")
print()
start = True
while start:
numDices=int(input("How many dices to throw? (1-5) "))
if numDices<1 or numDices>5:
print("Wrong input, try again")
break
total = 0
dices_counter = 0
while numDices > 0 :
eyes = random.randint(1, 6)
dices_counter+=1
total += eyes
if eyes == 1:
print("You rolled a one, the total is: ",str(total))
numDices-=1
elif eyes == 2:
print("You rolled a two, the total is: ",str(total))
numDices-=1
elif eyes == 3:
print("You rolled a three, the total is: ",str(total))
numDices-=1
elif eyes == 4:
print("You rolled a four, the total is: ",str(total))
numDices-=1
elif eyes == 5:
print("You rolled a five, the total is: ",str(total))
numDices-=1
elif eyes == 6:
total-=6
numDices+=2
print("You rolled a six, rolling two new dices")
print("The total sum is",str(total),"with",dices_counter,"number of rolls.")
print()
start=(input("Do you want to restart press Enter, to quit press 9. "))
if start=="9":
break
else:
print()
start = True
为了解决您的问题,我将您的for循环替换为while循环
此外,我在您的代码中看到很多不必要的内容,我将尝试列出它们:
您为什么使用那么多的“ while True”?
为什么不使用exit()函数退出而不是使用变量?
这是我的建议:
import random
remaining_dices=0
total = 0
print("------------")
print("DICE ROLLING")
print("------------")
print()
while True:
remaining_dices=int(input("How many dices to throw? (1-5) "))
if remaining_dices<1 or remaining_dices>5:
print("Wrong input, try again")
break
dicesArray = list()
while remaining_dices>0:
dice_value = random.randint(1, 6)
dicesArray.append(dice_value)
print(dice_value)
total += dice_value
remaining_dices -= 1
if(dice_value == 6):
total-=6
remaining_dices +=2
print("You rolled a 6, rolling two new dices")
else:
print("You rolled a " + str(dice_value) + ", the total is : " +str(total))
restart=(input("Do you want to restart press Enter, to quit press 9. "))
if restart=="9":
exit()
else:
print()
for i in range(numDices):
您的for循环会在评估/执行range(numDices)
后立即限制迭代次数。当您尝试使用numDices+=2
增加迭代次数时,它没有任何效果,因为range(numDices)
仅被评估一次。
如果要更改迭代次数,请使用另一个while循环,并使用i
作为计数器。有点像。
i = 0
while i <= numDices:
...
...
if ...:
...
elif ...:
...
i += 1
然后在elif dicesArray[i] == 6:
套件中,语句numDices += 2
将有效地增加迭代次数。
我看到您没有提到的另一个问题。您将从基于numDices
原始值的固定长度列表开始,然后将i
用作该列表的索引。
dicesArray = list(range(numDices))`
...
dicesArray[i]
...
如果i
可能大于原始的numDices
(大于len(dicesArray)
),则您将欣然收到IndexError。您应该以一个空列表开头,然后追加到get最后掷骰子,使用dicesArray[-1]
代替dicesArray[i]
。
...
dicesArray = []
dicesArray.append(random.randint(1, 6))
total += dicesArray[-1]
if dicesArray[-1] == 1:
...
...