2014-02-17 47 views
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 
+0

功能它們的本質意圖是作爲I/O,意味着它們只應接收輸入(或「void」)和返回值(或「void」)。根據功能的結果,您可以決定您的主要操作。如果您在編寫的每個函數中都開始打印,則只能是時間問題,而無法控制應用程序。只有非常罕見的情況下,你應該打印一個函數(主要是在錯誤或日誌記錄中)。 – 2014-02-17 22:50:16

+0

首先,您需要檢查您當前的功能是否都正常工作。你沒有將規則打印封裝到適當命名的函數中,用戶選擇了一個字符串,但是你測試了一個整數,計算機將只選擇'3'或'None','declarewinner'中的縮進需要檢查。 – jonrsharpe

回答

0

如何回合

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")) 

授予它可能是矯枉過正對於這個,但它是一個很酷的技術...如果你現在可以得到它,它可能會幫助你未來的作業

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!!!")