2017-05-31 69 views
0

我正在開發一款基本遊戲。我想知道如何讓代碼進入代碼/ if語句中的特定位置,而不是從一開始就開始。我如何讓代碼跳到if語句中的某個點

例如,在我的代碼在第19行:kithcen()我不想將它重定向到if語句的開頭;再次描述廚房,然後要求輸入:choice02 = raw_input("What do you do?"),而是我希望它直接跳到輸入部分。

def kitchen(): 
    print "You see a dusty old kitchen, nobody has probably used it for quite a while." 
    print "There are 3 different cupboards(0, 1, 2) or a letter(3) placed on the counter." 
    choice02 = raw_input("What do you do? >") 

    if choice02 == "0" or choice02 == "first cupboard" or choice02 == "check first cupboard" or choice02 == "open first cupboard": 
     print "You see a dusty empty bottles of milks, with spiderwebs at the corners of the cupboard. Nothing of Interest here." 
     raw_input(">") 
     kitchen() 

    elif choice02 == "1" or choice02 == "second cupbaord" or choice02 == "check second cupboard" or choice02 == "open second cupboard": 
     print "You see some packs of likely expired cereal and a key(0)." 
     choice_02_A = raw_input("What do you do? >") 

    #----------------------------------------------------------------------------------------------------------# 
     if choice02_A == "pick key" or choice02_A == "key" or choice02_A == "0" or choice02_A == "get key": 
      print "You picked the key. This probably unlocks some door." 
      raw_input("> ") 
      kitchen() 

     elif choice02_A == "close cupboard" or choice02_A == "kitchen" or choice02_A == "go back to kitchen": 
      kitchen() 

     else: 
      print "Not a valid option." 
    #-----------------------------------------------------------------------------------------------------------# 

    elif choice02 == "2" or choice02 == "third cupboard" or choice02 == "check third cupboard" or choice02 == "open third cupboard": 
     print "You see an empty or dusty cupboard. Nothing of interest here." 
     raw_input("> ") 
     kitchen() 

    elif choice02 == "3" or choice02 == "check letter" or choice02 == "letter" or choice02 == "read letter": 
     print """ 
     You read the letter: 
     \n"Dear Shawn............\n" 
     It makes no sense to you. 
     """ 


    elif choice02 == "go back" or choice02 == "entrance hall" or choice02 == "go to entrance hall": 
     entrance_hall() 

else: 
    "Not a valid Option." 
+0

正確縮進你的代碼 –

+0

此外,你一直在你的問題中說「循環」。什麼循環? –

+0

對不起,我的壞,我是新的。一個誤解,無論如何我做了一個編輯。 複製時,縮進會搞砸。 –

回答

1

我沒有看到循環,但如果你只想要打印語句,當你調用來自外部的功能,你可以有一個可選的布爾是這樣的:

def kitchen(already_there=False): 
    if not already_there: 
     print('...') 
    choice02 = raw_input("What do you do? >") 

    # ... 

    elif # ... 
     # ... 
     kitchen(already_there=True) 
+0

這是特別好的,因爲它向後兼容您已經編寫的任何代碼。 –

0

你可能要考慮重構你的代碼和使用字典來避免if-else語句太長。

請參閱https://stackoverflow.com/a/27365077/2884613

print "There are 3 different cupboards(0, 1, 2) or a letter(3) placed on the counter." 

alias = { 
    '0': '0', 
    'first cupboard': '0', 
    'check first cupboard': '0', 
    'open first cupboard': '0', 
    '1': '1', 
    'second cupboard': '1', 
    'check second cupboard':'1', 
    ... 
} 

dic = { 
    "0":"You see a dusty empty bottles of milks, with spiderwebs at the corners of the cupboard. Nothing of Interest here.", \ 
    "1":"You see some packs of likely expired cereal and a key(0).", \ 
    "2":"You see an empty or dusty cupboard. Nothing of interest here." 
    ... 
} 

while(True): 
    choice02 = raw_input("What do you do? >") 
    if choice02 in alias: 
    print dic[alias[choice02]] 
    break 
    else: 
    print "Not a valid option" 
+0

好的,謝謝!這絕對是更清潔。 雖然我怎麼能指定多個鍵到一個值/字符串 –

+0

嗯,一種可能性是使用兩個字典。一個存儲別名密鑰關聯,另一個存儲您的實際數據。我會盡快更新我的答案。 – raychz