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

巨蟒石布剪刀
EN

Stack Overflow用户
提问于 2014-02-23 07:00:54
回答 4查看 1.4K关注 0票数 0

我在这部分作业上遇到了麻烦。我必须声明游戏的胜利者,然后输入到一个函数中。一旦我输入了所有的if语句,我就必须创建一个函数def playGame()。这必须包括:

代码语言:javascript
复制
showRules()
user = getUserChoice()
computer = getComputerChoice()
declareWinner(user, computer)

def playGame():
    showRules()
    user = getUserChoice()
    computer = getComputerChoice()
    declareWinner(user, computer)

print("**** Rock-Paper-Scissors ****")

#Display game rules
def showRules():
    print("Rules: Each player chooses either Rock, Paper, or Scissors.")
    print("\tThe winner is determined by the following rules:")
    print("\t\tScissors cuts Paper   --> Scissors wins")
    print("\t\tPaper covers Rock     --> Paper wins")
    print("\t\tRock smashes Scissors --> Rock Wins")


#User selection
def getUserChoice():
    choice = (input("Make your selection (Rock, Paper, or Scissors). ")).lower()
    return choice

#obtain computer input
def getComputerChoice():
    import random
    rnum = random.randint(1,3)
    if rnum == 1:
        print("The computer has chosen Rock.")
    if rnum == 2:
        print("The computer has chosen Paper.")
    if rnum == 3:
        print("The computer has chosen Scissors.")
    return rnum

#Declare winner
def declareWinner(user, rnum):
    print("\n\nuser: ", user)
    print("rnum: ", rnum)    

    if ((user == "rock") and (rnum == 3)):
        print("Rock smashes Scissors --> Player wins!")    

    if ((user == "paper") and (rnum == 1)):
        print("Paper covers Rock --> Player wins!")

    if ((user == "Scissors") and (rnum == 2)):
        print("Scissors cuts Paper --> Player wins!")

下面是我运行程序时得到的输出:

代码语言:javascript
复制
**** Rock-Paper-Scissors ****
playGame()
Rules: Each player chooses either Rock, Paper, or Scissors.
    The winner is determined by the following rules:
        Scissors cuts Paper   --> Scissors wins
        Paper covers Rock     --> Paper wins
        Rock smashes Scissors --> Rock Wins
Make your selection (Rock, Paper, or Scissors). ROCk
The computer has chosen Rock.


user:  rock
rnum:  1

我需要输出如下所示:

代码语言:javascript
复制
Rules: Each player chooses either Rock, Paper, or Scissors.
    The winner is determined by the following rules:
        Scissors cuts Paper   --> Scissors wins
        Paper covers Rock     --> Paper wins
        Rock smashes Scissors --> Rock Wins
Make your selection (Rock, Paper, or Scissors). Paper

The computer has chosen Paper.

Paper covers Rock --> Player wins!

请帮帮忙。

我相信有更有效的方法可以做到这一点。在本课程的这一点上,我们正在使用函数,我需要知道如何合并它们以使程序函数正常工作。

EN

回答 4

Stack Overflow用户

发布于 2014-02-23 07:07:36

你只有3条if语句--你的程序只知道在这些情况下该怎么做:

代码语言:javascript
复制
if ((user == "rock") and (rnum == 3)):
if ((user == "paper") and (rnum == 1)):
if ((user == "Scissors") and (rnum == 2)):

你必须包括一些平局,获胜和失败的东西

代码语言:javascript
复制
if ((user == "rock") and (rnum == 1)):
    #Whatever you want it to do
if ((user == "rock") and (rnum == 2)):
    #Stuff
if ((user == "rock") and (rnum == 3)):
    #Etc
票数 2
EN

Stack Overflow用户

发布于 2014-02-23 07:07:08

看看你的情况;没有一个与你提供的样本中发生的情况相匹配。您不具备user == rockrnum == 1的条件。这就是为什么您的条件语句中没有任何print语句被打印出来的原因。

票数 1
EN

Stack Overflow用户

发布于 2014-02-23 07:15:33

您的代码可能不完整,或者缺少您选择的大小写。您的代码中没有平局,但结果显示是平局。

另外,我会使用ifelif来模仿“大小写”,就像你在其他语言中做的那样,并规范化用户输入:

代码语言:javascript
复制
rock == 1
paper == 2

这样你就可以更容易地比较它们;

代码语言:javascript
复制
if user == rnum:
 # print tie message
else:
    if user == 1 and rnum ==2:
     #dosomething
     elif user == 1 and rnum == 3:
     # do something else

诸若此类

另一种选择是嵌套if语句

代码语言:javascript
复制
if user == rnum:
# print tie
 else:
    if user == 1:
       if rnum == 2:
        #print
        elif rnum == 2:
        #print
    if user == 2:
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/21961796

复制
相关文章

相似问题

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