我想打印出结果1:正面,结果2:背面等,但我只能一直打印到结果1。我如何积累它?
import random
heads = 0
tails = 0
toss = heads + tails
count = int(input("How many coin tosses would you like to simulate?"))
max_result,curr_result = 0,0
max_head_streak,max_tail_streak,curr_head_streak,curr_tail_streak = 0,0,0,0
while heads + tails < count:
coin = random.randint(1, 2)
if coin == 1:
heads = heads + 1
curr_head_streak += 1
curr_tail_streak = 0
print("Result",curr_result + 1,": Heads")
else:
tails = tails + 1
curr_tail_streak += 1
curr_head_streak = 0
print("Result",curr_result + 1,": Tails")
max_head_streak = max(max_head_streak,curr_head_streak)
max_tail_streak = max(max_tail_streak,curr_tail_streak)
print("The total amount of heads: ",heads)
print("the best streak of heads: ",max_head_streak)
print("The total amount of tails: ",tails)
print("The best streak of tails: ",max_tail_streak)
该程序的其余部分按我想要的方式工作。
答案 0 :(得分:4)
您不会更新curr_result
变量
建议的解决方案:
import random
heads = 0
tails = 0
toss = heads + tails
count = int(input("How many coin tosses would you like to simulate?"))
max_result,curr_result = 0,0
max_head_streak,max_tail_streak,curr_head_streak,curr_tail_streak = 0,0,0,0
while heads + tails < count:
coin = random.randint(1, 2)
curr_result += 1
if coin == 1:
heads = heads + 1
curr_head_streak += 1
curr_tail_streak = 0
print("Result",curr_result,": Heads")
else:
tails = tails + 1
curr_tail_streak += 1
curr_head_streak = 0
print("Result",curr_result,": Tails")
max_head_streak = max(max_head_streak,curr_head_streak)
max_tail_streak = max(max_tail_streak,curr_tail_streak)
print("The total amount of heads: ",heads)
print("the best streak of heads: ",max_head_streak)
print("The total amount of tails: ",tails)
print("The best streak of tails: ",max_tail_streak)
答案 1 :(得分:1)
在第8行的while循环上方,将变量curr_result
初始化为等于0
。
然后在您编写的第17和22行中引用此变量:
print("Result",curr_result + 1,": Heads")
print("Result",curr_result + 1,": Tails")
由于您已将curr_result
初始化为等于0
,因此在第一次迭代中,将打印其中任何一个
Result1: Heads
或
Result1: Tails
但是,当程序再次迭代时,curr_result
仍等于0,因为您实际上尚未更新变量,所以您只打印了变量加一。
为解决此问题,我建议在if / else语句下添加以下行,但仍在while循环内:
curr_result += 1
答案 2 :(得分:1)
其他人似乎已经回答了为什么您的代码无法正常工作,因此这只是使用itertools.groupby
和collections.defaultdict
解决问题的另一种方法:
import random
from collections import defaultdict
from itertools import groupby
count = int(input("How many coin tosses would you like to simulate? "))
tally = defaultdict(int)
results = []
sides = ["tails", "heads"]
for i in range(count):
side = random.choice(sides)
tally[side] += 1
results.append(side)
# Iterates over results, creating groups by repeated entries.
groups = [list(g) for _, g in groupby(results)]
# filters the given list of groups for the different results,
# and gives it to the max function
heads_streak = max([len(l) for l in groups if "heads" in l])
tails_streak = max([len(l) for l in groups if "tails" in l])
print(f"The total amount of heads: {tally['heads']}")
print(f"the best streak of heads: {heads_streak}")
print(f"The total amount of tails: {tally['tails']}")
print(f"the best streak of tails: {tails_streak}")
print(results)
输出:
How many coin tosses would you like to simulate? 10
The total amount of heads: 5
the best streak of heads: 2
The total amount of tails: 5
the best streak of tails: 2
['tails', 'heads', 'heads', 'tails', 'heads', 'tails', 'tails', 'heads', 'tails', 'heads']
编辑:上面的代码在groups
中始终存在两种类型的值的前提下工作。我们可以使用"walrus operator" (new in 3.8)来更改该行为:
heads_streak = max(x if (x := [len(l) for l in groups if "heads" in l]) else [0])
tails_streak = max(x if (x := [len(l) for l in groups if "tails" in l]) else [0])
不是我编写的最漂亮的代码,但是它似乎可以工作。
答案 3 :(得分:0)
您不增加curr_result的值。也许您想使用for循环?这可以实现与while循环相同的作用:
import random
heads = 0
tails = 0
toss = heads + tails
count = int(input("How many coin tosses would you like to simulate?"))
max_result = 0
max_head_streak,max_tail_streak,curr_head_streak,curr_tail_streak = 0,0,0,0
for curr_result in range(count):
coin = random.randint(1, 2)
if coin == 1:
heads = heads + 1
curr_head_streak += 1
curr_tail_streak = 0
print("Result", curr_result + 1,": Heads")
else:
tails = tails + 1
curr_tail_streak += 1
curr_head_streak = 0
print("Result", curr_result + 1,": Tails")
max_head_streak = max(max_head_streak,curr_head_streak)
max_tail_streak = max(max_tail_streak,curr_tail_streak)
print("The total amount of heads: ",heads)
print("the best streak of heads: ",max_head_streak)
print("The total amount of tails: ",tails)
print("The best streak of tails: ",max_tail_streak)