2016-02-12 54 views
0

所以我最初編寫這段代碼是爲了弄清楚一個人是誰,得到了一些if/elif/else語句。問題是我無法弄清楚如何讓else語句回到原來的問題,因爲這個問題只是由一個變量= raw_input語句表示的。這個想法是爲了讓我的很多朋友錯綜複雜。在用else語句修復原始問題時,我搞亂了添加部分以確定答案。我如何修復它實際計數,因爲它走了?正如你所看到的,我對編碼非常陌生。對於基本問題,我很抱歉,因爲我確信答案非常簡單。我感謝任何幫助。Python通過if else和def添加

from sys import exit 


Tom = 0 
Jon = 0 
Chris = 0 


def question_a(): 
    q1 = raw_input("Hello there. I am going to try to guess who you are. Would you like to play?") 

    if q1 == "yes": 
     question_b() 
    elif q1 == "no": 
     print "Well f**k you too then" 
    else: 
     print "You should follow the rules." 
    question_a() 

def question_b(): 
    print "Do you have any hair?" 

    q1 = raw_input("> ") 

    if q1== "no": 
     print "you're Karl" 
     exit(0) 


    elif q1 == "yes": 
     Tom == Tom + 1 
     Chris == Chris + 1 
     Jon == Jon + 1 
     question_c() 
    else: 
     print "You should follow the rules." 
    question_b() 

def question_c(): 
    print "Do you enjoy working on cars?" 

    q1 = raw_input("> ") 

    if q1 == "yes": 
     Chris == Chris + 1 
     Jon == Jon + 1 
     question_d() 

    elif q1 == "no": 
     Tom == Tom + 1 
     question_d() 
    else: 
     print "you should follow the rules." 
    question_c() 

def question_d(): 
    print "Do you own a husky?" 

    q1 = raw_input("> ") 

    if q1 == "no": 
     Tom == Tom + 1 
     Chris == Chris + 1  
    elif q1 == "yes": 
     Jon == Jon + 1 
    else: 
     print "Hey you, follow the rules." 
    question_d() 

    # python guess_who2.py 

    for Tom > Jon and Tom > Chris: 
     print "You're Tom" 
    for Jon > Chris and Jon > Tom: 
     print "You're Jon" 
    for Chris > Tom and Chris > Jon: 
     print "You're Chris" 
question_a() 
+2

你已經搞砸了不止於此。由於語法錯誤,此程序無法運行。我推薦增量編程:一次寫幾行。在寫更多內容之前讓那些人工作。繼續執行,直到遇到無法解決的問題。 – Prune

+2

您已使用** == **而不是** = **進行分配。你的計數器需要是全局變量,而不是本地的。你正在進行遞歸調用(一個自己調用的函數),沒有特別的原因。你真的需要修復這些東西,然後才能擔心你的計數邏輯。最後,您使用關鍵字**作爲**來推動決策;如果**陳述,那些應該更**。 – Prune

回答

0

做這樣的:

from sys import exit 


def question_a(tom=0, jon=0, chris=0): 
    q = '' 
    while q not in ['yes', 'no']: 
     q = raw_input("Hello there. I am going to try to guess who you are. Would you like to play?") 

     if q == "yes": 
      question_b(tom, jon, chris) 
     elif q == "no": 
      print "Well f**k you too then" 
     else: 
      print "You should follow the rules." 


def question_b(tom=0, jon=0, chris=0): 
    print "Do you have any hair?" 

    q = '' 
    while q not in ['yes', 'no']: 
     q = raw_input("> ") 

     if q == "no": 
      print "you're Karl" 
      exit(0) 
     elif q == "yes": 
      tom = tom + 1 
      chris = chris + 1 
      jon = jon + 1 
      question_c(tom, jon, chris) 
     else: 
      print "You should follow the rules." 


def question_c(tom=0, jon=0, chris=0): 
    print "Do you enjoy working on cars?" 

    q = '' 
    while q not in ['yes', 'no']: 
     q = raw_input("> ") 

     if q == "yes": 
      chris = chris + 1 
      jon = jon + 1 
      question_d(tom, jon, chris) 

     elif q == "no": 
      tom = tom + 1 
      question_d(tom, jon, chris) 
     else: 
      print "you should follow the rules." 


def question_d(tom=0, jon=0, chris=0): 
    print "Do you own a husky?" 

    q = '' 
    while q not in ['yes', 'no']: 
     q = raw_input("> ") 

     if q == "no": 
      tom = tom + 1 
      chris = chris + 1 
     elif q == "yes": 
      jon = jon + 1 
     else: 
      print "Hey you, follow the rules." 

    # python guess_who2.py 

    if tom > jon and tom > chris: 
     print "You're tom" 
    if jon > chris and jon > tom: 
     print "You're jon" 
    if chris > tom and chris > jon: 
     print "You're chris" 

    if raw_input('Play again? (yes or no)') == 'yes': 
     question_a() 
    else: 
     print 'bye.' 

if __name__ == '__main__': 
    question_a() 

儘量先閱讀一些Python基本語法,這樣就可以輕鬆地處理if語句。

+0

也謝謝你!我會檢查你的代碼和melalonso之間的差異,並嘗試理解它們。 –

1

我希望你正在享受編程的世界:) 我已經對你的代碼做了一些改變,以便使它工作,我會評論它們。

from sys import exit 


Tom = 0 
Jon = 0 
Chris = 0 


def question_a(): 
    q1 = raw_input("Hello there. I am going to try to guess who you are. Would you like to play?") 

    if q1 == "yes": 
     question_b() 
    elif q1 == "no": 
     print "Well f**k you too then" 
    else: 
     print "You should follow the rules." 
     question_a() 

def question_b(): 
    global Tom, Chris, Jon 
    print "Do you have any hair?" 

    q1 = raw_input("> ") 

    if q1== "no": 
     print "you're Karl" 
     exit(0) 


    elif q1 == "yes": 
     Tom = Tom + 1 
     Chris = Chris + 1 
     Jon = Jon + 1 
     question_c() 
    else: 
     print "You should follow the rules." 
     question_b() 

def question_c(): 
    global Tom, Chris, Jon 
    print "Do you enjoy working on cars?" 

    q1 = raw_input("> ") 

    if q1 == "yes": 
     Chris = Chris + 1 
     Jon = Jon + 1 
     question_d() 

    elif q1 == "no": 
     Tom = Tom + 1 
     question_d() 
    else: 
     print "you should follow the rules." 
     question_c() 

def question_d(): 
    global Tom, Chris, Jon 
    print "Do you own a husky?" 

    q1 = raw_input("> ") 

    if q1 == "no": 
     Tom = Tom + 1 
     Chris = Chris + 1 
    elif q1 == "yes": 
     Jon = Jon + 1 
    else: 
     print "Hey you, follow the rules." 
     question_d() 

# python guess_who2.py 
question_a() 
if Tom > Jon and Tom > Chris: 
    print "You're Tom" 
elif Jon > Chris and Jon > Tom: 
    print "You're Jon" 
elif Chris > Tom and Chris > Jon: 
    print "You're Chris" 

在每一個(question_a(),question_b()..)調用該函數應在其他語句中,以問縮進功能再次以防萬一答案是不既不是

else: 
    print "You should follow the rules." 
    question_a() 

在要修改你需要指出的Python湯姆,克里斯或喬恩變量的函數,通過使用線

global Tom, Chris, Jon 

如果你想增加一個變量的點,你像這樣做:

Tom = Tom + 1 
Chris = Chris + 1 
Jon = Jon + 1 

如果你喜歡寫東西

Tom == Tom + 1 
因爲

你正在做一個布爾表達式,會給你真或假。

最後

question_a() 
if Tom > Jon and Tom > Chris: 
    print "You're Tom" 
elif Jon > Chris and Jon > Tom: 
    print "You're Jon" 
elif Chris > Tom and Chris > Jon: 
    print "You're Chris" 

我們通過調用question_a()函數(如你已經知道)開始執行,並檢查使用如果表達的點。 對於語句用於製作循環(重複一段代碼X的次數)

+0

謝謝!今天晚些時候我應該能夠修改代碼並研究變化,以便將來更好地理解它們。我很感激。 –

+0

真棒,有運氣和享受! – melalonso