Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x 石头、纸、剪刀蟒蛇_Python 3.x - Fatal编程技术网

Python 3.x 石头、纸、剪刀蟒蛇

Python 3.x 石头、纸、剪刀蟒蛇,python-3.x,Python 3.x,我正在学习python并编写一个石头、纸和剪刀游戏。我可以运行该程序,但每次程序通过嵌套的if、elif、else语句时,无论humplayer变量的值是什么,输出都将继续是外部的else语句。有什么帮助吗 谢谢大家! import random def main(): playerName=introduction() humplayer=hum_player() compchoice=com_player() tieScore = 0 hum

我正在学习python并编写一个石头、纸和剪刀游戏。我可以运行该程序,但每次程序通过嵌套的if、elif、else语句时,无论humplayer变量的值是什么,输出都将继续是外部的else语句。有什么帮助吗

谢谢大家!

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

当你向人类请求输入时,你并没有将它转换为
int
。而不是重写这一行:

humplayer=input("Enter your move (0 for Rock, 1 for Paper, and 2 for Scissors): ") 
为什么不呢 如果你不使用你编写的函数

humplayer = hum_player()
虽然在
hum\u player()
函数中正确地强制转换为
int
,但在所有其他重新查询的地方都不会。你不妨重复使用这个功能。这就是我们首先编写单独函数的原因(因此我们不必重复已经编写的代码)

第二件有点错误的事情是您使用
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(...)
不使用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.")

感谢在发布这个问题后,我意识到我的输入中没有包含int(),但是统计方法的问题对我来说是个新问题!