我对Python非常陌生,我必须创建一个模拟翻转硬币的游戏,并要求用户输入掷硬币的次数。基于该响应,程序必须选择0或1的随机数(并且决定哪个代表“头”并且代表“尾部”)指定的次数。计算“头部”的数量和产生的“尾部”的数量,并向用户呈现以下信息:由模拟的硬币投掷组成的列表,以及头部数量和产生的尾部数量的摘要。例如,如果用户输入5,则掷硬币模拟可能会导致['head','tails','tails','head','heads']。该程序应该打印如下内容:“['head','tails','tails','head','heads']
这是我到目前为止所做的,而且根本不起作用......
import random
def coinToss():
number = input("Number of times to flip coin: ")
recordList = []
heads = 0
tails = 0
flip = random.randint(0, 1)
if (flip == 0):
print("Heads")
recordList.append("Heads")
else:
print("Tails")
recordList.append("Tails")
print(str(recordList))
print(str(recordList.count("Heads")) + str(recordList.count("Tails")))
答案 0 :(得分:7)
您需要loop
才能执行此操作。我建议使用for
循环:
import random
def coinToss():
number = input("Number of times to flip coin: ")
recordList = []
heads = 0
tails = 0
for amount in range(number):
flip = random.randint(0, 1)
if (flip == 0):
print("Heads")
recordList.append("Heads")
else:
print("Tails")
recordList.append("Tails")
print(str(recordList))
print(str(recordList.count("Heads")) + str(recordList.count("Tails")))
我建议你阅读this on for
loops。
此外,您可以将number
作为parameter to the function传递:
import random
def coinToss(number):
recordList, heads, tails = [], 0, 0 # multiple assignment
for i in range(number): # do this 'number' amount of times
flip = random.randint(0, 1)
if (flip == 0):
print("Heads")
recordList.append("Heads")
else:
print("Tails")
recordList.append("Tails")
print(str(recordList))
print(str(recordList.count("Heads")) + str(recordList.count("Tails")))
然后,您需要在最后调用该函数:coinToss()
。
答案 1 :(得分:5)
你快到了:
1)你需要调用函数:
coinToss()
2)您需要设置一个循环来重复调用random.randint()
。
答案 2 :(得分:2)
我会采取以下方式:
from random import randint
num = input('Number of times to flip coin: ')
flips = [randint(0,1) for r in range(num)]
results = []
for object in flips:
if object == 0:
results.append('Heads')
elif object == 1:
results.append('Tails')
print results
答案 3 :(得分:2)
这可能更加pythonic,虽然不是每个人都喜欢列表理解。
import random
def tossCoin(numFlips):
flips= ['Heads' if x==1 else 'Tails' for x in [random.randint(0,1) for x in range(numflips)]]
heads=sum([x=='Heads' for x in flips])
tails=numFlips-heads
答案 4 :(得分:1)
import random
import time
flips = 0
heads = "Heads"
tails = "Tails"
heads_and_tails = [(heads),
(tails)]
while input("Do you want to coin flip? [y|n]") == 'y':
print(random.choice(heads_and_tails))
time.sleep(.5)
flips += 1
else:
print("You flipped the coin",flips,"times")
print("Good bye")
你可以尝试这个,我有它所以它会问你是否要翻转硬币然后当你说不,或者它告诉你你翻了几次硬币。 (这是在python 3.5中)
答案 5 :(得分:1)
创建一个包含两个元素的列表,该元素包含头和尾,并使用random中的choice()获得硬币翻转结果。要获得头或尾出现的次数的计数,请将计数附加到列表中,然后使用集合中的Counter(list_name)。使用uin()调用
##coin flip
import random
import collections
def tos():
a=['head','tail']
return(random.choice(a))
def uin():
y=[]
x=input("how many times you want to flip the coin: ")
for i in range(int(x)):
y.append(tos())
print(collections.Counter(y))
答案 6 :(得分:0)
除了这些,您还可以这样做:
import random
options = ['Heads' , 'Tails']
number = int(input('no.of times to flip a coin : ')
for amount in range(number):
heads_or_tails = random.choice(options)
print(f" it's {heads_or_tails}")
print()
print('end')
答案 7 :(得分:0)
我就是这样做的。可能不是最好和最有效的方式,但嘿,现在您有不同的选择可供选择。我循环了 10000 次,因为练习中已经说明了这一点。
#Coinflip program
import random
numberOfStreaks = 0
emptyArray = []
for experimentNumber in range(100):
#Code here that creates a list of 100 heads or tails values
headsCount = 0
tailsCount = 0
#print(experimentNumber)
for i in range(100):
if random.randint(0, 1) == 0:
emptyArray.append('H')
headsCount +=1
else:
emptyArray.append('T')
tailsCount += 1
#Code here that checks if the list contains a streak of either heads or tails of 6 in a row
heads = 0
tails = 0
headsStreakOfSix = 0
tailsStreakofSix = 0
for i in emptyArray:
if i == 'H':
heads +=1
tails = 0
if heads == 6:
headsStreakOfSix += 1
numberOfStreaks +=1
if i == 'T':
tails +=1
heads = 0
if tails == 6:
tailsStreakofSix += 1
numberOfStreaks +=1
#print('\n' + str(headsStreakOfSix))
#print('\n' + str(tailsStreakofSix))
#print('\n' + str(numberOfStreaks))
print('\nChance of streak: %s%%' % (numberOfStreaks / 10000))
答案 8 :(得分:0)
#program to toss the coin as per user wish and count number of heads and tails
import random
toss=int(input("Enter number of times you want to toss the coin"))
tail=0
head=0
for i in range(toss):
val=random.randint(0,1)
if(val==0):
print("Tails")
tail=tail+1
else:
print("Heads")
head=head+1
print("The total number of tails is {} and head is {} while tossing the coin {} times".format(tail,head,toss))
答案 9 :(得分:-1)
import random
def coinToss(number):
heads = 0
tails = 0
for flip in range(number):
coinFlip = random.choice([1, 2])
if coinFlip == 1:
print("Heads")
recordList.append("Heads")
else:
print("Tails")
recordList.append("Tails")
number = input("Number of times to flip coin: ")
recordList = []
if type(number) == str and len(number)>0:
coinToss(int(number))
print("Heads:", str(recordList.count("Heads")) , "Tails:",str(recordList.count("Tails")))