Python-我需要我的程序来计算掷出 2 个骰子的每个结果发生了多少次。 (2-12是可能的结果)
Python-I need my program to count how many times each outcome of 2 dice being rolled has happened. (2-12 are the possible outcomes)
import random
num_rolls = int(input('Enter number of rolls:\n'))
results = int()
if num_rolls >= 1:
for i in range(num_rolls):
die1 = random.randint(1,6)
die2 = random.randint(1,6)
roll_total = die1 + die2
results =+ 1
print('Roll %d is %d (%d + %d)' % (i, roll_total, die1, die2))
for j in range(results):
print('\nDice roll statistics:')
print('%ds:'% roll_total, results)
输出应该是这样的:
卷 1 是 6 (3 + 3)
第 2 卷是 7 (3 + 4)
第 3 卷是 6 (4 + 2)
第 4 卷是 5 (2 + 3)
掷骰统计数据:
6s: 1
7s: 2
6s: 3
5s: 4
因为 roll_total = die1 + die2
每次迭代都会覆盖 roll_total
。
您想roll_total += (die1 + die2)
.
(PS: 调试提示,如果你在你的 for 循环中使用 print(roll_total)
你可能会发现这个)
假设您尝试为每个卷打印 roll_total
,将每个 roll_total
的值存储为 results
中的列表。
import random
num_rolls = int(input('Enter number of rolls:\n'))
results = []
if num_rolls >= 1:
for i in range(num_rolls):
die1 = random.randint(1,6)
die2 = random.randint(1,6)
roll_total = die1 + die2
results.append(roll_total)
print('Roll %d is %d (%d + %d)' % (i, roll_total, die1, die2))
print('\nDice roll statistics:')
for j in range(len(results)):
print(f'{j+1}: {results[j]}')
示例输出:
Enter number of rolls:
6
Roll 5 is 9 (3 + 6)
Dice roll statistics:
1: 7
2: 7
3: 7
4: 9
5: 6
6: 9
假设你想要下一个输出:
Enter number of rolls:
11
Roll 1 is 6 (2 + 4)
Roll 2 is 7 (2 + 5)
Roll 3 is 6 (4 + 2)
Roll 4 is 3 (1 + 2)
Roll 5 is 6 (3 + 3)
Roll 6 is 2 (1 + 1)
Roll 7 is 9 (6 + 3)
Roll 8 is 5 (3 + 2)
Roll 9 is 6 (5 + 1)
Roll 10 is 5 (3 + 2)
Roll 11 is 7 (4 + 3)
Dice roll statistics:
6s: 4
7s: 2
3s: 1
2s: 1
9s: 1
5s: 2
你可以做到
import random
num_rolls = int(input('Enter number of rolls:\n'))
statistics = []
helper = 0
if num_rolls >= 1:
for i in range(1,num_rolls+1):
die1 = random.randint(1,6)
die2 = random.randint(1,6)
roll_total = die1 + die2
print('Roll %d is %d (%d + %d)' % (i, roll_total, die1, die2))
stat = '%ds:'% roll_total
final = [stat,1]
if len(statistics) > 0:
for i in statistics:
if stat in i:
i[1] += 1
helper = 1
break
if helper == 0:
statistics.append(final)
helper = 0
else:
statistics.append(final)
print('\nDice roll statistics:')
for i in statistics:
print(*i)
import random
num_rolls = int(input('Enter number of rolls:\n'))
results = int()
if num_rolls >= 1:
for i in range(num_rolls):
die1 = random.randint(1,6)
die2 = random.randint(1,6)
roll_total = die1 + die2
results =+ 1
print('Roll %d is %d (%d + %d)' % (i, roll_total, die1, die2))
for j in range(results):
print('\nDice roll statistics:')
print('%ds:'% roll_total, results)
输出应该是这样的:
卷 1 是 6 (3 + 3)
第 2 卷是 7 (3 + 4)
第 3 卷是 6 (4 + 2)
第 4 卷是 5 (2 + 3)
掷骰统计数据:
6s: 1
7s: 2
6s: 3
5s: 4
因为 roll_total = die1 + die2
每次迭代都会覆盖 roll_total
。
您想roll_total += (die1 + die2)
.
(PS: 调试提示,如果你在你的 for 循环中使用 print(roll_total)
你可能会发现这个)
假设您尝试为每个卷打印 roll_total
,将每个 roll_total
的值存储为 results
中的列表。
import random
num_rolls = int(input('Enter number of rolls:\n'))
results = []
if num_rolls >= 1:
for i in range(num_rolls):
die1 = random.randint(1,6)
die2 = random.randint(1,6)
roll_total = die1 + die2
results.append(roll_total)
print('Roll %d is %d (%d + %d)' % (i, roll_total, die1, die2))
print('\nDice roll statistics:')
for j in range(len(results)):
print(f'{j+1}: {results[j]}')
示例输出:
Enter number of rolls:
6
Roll 5 is 9 (3 + 6)
Dice roll statistics:
1: 7
2: 7
3: 7
4: 9
5: 6
6: 9
假设你想要下一个输出:
Enter number of rolls:
11
Roll 1 is 6 (2 + 4)
Roll 2 is 7 (2 + 5)
Roll 3 is 6 (4 + 2)
Roll 4 is 3 (1 + 2)
Roll 5 is 6 (3 + 3)
Roll 6 is 2 (1 + 1)
Roll 7 is 9 (6 + 3)
Roll 8 is 5 (3 + 2)
Roll 9 is 6 (5 + 1)
Roll 10 is 5 (3 + 2)
Roll 11 is 7 (4 + 3)
Dice roll statistics:
6s: 4
7s: 2
3s: 1
2s: 1
9s: 1
5s: 2
你可以做到
import random
num_rolls = int(input('Enter number of rolls:\n'))
statistics = []
helper = 0
if num_rolls >= 1:
for i in range(1,num_rolls+1):
die1 = random.randint(1,6)
die2 = random.randint(1,6)
roll_total = die1 + die2
print('Roll %d is %d (%d + %d)' % (i, roll_total, die1, die2))
stat = '%ds:'% roll_total
final = [stat,1]
if len(statistics) > 0:
for i in statistics:
if stat in i:
i[1] += 1
helper = 1
break
if helper == 0:
statistics.append(final)
helper = 0
else:
statistics.append(final)
print('\nDice roll statistics:')
for i in statistics:
print(*i)