我在这部分作业上遇到了麻烦。我必须声明游戏的胜利者,然后输入到一个函数中。一旦我输入了所有的if语句,我就必须创建一个函数def playGame()。这必须包括:
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!")下面是我运行程序时得到的输出:
**** 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我需要输出如下所示:
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!请帮帮忙。
我相信有更有效的方法可以做到这一点。在本课程的这一点上,我们正在使用函数,我需要知道如何合并它们以使程序函数正常工作。
发布于 2014-02-23 07:07:36
你只有3条if语句--你的程序只知道在这些情况下该怎么做:
if ((user == "rock") and (rnum == 3)):
if ((user == "paper") and (rnum == 1)):
if ((user == "Scissors") and (rnum == 2)):你必须包括一些平局,获胜和失败的东西
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发布于 2014-02-23 07:07:08
看看你的情况;没有一个与你提供的样本中发生的情况相匹配。您不具备user == rock和rnum == 1的条件。这就是为什么您的条件语句中没有任何print语句被打印出来的原因。
发布于 2014-02-23 07:15:33
您的代码可能不完整,或者缺少您选择的大小写。您的代码中没有平局,但结果显示是平局。
另外,我会使用if和elif来模仿“大小写”,就像你在其他语言中做的那样,并规范化用户输入:
rock == 1
paper == 2这样你就可以更容易地比较它们;
if user == rnum:
# print tie message
else:
if user == 1 and rnum ==2:
#dosomething
elif user == 1 and rnum == 3:
# do something else诸若此类
另一种选择是嵌套if语句
if user == rnum:
# print tie
else:
if user == 1:
if rnum == 2:
#print
elif rnum == 2:
#print
if user == 2:https://stackoverflow.com/questions/21961796
复制相似问题