0
我在做這部分任務時遇到了麻煩。我必須宣佈遊戲的勝利者然後輸入一個函數。一旦我輸入了所有的if語句,我就必須創建一個函數def playGame()
。這有可能包括:在遊戲中實現功能
showRules()
user = getUserChoice()
computer = getComputerChoice()
declareWinner(user, computer)
我也不太清楚如何做到這一點。請幫忙。
下面是我迄今完成的代碼:(我需要做的,如果剪刀語句呢?)
#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
功能它們的本質意圖是作爲I/O,意味着它們只應接收輸入(或「void」)和返回值(或「void」)。根據功能的結果,您可以決定您的主要操作。如果您在編寫的每個函數中都開始打印,則只能是時間問題,而無法控制應用程序。只有非常罕見的情況下,你應該打印一個函數(主要是在錯誤或日誌記錄中)。 – 2014-02-17 22:50:16
首先,您需要檢查您當前的功能是否都正常工作。你沒有將規則打印封裝到適當命名的函數中,用戶選擇了一個字符串,但是你測試了一個整數,計算機將只選擇'3'或'None','declarewinner'中的縮進需要檢查。 – jonrsharpe