2014-06-18 3 views
-2

나는이 작업을하고 싶었지만 어떤 이유 때문에 꼬리면이 보이지 않게하기 위해 꼬리 사이드 1을 계속 보여주었습니다. 파이썬에서도 새로운 점을 기억하십시오.Quater Toss Guessing Game 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?') 
+2

'itstails의 =의 itsTails에 대한 quesiotns이있는 경우 문의 주시기, 게임을하고, 동전을 던져하는 기능을 만들었습니다 + 1 '은 새로운 변수를 생성 다른 대문자 사용. (똑같은 사건이 일어 났을 때 Head와 Tails 카운터를 모두 늘리려고하면 논리가 무섭게 깨지기 때문에 아무 의미가 없습니다 ...) – geoffspear

+0

^this ..... try this change 'random. randint (1,2) == 1 : itsHeads의 =의 itsHeads + 다른 1 : itsTails의 =의 itsTails + 1 tossQuater = tossQuater + 1 '나의 부분에 나쁜이었다 엉망 들여 쓰기 –

+0

OPPS에 대한 죄송합니다. 수정 해 주셔서 감사합니다. @Wooble –

답변

2

당신은 itstails 변수를 증가시키고 itsTails 변수를 출력하고 있습니다. 그래서, itsTails 변수를 출력 할 때 1이 표시됩니다 (증가하지 않았으므로). 귀하의 코드는 다음과 같아야합니다

itsTails = itsTails+1 

이외에도 논리에 결함이있는 것으로 보입니다. 내가 한 수 있다면, 그것은 머리 것 나는 2를 얻을 수 있다면, 그것은 꼬리 것이라고 조건을 설정하고

import random 

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

: 나는 다음과 같은 코드를 선호하는 것입니다. 이것은 완벽한 가정이지만 완벽하게 현실적인 것은 아닙니다. 왜냐하면 randint 방법조차도 일부 내장 함수를 기반으로 숫자를 생성하기 때문입니다.

1

일반적인 질문에 대해서는 논리를 연구해야합니다. 왜 "itsHeads"와 "itsTails"에 동시에 1을 더할 것입니까?

또한 "randint"의 작동 방식을 살펴 봐야합니다. 지금은 당신이 500 개의 양면 동전을 던져서 그면 중 하나에 착륙했는지 확인하는 것뿐입니다.

논리 오류가 더 있습니다. 여기서 구체적인 숫자를 많이 사용하는 것 같습니다. 이것에 대한 이유가 있습니까? 질문을 더 명확하게하기 위해 이들 중 일부를 편집하십시오.

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()