我在执行这部分工作时遇到了麻烦。我必须宣布游戏的赢家,然后输入一个功能。输入所有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!
请帮忙。
我确信有更有效的方法可以做到这一点。在本课程中我们正在使用函数,我需要知道如何将它们合并以使程序功能正常工作。
答案 0 :(得分:2)
您只有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
答案 1 :(得分:1)
看看你的情况;它们都不匹配您给出的样本中发生的情况。您没有user == rock
和rnum == 1
的条件。这就是为什么条件中没有print
语句被打印出来的原因。
答案 2 :(得分:0)
您的代码不完整或缺少您选择的案例。您的代码没有关系,但结果显示平局。
另外,我会像使用其他语言一样使用if
和elif
来模仿“case”,并规范化用户输入:
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:
答案 3 :(得分:0)
在几乎所有编程中,您都应该考虑可能的结果以及是否正确处理它们。这基本上是单元测试的作用;测试代码单元的许多合法和非法输入,以便代码仍然按预期运行。
在这种情况下,您遇到了一个意外结果的经典案例,您输入了一件事,但没有得到您想要的结果。如果您检查代码,并且正如其他人所指出的那样,Rock Paper Scissors有3 ^ 2 = 9种可能的匹配,您只需在代码中定义3。这让6人失踪。
现在,你想为其他6场比赛写下案例。你可以写一个if
语句的大清单(简单的if user == computer
将立即解决6个匹配中的3个),但是我会给你一些我认为看起来更干净,表达意图和是可读的,不涉及代码重复,并保持逻辑很好地分开。希望它可以帮到你。
DRAW = 0
LOSE = 1
WIN = 2
ROCK = "rock"
PAPER = "paper"
SCISSORS = "scissors"
def winsAgainst(one, other):
""" Returns true if `one` wins against `other` """
return ((one == ROCK and other == SCISSORS) or
(one == PAPER and other == ROCK) or
(one == SCISSORS and other == PAPER))
def reasonOfWinning(winner):
""" A description of WHY the winner actually won """
if winner == ROCK:
return "Rock crushes Scissors"
if winner == PAPER:
return "Paper covers Rock"
if winner == SCISSORS:
return "Scissors cut Paper"
def match(user, computer):
"""
Matches up user and computer and returns who won.
Both user and computer should be one of ROCK, PAPER or SCISSORS.
"""
# This handles 3 cases of drawing
if user == computer:
return DRAW
# Handles 3 cases where user wins
if winsAgainst(user, computer):
return WIN
# Handles the last 3, where computer wins.
else:
return LOSE
def playGame():
showRules()
user = getUserChoice()
computer = getComputerChoice()
outcome = match(user, computer)
if(outcome == DRAW):
print "Awhh, it's a draw"
elif(outcome == WIN):
print "{} --> Player wins!".format(reasonOfWinning(user))
elif(outcome == LOSE):
print "{} --> Computer wins!".format(reasonOfWinning(computer))