Quarter Toss Guessing Game Python

时间:2014-06-18 11:52:34

标签: python random

我想做这个工作,但由于某种原因,我不能让尾巴工作,它继续显示1.还要注意我仍然是新的python。

我也想制作循环。让我们说他们没有得到他们猜测的数字。因此,如果他们拒绝(它再次开始游戏)或者是(它会让他们感谢玩游戏)

这是我到目前为止所拥有的。

import random

print 'Welcome to Heads or Tails Guessing Game'
print ''
print 'Lets say I toss a Quater up in the air. What are the chances it will come up heads or even tails every time split even? 5 times or even 5000 times. Choose an ejucated guess.'
raw_input ('What would your number be? ')
tossQuater, itsHeads, itsTails = 1,1,1
while tossQuater < 5000:
    if random.randint(1,500) == 1:
        itsHeads = itsHeads + 1
        itstails = itsTails + 1
    tossQuater = tossQuater + 1

    if tossQuater == 1300:
        print 'First 1,500 tosses. Heads will come up ' + str(itsHeads) + ' times.' ' But for Tails it came up ' + str(itsTails)+ ' times.'
    if tossQuater == 1900:
        print 'Second 2,000 tosses. Heads will come up ' + str(itsHeads) + ' times damm!.' ' But for Tails it came up ' + str(itsTails)+ ' times.'
    if tossQuater == 3050:
        print 'Third and final 1,500 tosses. Heads will come up ' + str(itsHeads) + ' times...' ' But for Tails it came up ' + str(itsTails)+ ' times...'

print ''
print 'Out of 5 times or even 5000 times. Well the number of heads showed out to be.' + str(itsHeads) + ' times! ' ' But from Tails it showed ' + str(itsTails) + ' times..'
raw_input ('\n\nDid the number you choose was the right one?')

3 个答案:

答案 0 :(得分:2)

您正在递增itstails变量并打印出itsTails变量。因此,当您打印出itsTails变量时,它会显示1(因为它没有增加)。您的代码应该像

itsTails = itsTails+1

除此之外,你的逻辑似乎也存在缺陷。我更喜欢以下代码:

import random

while tossQuater < 5000:
    if random.randint(1,2) == 1:
        itsHeads = itsHeads + 1
    else:
        itsTails = itsTails + 1
    tossQuater = tossQuater + 1

我设定的条件是,如果我得到1,那将是头,如果我得到2,那将是尾巴。这是一个公平的假设,但不是一个完全现实的假设,因为即使是randint方法也会根据一些内置函数生成一个数字。

答案 1 :(得分:1)

对于您的一般问题,您需要处理您的逻辑。为什么要在两个&#34; itsHeads&#34;和&#34; itsTails&#34;在同一时间?

另外,你需要看看&#34; randint&#34;作品。就像现在一样,你正在扔一枚500面硬币,只检查它是否落在其中一面。

还有更多的逻辑错误。你似乎在这里使用了很多特定的数字。是否有一个原因?请编辑其中一些以使问题更清晰。

答案 2 :(得分:0)

我重写了一些代码,因为我不确定你在中间部分尝试做什么。下面将不断循环,直到用户声明他们不想继续播放。

我已经制作了一个玩游戏并掷硬币的功能,随时可以询问您是否有任何关于功能或任何其他问题的问题

 import random


def toss(flips):
    heads=0;tails=0
    for i in range(0,flips):
        if random.random()>0.5:
            heads+=1
    return heads

def guessing_game():
    print 'Welcome to Heads or Tails Guessing Game'
    print ''
    print '''Lets say I toss a Quater up in the air 5000 imes.
            How many times will it come up heads?
            5 times or even 5000 times. Choose an educated guess.'''
    guess = raw_input ('What would your number be? ')

    ## compare the guess and the answer
    heads=toss(5000)
    print 'You guessed '+str(guess)+' heads and the actual answer was '+str(heads)
    if heads==guess:
        print 'Well Done'
    else:
        print 'Better luck next time'

    return  raw_input ('\n\n Do you want to play again (y/n)?')


play='y'
while play<>'n':
    play= guessing_game()
相关问题