2017-03-29 5 views
0

私は、pythonを学習し、岩、紙、はさみのゲームをコーディングしています。私はプログラムを実行することができますが、プログラムがネストされたif、elif、elseステートメントを通過するたびに、出力はhumplayer変数の値に関係なく、外部elseステートメントに続きます。どんな助け?ロック、ペーパー、はさみのpython

ありがとうございました!

import random 
def main(): 
    playerName=introduction() 
    humplayer=hum_player() 
    compchoice=com_player() 
    tieScore = 0 
    humScore=0 
    comScore=0 
    humplayer=input("Enter your move (0 for Rock, 1 for Paper, and 2 for Scissors): ") 
    while humplayer != -1: 
     compchoice=com_player() 
     result= evaluate_Game(humplayer,compchoice,playerName) 
     if result==0: 
      print("It's a tie!") 
      tieScore+=1 
     elif result==1: 
      comScore+=1 
     else: 
      humScore+=1 
     humplayer=input("Enter your move (0 for Rock, 1 for Paper, and 2 for Scissors): ") 
    print(statistics(playerName,humScore,comScore,tieScore)) 



def introduction(): 
    print("Welcome to the game of Rock, Paper, Scissors. You will be playing against the computer.") 
    name= input("What is your name? ") 
    print("Here are the rules", name+":") 
    print(" If a player chooses Rock and the other chooses Scissors, Rock wins.") 
    print(" If a player chooses Scissors and the other chooses Paper, Scissors wins.") 
    print(" If a player chooses Paper and the other chooses Rock, Paper wins.") 
    print(" If both players make the same choice, it's a tie.") 
    print(" Enter -1 to quit the game ") 
    return name 

def hum_player(): 
      choice = int(input("Enter your move (0 for Rock, 1 for Paper, and 2 for Scissors): ")) 
      return choice 

def com_player(): 
      random_Num = random.randint(0,2) 
      return(random_Num) 




def evaluate_Game(humplayer,compchoice,playerName): 
    a = "Rock" 
    b="Paper" 
    c="Scissors" 
    if humplayer==0: 
     if compchoice==0: 
      return 0 
     elif compchoice==1: 
      print(playerName, "plays ",a," computer plays",b) 
      print("Paper covers Rock, Computer wins!") 
      return 1 
     else: 
      print(playerName, "plays",a,"computer plays", c) 
      print("Rock crushes Scissors ,", playerName," wins!") 
      return 2 
    elif humplayer==1: 
     if compchoice==0: 
      print(name,"plays",b," computer plays", a) 
      print("Paper covers Rock.", playerName,"wins!") 
      return 2 
     elif compchoice==1: 
      print(playerName,"plays", b," computer plays" , b) 
      print("It's a tie!") 
      return 0 
     else: 
      print(playerName, "plays", b,"computer plays", c) 
      print("Scissors cuts Paper. Computer wins!") 
      return 1 
    else: 
     if compchoice==0: 
      print(playerName, "plays", c," computer plays", a) 
      print("Rock breaks Scissors. Computer wins!") 
      return 1 
     elif compchoice==1: 
      print(playerName, "plays", c, " computer plays", b) 
      print("Scissors cuts Paper." , playerName, "wins!") 
      return 2 
     else: 
      print(playerName, "plays", c," computer plays", c) 
      print("It's a tie!") 
      return 0 

def statistics(playerName,humScore,tieScore,comScore): 
    print("There were", tieScore+comScore+humscore, "games:", playerName, "won", humScore, "games, the computer won", comScore, "games and there were", tieScore, "ties.") 

main() 

答えて

0

あなたがintにキャストされていない、人間からの入力をお願いし-再。むしろ、この行の書き換えより:あなたは適切にあなたがいないあなたのhum_player()機能でintにキャストがドン 「tはあなたが書いた機能を使用して行う、

humplayer = hum_player() 

なぜ

humplayer=input("Enter your move (0 for Rock, 1 for Paper, and 2 for Scissors): ") 

をあなたが再クエリーする他のすべての場所で。この機能を再利用することもできます。それが最初に別々の関数を書く理由です(既に書いたコードを繰り返す必要はありません)。

もう少し間違っているのは、statistics()メソッドを使用している方法です。

def statistics(playerName,humScore,tieScore,comScore): 
    print("There were", tieScore+comScore+humscore, "games:", playerName, "won", humScore, "games, the computer won", comScore, "games and there were", tieScore, "ties.") 

これは実際にはOKです。問題は、実際のstatistics方法はNoneので、このprint文を返し

'print(statistics(...))` 

でそれを呼び出している(実際の関数呼び出しは、まだコンソールに出力しますが)Noneを印刷しようとしていることです。 print文なし

statistics(...) 

、または文字列を返すのではなく、それを印刷するには、あなたしている統計方法を変更:行うには良い方法は、どちらかだけのメソッドを呼び出すことです。

def statistics(playerName,humScore,tieScore,comScore): 
    return ("There were", tieScore+comScore+humscore, "games:", playerName, "won", humScore, "games, the computer won", comScore, "games and there were", tieScore, "ties.") 
+0

ありがとう!私はこの質問を投稿した後に私の入力にint()を含めなかったことに気付きましたが、統計メソッドの問題は私には新しいものでした! –

関連する問題