岩石,纸,剪刀蟒蛇

时间:2017-03-29 03:12:51

标签: python-3.x

我正在学习python并编写摇滚,纸张和剪刀游戏。我能够运行该程序,但每次程序通过嵌套的if,elif,else语句时,无论 else 语句> humplayer 变量是。有什么帮助吗?

谢谢!

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

1 个答案:

答案 0 :(得分:0)

当您重新询问人类的输入时,您不会将其投射到int。而不是重写这一行:

humplayer=input("Enter your move (0 for Rock, 1 for Paper, and 2 for Scissors): ") 
为什么不呢 你使用你写的和做的功能,

humplayer = hum_player()

虽然您已在int功能中正确地投射到hum_player(),但您并未在所有其他需要重新查询的地方投放。您也可以重复使用此功能。这就是我们首先编写单独函数的原因(因此我们不必重复我们已经编写的代码)。

另一个有点不对的是你使用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.")

如上所述,这实际上是可以的。问题是你用

调用它
'print(statistics(...))`

实际的statistics方法返回None所以这个print语句将打印None(尽管实际的函数调用仍将打印到控制台)。更好的方法是调用方法:

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.")