带计数器的骰子滚动程序
Dice rolling program with counter
我是编程新手,我有一个任务我自己无法解决。
任务是编写一个程序,让您决定掷多少个骰子,1 到 5 个,并检查您是否输入错误。
每次掷骰后,都会显示骰子的数量和到目前为止的总数。
如果骰子掷出 6,则它不包括在总数中,您将获得更多掷骰。这就是我被困的地方。我希望我的程序在骰子变成 6 时重新启动循环,并且只是将 ad 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() 函数退出而不是使用 a
变量?
- 所有的 if 部分真的有必要吗,你不能只打印
数?
这是我的建议:
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):
只要 range(numDices)
为 evaluated/executed,您的 for 循环就会限制迭代次数。当您尝试使用 numDices+=2
增加迭代次数时,它没有任何效果,因为 range(numDices)
仅评估一次。
如果您希望能够更改迭代次数,请使用另一个 while 循环并使用 i
作为计数器。有点像。
i = 0
while i <= numDices:
...
...
if ...:
...
elif ...:
...
i += 1
那么在suite for elif dicesArray[i] == 6:
语句中numDices += 2
会有效的增加迭代次数
我看到另一个你没有提到的问题。您从基于 numDices
原始值的固定长度列表开始,然后使用 i
作为该列表的索引。
dicesArray = list(range(numDices))`
...
dicesArray[i]
...
如果 i
可能大于 original numDices
(大于 len(dicesArray)
),您最终会得到一个 IndexError。您可能应该从一个空列表开始并附加到它。要 获得 最近的掷骰结果,请使用 dicesArray[-1]
而不是 dicesArray[i]
.
...
dicesArray = []
i = 0
while i <= numDices:
dicesArray.append(random.randint(1, 6))
total += dicesArray[-1]
if dicesArray[-1] == 1:
...
...
使用递归函数
import random
total = 0
diceRollList = []
# Define dice rolling function
def rollDice():
rollResult = random.randint(1, 6)
if rollResult == 6:
# If 6 Rolled run two more rolls and sum the results
print("Rolled a 6 Rolling 2 more")
return sum([rollDice() for _ in range(2)])
# If 6 not rolled return the result
print(f"Rolled a {rollResult}")
return rollResult
while True:
numberOfDice = int(input("How many Die to throw: "))
if numberOfDice not in range(1, 6):
print("Number of dice should be between 1 and 5")
break
for dice in range(numberOfDice):
print(f"Rolling Dice {dice}")
# Add result to the total
total += rollDice()
print(f"Running Total: {total}")
这个跟踪它滚动了多少:
import random
a = int(0)
b = int(0)
c = int(0)
d = int(0)
e = int(0)
f = int(0)
limit = 101
count = 0
while True:
g = (random.randint(1, 6))
print(g)
count += 1
if g == 1:
a += 1
elif g == 2:
b += 1
elif g == 3:
c += 1
elif g == 4:
d += 1
elif g == 5:
e += 1
elif g == 6:
f += 1
if count > limit:
break
print(f"There are {a} 1's")
print(f"There are {b} 2's")
print(f"There are {c} 3's")
print(f"There are {d} 4's")
print(f"There are {e} 5's")
print(f"There are {f} 6's")
我是编程新手,我有一个任务我自己无法解决。
任务是编写一个程序,让您决定掷多少个骰子,1 到 5 个,并检查您是否输入错误。 每次掷骰后,都会显示骰子的数量和到目前为止的总数。 如果骰子掷出 6,则它不包括在总数中,您将获得更多掷骰。这就是我被困的地方。我希望我的程序在骰子变成 6 时重新启动循环,并且只是将 ad 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() 函数退出而不是使用 a 变量?
- 所有的 if 部分真的有必要吗,你不能只打印 数?
这是我的建议:
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):
只要 range(numDices)
为 evaluated/executed,您的 for 循环就会限制迭代次数。当您尝试使用 numDices+=2
增加迭代次数时,它没有任何效果,因为 range(numDices)
仅评估一次。
如果您希望能够更改迭代次数,请使用另一个 while 循环并使用 i
作为计数器。有点像。
i = 0
while i <= numDices:
...
...
if ...:
...
elif ...:
...
i += 1
那么在suite for elif dicesArray[i] == 6:
语句中numDices += 2
会有效的增加迭代次数
我看到另一个你没有提到的问题。您从基于 numDices
原始值的固定长度列表开始,然后使用 i
作为该列表的索引。
dicesArray = list(range(numDices))`
...
dicesArray[i]
...
如果 i
可能大于 original numDices
(大于 len(dicesArray)
),您最终会得到一个 IndexError。您可能应该从一个空列表开始并附加到它。要 获得 最近的掷骰结果,请使用 dicesArray[-1]
而不是 dicesArray[i]
.
...
dicesArray = []
i = 0
while i <= numDices:
dicesArray.append(random.randint(1, 6))
total += dicesArray[-1]
if dicesArray[-1] == 1:
...
...
使用递归函数
import random
total = 0
diceRollList = []
# Define dice rolling function
def rollDice():
rollResult = random.randint(1, 6)
if rollResult == 6:
# If 6 Rolled run two more rolls and sum the results
print("Rolled a 6 Rolling 2 more")
return sum([rollDice() for _ in range(2)])
# If 6 not rolled return the result
print(f"Rolled a {rollResult}")
return rollResult
while True:
numberOfDice = int(input("How many Die to throw: "))
if numberOfDice not in range(1, 6):
print("Number of dice should be between 1 and 5")
break
for dice in range(numberOfDice):
print(f"Rolling Dice {dice}")
# Add result to the total
total += rollDice()
print(f"Running Total: {total}")
这个跟踪它滚动了多少:
import random
a = int(0)
b = int(0)
c = int(0)
d = int(0)
e = int(0)
f = int(0)
limit = 101
count = 0
while True:
g = (random.randint(1, 6))
print(g)
count += 1
if g == 1:
a += 1
elif g == 2:
b += 1
elif g == 3:
c += 1
elif g == 4:
d += 1
elif g == 5:
e += 1
elif g == 6:
f += 1
if count > limit:
break
print(f"There are {a} 1's")
print(f"There are {b} 2's")
print(f"There are {c} 3's")
print(f"There are {d} 4's")
print(f"There are {e} 5's")
print(f"There are {f} 6's")