首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >岩石,纸,剪刀巨蟒

岩石,纸,剪刀巨蟒
EN

Stack Overflow用户
提问于 2017-03-29 03:12:51
回答 1查看 322关注 0票数 0

我正在学习巨蟒,并编写岩石,纸张和剪刀游戏。我能够运行这个程序,但是每次程序经过嵌套的if,elif,else语句时,输出仍然是outer语句,不管humplayer变量的值是什么。有什么帮助吗?

谢谢!

代码语言:javascript
复制
    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()
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-03-29 03:55:02

当您重新请求来自人类的输入时,您并不是将其转换为int。而不是重写这一行:

代码语言:javascript
复制
humplayer=input("Enter your move (0 for Rock, 1 for Paper, and 2 for Scissors): ") 

你为什么不用你写的函数

代码语言:javascript
复制
humplayer = hum_player()

虽然在您的int函数中正确地转换为hum_player(),但并不是在请求的所有其他地方。你也可以重复使用这个功能。这就是为什么我们首先编写单独的函数(所以我们不必重复我们已经编写的代码)。

第二件事有点不对,就是你使用statistics()方法的方式。

代码语言:javascript
复制
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.")

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

代码语言:javascript
复制
'print(statistics(...))`

实际的statistics方法返回None,因此这个print语句将打印None (尽管实际的函数调用仍将打印到控制台)。更好的方法是要么直接调用该方法:

代码语言:javascript
复制
statistics(...)

如果没有print语句,或者更改,您将使用统计方法返回字符串,而不是打印字符串。

代码语言:javascript
复制
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
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/43083630

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档