2017-12-13 5 views
-3

シンプルなpython 2のコイン投げゲームです。わからないところ私が間違っているつもりですが、私は「あなたはそれを得た」印刷する正しい構文をコーディングすることはできません、ユーザーが正しく推測するとき....コイン投げゲームのPythonバグ

import random 
guess = '' 
while guess not in ('heads', 'tails'): 
    print('Guess the coin toss! Enter heads or tails:') 
    guess = input() 
toss = random.randint(0, 1) # 0 is tails, 1 is heads 
if toss == guess: 
    print('You got it!') 
else: 
    print('Nope! Guess again!') 
    guess = input() 
    if toss == guess: 
     print('You got it!') 
    else: 
     print('Nope. You are really bad at this game.') 
+1

'heads''tails'0とし、1

の変化を比較しているあるかもしれない '1の場合です!= ' 1''? –

+5

それは '1 '=' 1 'でさえありません。 '1!= 'heads''です。 –

答えて

0

あなたは

import random 
guess = '' 
faces = ('heads', 'tails') 
while guess not in faces: 
    print('Guess the coin toss! Enter heads or tails:') 
    guess = input() 
toss = faces[random.randint(0, 1)] # 0 is tails, 1 is heads 
#print(toss) 
if toss == guess: 
    print('You got it!') 
else: 
    print('Nope! Guess again!') 
    guess = input() 
    if toss == guess: 
     print('You got it!') 
    else: 
     print('Nope. You are really bad at this game.') 

https://repl.it/repls/GenuineThinPterosaurs

3

を、簡単な答えは、あなたがあなたのトスを変更することです:

toss = random.choice(['heads', 'tails']) 
0

あなたは乱数に「頭」を当てています。文字列内の適切な単語にインデックスを付けるには、乱数を使用します。あなたは間違って何をすべきか

coin = ('heads','tails') 

toss = coin[random.randint(0, 1)] 
+1

'toss = coin [random.randint(0、1)]' –

+0

ありがとう!なぜこれがうまくいかないのか、私は狂っているようでした!丸括弧はPythonのTUPLEを意味します。正方形はリストを意味します。この場合、括弧( '[]')は添え字演算子であり、括弧( '()')は呼び出し(関数呼び出し)演算子になります。 – Dasman

+0

'coin [random.randint(0、1)]'は、コインのインデックス0または1の値を選択します(ここではリストやタプルは無関係です)。 –

関連する問題