2016-09-18 4 views
-7

私のプログラムがなぜ動かないのか分かりません。インデントされたブロックを予想しています。私は何回もインデントしなかったか、少ししかインデントしていないことを確認しましたが、問題を見つけることができません。誰も助けることができますか?お願いしてありがとう。インデントされたブロックが予想され、どこか理由がわからない

import random 

userChoice = eval(input("Scissor (0), Rock (1), Paper (2)")) 

computerChoice = random.randint(0,2) 
if computerChoice == 0: 
elif computerChoice == 1: 
elif computerChoice == 2: 

if userChoice == 0: 
elif userChoice == 1: 
elif userChoice == 2: 

if userChoice == 0 and computerChoice == 0: 
    print("The computer is Scissors. You are Scissors. It's a tie!") 
elif userChoice == 0 and computerChoice == 1: 
    print("The computer is Rock. You are Scissors. You lost") 
elif userChoice == 0 and computerChoice == 2: 
    print("The computer is Paper. You are Scissors. You won!") 

if userChoice == 1 and computerChoice == 0: 
    print("The computer is Scissors. You are Rock. You won!") 
elif userChoice == 1 and computerChoice == 1: 
    print("The computer is Rock. You are Rock. It's a tie!") 
elif userChoice == 1 and computerChoice == 2: 
    print("The computer is Paper. You are Rock. You lost") 

if userChoice == 2 and computerChoice == 0: 
    print("The computer is Scissors. You are Paper. You lost") 
elif userChoice == 2 and computerChoice == 1: 
    print("The computer is Rock. You are Paper. You won!") 
elif userChoice == 2 and computerChoice == 2: 
    print("The computer is Paper. You are Paper. It's a tie!") 
+6

if/elif if文は空です。また、 'eval()'の代わりに 'int()'を使用してください。 – Li357

+0

@AndrewL。こんにちはAndrew、もしあなたが/ elifの文が空であれば、どういう意味ですか?わかりません。 – DandyPotato

+0

制御文を空にすることはできません。そのため、 'if ...:'の中に文がない場合は無効です –

答えて

0

これらの空のif/elifのステートメント

例(Pythonの2様式)に体が必要である:

if computerChoice == 0: 
    print 'Something needs to be done here' 
elif computerChoice == 1: 
    print 'Something needs to be done here too' 
elif computerChoice == 2: 
    print 'And something needs to be done here, too' 

明らかにあなたが何であるかのいくつかの実装で、これらのprint文を置き換えますやろうとしている。しかし、そこに何もあることはできません。

例外 - 本当に何もしたくない場合は、のパスを使用しても問題ありません。

+0

if/elifがif/elifがすでにステートメントであると思ったので、if/elifがステートメントの本文を必要としていることに気づいていませんでした。申し訳ありませんが、私はかなり新しいコーディングです。ご清聴ありがとう、ありがとうございました。 – DandyPotato

+0

@DandyPotato:しかし、条件が満たされたときに何もしたくない場合は、なぜそれらの 'if'と' elif'文を持っていますか? – Matthias

+0

たとえば、3のうちの1つの条件だけが満たされ、他の2つの条件に対しては何もしない場合、何かをしたいことがあります。すべての条件に対してパスを使用することは理にかなっていませんが、一部の条件でのみ使用できます。 – paisanco

0

コロン:の後の文がif-elifブロックにありません。 passを追加してみてください。

文が必要な場合は、Pythonのpass文が使用されます。 は構文的には実行されませんが、実行するコマンドやコードはありません。

computerChoice = random.randint(0,2) 
if computerChoice == 0: 
    pass 
elif computerChoice == 1: 
    pass 
elif computerChoice == 2: 
    pass 
+0

コマンドパスについて私に知らせてくれてありがとう、私はそれを試してみました。私はまだまだコーディングするのが新しいです。 – DandyPotato

関連する問題