我试图用Python编写一个翻转硬币并返回最长系列头部和尾部的程序。它询问用户翻转硬币的次数。出于某种原因,我的程序没有运行,我无法弄清楚原因。我不知道为什么它不会要求用户提供"翻转次数"和"作为字符串"例如。
import random
def flip():
flipValue = random.randint(1,2)
if flipValue == 1:
side = "Heads"
else:
side = "Tails"
return side
def nStreak():
number = int(input("Number of flips: "))
chars = int(input("As a character string: "))
series = 0
heads = 0
tails = 0
longest_h = 0
longest_t = 0
while series != number:
side = flip()
series += 1
if side == "Heads":
heads += 1
tails = 0
if heads == chars:
longest_h += 1
heads = 0
if side == "Tails":
tails += 1
heads = 0
if tails == chars:
longest_t += 1
tails = 0
print("Number of heads streaks: ", longest_h)
print("Number of tails streaks: ", longest_t)
当我跑步时,我什么都没得到。
答案 0 :(得分:0)
您需要在最后一行调用该函数。
nStreak()
否则它不会执行代码。
答案 1 :(得分:0)
在您的脚本底部添加nStreak()
,没有任何标识。