[英]implementing functions in a game
I am having trouble with this part of my assignment. 我在这部分作业中遇到麻烦。 I have to declare a winner of the game and then input into a function. 我必须声明游戏的获胜者,然后输入函数。 Once I have entered all the if statements I then have to create a function def playGame()
. 输入所有if语句后,就必须创建一个函数def playGame()
。 This has to include: 其中必须包括:
showRules()
user = getUserChoice()
computer = getComputerChoice()
declareWinner(user, computer)
I am not too sure how to do this. 我不太确定该怎么做。 Please help. 请帮忙。
Below is the code that I have completed so far: (Would I need to do an if statement for scissors as well?) 以下是到目前为止我已经完成的代码:(我也需要为剪刀做if语句吗?)
#Display game rules
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")
def userchoice():
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):
if userchoice == 1:
print("You have chosen rock.")
if getComputerchoice == 1:
print("Computer has chose rock as well.")
return 0
else:
print("The computer has chosen scissors. Rock breaks scissors. You WIN!")
return 1
if userchoice == 2:
print("You have chosen paper.")
if getComputerchoice == 1:
print("The computer has chosen rock. Paper covers rock. You WIN!")
return 1
elif getComputerchoice == 2:
print("The computer has chosen paper as well.")
return 0
else:
print("The computer has chosen scissors. Scissors cut paper. You LOSE!")
return 1
if userchoice == 3:
print("You have chosen scissors")
if getComputerchoice == 1:
print("The computer has chosen rock. Rock breaks scissors. You LOSE!")
return 1
elif getComputerchoice == 2:
print("The computer has chosen paper. Scissors cut paper. You WIN!")
return 1
how bout 怎么样
class hand:
def __init__(self,rps):
self.rps = ["rock","paper","scissors"].index(rps.lower())
def __cmp__(self,other): #take advantage of pythons __cmp__ and override it
#this is the magic
wins_against = {0:2,1:0,2:1}
#if their equal return 0
if self.rps == other.rps: return 0
#if this hand wins return 1
if wins_against[self.rps] == other.rps:return 1
#else (This hand loses) return -1
return -1
def __str__(self):
return ["rock","paper","scissors"][self.rps]
print( hand("rock") > hand("paper") )
print( hand("scissors") > hand("paper") )
print( hand("rock") > hand("scissors") )
granted its probably overkill for this but it sa cool technique ... and if you can get it down now it may help you with future assignments 授予了它可能的过度杀伤力,但这是一个很酷的技术……如果您现在就把它弄下来,它可能会帮助您完成将来的任务
def get_cpu_pick():
return random.choice(["rock", "paper", "scissors"])
def get_player_pick():
valid_inputs = {
'r':'rock','p':'paper','s':'scissors'
}
user_choice = input("Select RPS:")
while user_choice.lower() not in valid_inputs:
user_choice = input("Select RPS:")
return valid_inputs.get(user_choice.lower())
def play():
user_hand = hand(get_player_pick())
cpu_hand = hand(get_cpu_pick())
if user_hand > cpu_hand:
print ("User Wins {0} v. {1}".format(user_hand,cpu_hand))
elif user_hand < cpu_hand:
print ("CPU Wins {0} v. {1}".format(user_hand,cpu_hand))
else:
print("TIE!!!")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.