2017-03-05 8 views
1

私は下のコードを書いていますが、すべてがうまく動作するようです。その答えは無効であると考えられます。私の論理が正しいように私は何が間違っているのか分かりません。私の「ロック、ペーパー、はさみ」コードで何かが間違っています

print("Welcome to Rock, Paper, Scissors!") 
print("Let's Begin ...") 
name1 = input("Player 1: What's your name?") 
name2 = input("Player 2: What's your name?") 

print("Hello " + name1 + " and " + name2) 
print(name2 + ": Close your eyes!") 

choice1 = input(name1 + ": enter 'r' for rock, 'p' for paper, and 's' for Scissors: ") 

print("Great choice! Now - cover your answer and ask " + name2 + " to choose. \n\n\n") 
choice2 = input(name2 + ": enter 'r' for rock, 'p' for paper, and 's' for scissors: ") 

if(choice1 == "r" , choice2 == "p"): 
    print(name2 + " wins!") 
elif(choice1 == "r" , choice2 == "s"): 
    print(name1 + " wins!") 
elif(choice1 == "r" , choice2 == "r"): 
    print("It is a draw") 
elif(choice2 == "r" , choice1 == "p"): 
    print(name1 + " wins!") 
elif(choice2 == "r" , choice1 == "s"): 
    print(name2 + " wins!") 
elif(choice1 == "p" , choice2 == "s"): 
    print(name2 + " wins!") 
elif(choice1 == "p" , choice2 == "p"): 
    print("It is a draw!") 
elif(choice2 == "p" , choice1 == "s"): 
    print(name1 + " wins!") 
elif(choice1 == "s" , choice2 == "s"): 
    print("It is a draw!") 
else: 
    print("Invalid asnwer") 


print("Thanks for playing Rock, Paper, scissors") 
+2

を行うことができますか? – Soviut

+0

論理は正しいですし、構文も...少なくともインタプリタによってエラーを引き起こさないように修正してください。 if文でカンマを見直して、コンパイルエラーが発生していると思われる場合があります。 –

答えて

1

あなたの全体の方法が間違っている - あなたも届くことは決してないだろうので、

if(choice1 == "r" , choice2 == "p"): 

は、それが空であってはならないよう常にtruthyなりますchoice1 == "r", choice2 == "p"によって形成されたタプルのtruthinessを取りますelifステートメントとプレーヤー2は常に勝つでしょう。

両方の条件を確認する正しい方法は、and演算子であり、かっこは必要ありません。

if choice == "r" and choice2 == "p": 

これらの条件をすべて修正すると役立ちます。

0

変更 '' このように 'と' のための条件文、すべての場合で:

if choice1 == "r" and choice2 == "p": 
    print(name2 + " wins!") 

注:括弧は必需品ではありません。

「name2 wins」はいつも得意ですか?

コンマのため、tupleを作成すると、空でない場合はtrueと評価されます。複数の条件をチェックするための

0

あなたはそれを文から来る場合は、あなたの中にコンマを置くためにあなたのアイデアでした

 

    choice = [choice1, choice2] 
    if(choice == ['r', 'p']): 
     print(name2 + " wins! ") 
    elif(choice == ['r', 'p']): 
     print(name1 + " wins!") 
    elif(choice == ['r', 'r']): 
     print("It is a draw") 
    elif(choice == ['p', 'r']): 
     print(name1 + " wins!") 


関連する問題