2014-10-26 59 views
-1

所以這是我製作的Rock Scissors Paper的快速代碼。它工作正常,它寫得很簡單,與我所見過的其他代碼相關(我認爲),但是我在保持比分方面存在問題。從算法上講,我需要將比分保存在一個類中,在循環Play - Results - 最後一場比賽的比分 - Rematch之外,並且要做到這一點,我必須以某種方式將Score最後一場比賽與Score類連接起來,以便連接每場比賽。如何將連接函數結果給Python中的類2.7

我的代碼是波紋管:

import random 


def main(): 
    pick_list = ["Rock","Paper","Scissors"] 
    player_pick = str(raw_input("Rock, Scissors, Paper! Make your Pick! ")) 
    pc_pick = random.choice(pick_list) 
    if player_pick not in pick_list: 
     print "Oops, looks like you didn't pick correctly. Try again!" 
     main() 
    else:  
     print "Your pick was " + player_pick 
     print "My pick was " + pc_pick 
    results(pc_pick,player_pick) 
    table_score(results,pc_pick,player_pick) 
    Score(pc_pick, player_pick) 
    rematch() 


def results(pc_pick,player_pick): 
    if pc_pick == player_pick: 
     print "It's a tie!" 
    elif pc_pick == "Rock" and player_pick == "Paper": 
     print "You win!" 
     return "Victory" 
    elif pc_pick == "Rock" and player_pick == "Scissors": 
     print "Sorry, I win" 
     return "Loss" 
    elif pc_pick == "Scissors" and player_pick == "Rock": 
     print "You win!" 
     return "Victory" 
    elif pc_pick == "Scissors" and player_pick == "Paper": 
     print "Sorry, I win" 
     return "Loss" 
    elif pc_pick == "Paper" and player_pick == "Scissors": 
     print "You win!" 
     return "Victory" 
    elif pc_pick == "Paper" and player_pick == "Rock": 
     print "Sorry, I win" 
     return "Loss" 


def rematch(): 
    challenge = str(raw_input('Wanna go again? [Y/n]')) 
    positive_answers =['Y','y', 'yes','Yes'] 
    negative_answers = ['N','n','No','no'] 
    if challenge in positive_answers: 
     main() 
    elif challenge in negative_answers: 
     print "Goodbye!" 
     raw_input("Press any key to exit") 
    else: 
     print "Sorry I didn't get that, try again" 
     rematch()  


def table_score(results, pc_pick, player_pick): 
    pc_score = 0 
    player_score = 0 
    if results(pc_pick, player_pick) == "Victory": 
     player_score += 1 
    elif results(pc_pick,player_pick) == "Loss": 
     pc_score += 1 
    else: 
     player_score += 0 
     pc_score += 0 
    print "Player:" + str(player_score) 
    print "Pc:" + str(pc_score) 


class Score(): 
    pass 

main() 

的事情是,因爲我的複賽的功能,我需要保持「節能」的比分地方外循環。我知道這是一個愚蠢的事情,但我只是被卡住了。由於

回答

0

好了,到目前爲止,我已經設法與此代碼來解決這個問題:

import random 


def main(): 
    pick_list = ["Rock","Paper","Scissors"] 
    player_pick = str(raw_input("Rock, Scissors, Paper! Make your Pick! ")) 
    pc_pick = random.choice(pick_list) 
    if player_pick not in pick_list: 
     print "Oops, looks like you didn't pick correctly. Try again!" 
     main() 
    else:  
     print "Your pick was " + player_pick 
     print "My pick was " + pc_pick 
    results(pc_pick,player_pick) 
    table_score(results,pc_pick,player_pick) 
    print "The score so far is... You: ",Score.player_score ,"Me", Score.pc_score 
    rematch() 


def results(pc_pick,player_pick): 
    if pc_pick == player_pick: 
     print "It's a tie!" 
    elif pc_pick == "Rock" and player_pick == "Paper": 
     print "You win!" 
     return "Victory" 
    elif pc_pick == "Rock" and player_pick == "Scissors": 
     print "Sorry, I win" 
     return "Loss" 
    elif pc_pick == "Scissors" and player_pick == "Rock": 
     print "You win!" 
     return "Victory" 
    elif pc_pick == "Scissors" and player_pick == "Paper": 
     print "Sorry, I win" 
     return "Loss" 
    elif pc_pick == "Paper" and player_pick == "Scissors": 
     print "You win!" 
     return "Victory" 
    elif pc_pick == "Paper" and player_pick == "Rock": 
     print "Sorry, I win" 
     return "Loss" 


def rematch(): 
    challenge = str(raw_input('Wanna go again? [Y/n]')) 
    positive_answers =['Y','y', 'yes','Yes'] 
    negative_answers = ['N','n','No','no'] 
    if challenge in positive_answers: 
     main() 
    elif challenge in negative_answers: 
     print "Goodbye!" 
     raw_input("Press any key to exit") 
    else: 
     print "Sorry I didn't get that, try again" 
     rematch()  


def table_score(results, pc_pick, player_pick): 
    pc_score = 0 
    player_score = 0 
    if results(pc_pick, player_pick) == "Victory": 
     Score.player_score += 1 
    elif results(pc_pick,player_pick) == "Loss": 
     Score.pc_score += 1 
    else: 
     Score.player_score += 0 
     Score.pc_score += 0 
    print "Player:" + str(player_score) 
    print "Pc:" + str(pc_score) 


class Score: 
    def __init__(self): 
     self.pc_score = 0 
     self.player_score = 0 
Score = Score() 

main() 

我唯一的問題是,輸出給我3次「這是一個平局」輸出(如果是平局),兩次「我贏」「你贏」輸出......我還不知道爲什麼。

0

你需要的東西是這樣的:

class Score(object): 
    pc_score = 0 
    player_score = 0 

    def update_score(self, pc_score, player_score): 
     self.pc_score += pc_score 
     self.player_score += player_score 
+0

是的,我知道,但問題有點複雜。我在哪裏以及如何稱此課程?在主要功能的原因,我得到了複賽功能,將從頭開始遊戲。那麼我如何將主要功能的分數與這個類連接起來呢,我真的很想知道。儘管謝謝你的回覆 – user2390606 2014-10-27 12:42:40

相關問題