[英]random.randint function not working
This point of this game is to flip a coin 100 times and record either heads or tails.这个游戏的重点是抛硬币 100 次并记录正面或反面。 At the end of 100 flips, the program should print out the number of times it flipped heads and tails.
在 100 次翻转结束时,程序应该打印出它翻转正面和反面的次数。 The program is only printing heads when I run it and I feel that it is doing this by executing the first elif clause instead of the random.randint function.
该程序仅在我运行时打印头,我觉得它是通过执行第一个 elif 子句而不是 random.randint 函数来实现的。
Can anyone help complete this program??谁能帮忙完成这个程序??
import random
print ('Coin flip game')
start = input('Press enter to flip the coin')
coin_flip = random.randint(1, 2)
heads = int(1)
tails = int(2)
heads = int(heads)
tails - int(tails)
count = 0
while coin_flip:
count += 1
if count == 100:
break
elif coin_flip == 1:
print ('Heads')
elif coin_flip == 2:
print ('Tails')
try something like this:尝试这样的事情:
import random
print("Welcome to the coin flipper!")
start = input('Press enter to flip the coin')
count = 0
while True:
coin_flip = random.randint(1,2)
count+=1
if count >100:
break
if coin_flip == 1:
print('Heads')
elif coin_flip == 2:
print("Tails")
this way each iteration of the loop the number will change since its inside the loop这样循环的每次迭代,数字都会改变,因为它在循环内
also you dont need to assign all those variables that you dont even use later and you should use while True:
for the loop你也不需要分配所有那些你以后甚至不使用的变量,你应该使用
while True:
for the loop
How you want it printed is not clear.您希望如何打印尚不清楚。 You could do:
你可以这样做:
>>> import random
>>> print [['H', 'T'][random.randint(0, 1)] for _ in range(100)]
['T', 'T', 'T', 'H', 'T', 'T', 'H', 'H', 'H', 'H', 'T', 'H', 'T', 'H', 'T', 'H', 'H',
'H', 'H', 'H', 'T', 'T', 'H', 'T', 'H', 'H', 'H', 'H', 'H', 'H', 'H', 'H', 'T', 'T',
'H', 'H', 'H', 'H', 'H', 'H', 'H', 'H', 'H', 'H', 'H', 'H', 'H', 'H', 'H', 'H', 'H',
'H', 'H', 'T', 'H', 'T', 'H', 'T', 'H', 'H', 'H', 'T', 'T', 'T', 'H', 'H', 'H', 'T',
'H', 'H', 'T', 'T', 'H', 'H', 'H', 'H', 'H', 'H', 'H', 'T', 'T', 'T', 'H', 'T', 'T',
'H', 'H', 'H', 'H', 'T', 'H', 'H', 'H', 'T', 'T', 'T', 'H', 'H', 'H', 'T',]
or:要么:
>>> heads = sum(random.randint(0, 1) for _ in range(100))
>>> print "heads =", heads, ' tails =', 100 - heads
heads = 46 tails = 54
Your issue is here:你的问题在这里:
while coin_flip: # coin_flip is set to 1 or 2
...
Since coin_flip
is set to 1 or 2, your loop will be infinite, and the important part is that coin_flip
won't change (you are not updating it).由于
coin_flip
设置为 1 或 2,您的循环将是无限的,重要的是coin_flip
不会改变(您没有更新它)。
To fix this, you should update your coin_flip
by assigning it a random number inside the loop:要解决此问题,您应该通过在循环内
coin_flip
分配一个随机数来更新您的coin_flip
:
while True:
count += 1
coin_flip = random.randint(1, 2) # you need to "flip" coin inside the loop
if count == 100:
break
elif coin_flip == 1:
print ('Heads')
elif coin_flip == 2:
print ('Tails')
To count the number of heads, you could have a heads
and tails
variable to keep track:要计算正面的数量,您可以使用
heads
和tails
变量来跟踪:
heads = 0
tails = 0
while True:
count += 1
coin_flip = random.randint(1, 2) # you need to "flip" coin inside the loop
if count == 100:
break
elif coin_flip == 1:
print ('Heads')
heads += 1
elif coin_flip == 2:
print ('Tails')
tails += 1
print "Number of heads: %s, Number of tails: %s" % (heads, tails)
heads,tails,=0,0
import random
for i in range(100):
a=random.randint(1,2)
if(a%2==0):
tails+=1
else:
heads+=1
print 'no. of heads in 100 flips,',heads
print 'no. of tails in 100 flips,',tails
try this out试试这个
I know this is years ago but i fixed Serials' code to count the total number of heads and tails at the end:我知道这是几年前的事了,但我修正了 Serials 的代码来计算最后的正面和反面的总数:
import random
print("Welcome to the coin flipper!")
start = input('Press enter to flip the coin')
count = 0
heads = 0
tails = 0
while True:
coin_flip = random.randint(1,2)
count+=1
if count >100:
break
if coin_flip == 1:
print('Heads')
heads += 1
elif coin_flip == 2:
print("Tails")
tails += 1
print(f'''
Number of tails: {tails}
Number of times heads {heads}
''')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.