2015-07-03 55 views
2

我正在嘗試編寫一個小遊戲,它會要求做出選擇,然後根據所做的選擇調用不同的函數。在我打開程序的時候,我會看到下面的主要功能。獲取if語句讀取raw_input選項

我的問題是,當程序運行時,我把其中一個選項,程序將只會通過第一個if-statement

因此,如果我將2school納入raw_input或任何其他選項,我的程序仍然會調用work函數。

def bedroom(): 

print "Good morning! Today is full of possibilities. What do you want to do?" 
print """ 
1. Go to work 
2. Go to school 
3. Go on an adventure 
4. Relax with a friend 
""" 

choice = raw_input("| ") 

if "1" or "work" in choice: 
    print "Great! Making money and being diligent is a brilliant thing to do with life!" 
    work() 
elif "2" or "school" in choice: 
    print "Wonderful! You can never learn enough." 
    school() 
elif "3" or "adventure" in choice: 
    print "Oh, yay! Adventures make life exciting!" 
    adventure() 
elif "4" or "relax" or "friend" in choice: 
    print "It's importanat to not exhaust yourself. Relaxing will help you refocus." 
    relax() 
else: 
    print "Stop being creative! That wasn't an option." 
    bedroom() 

任何想法,爲什麼它不考慮if-else語句的休息嗎?

回答

1

這是因爲"1"評估爲True。您的代碼其實就是做

if ("1" or ("work" in choice)): 
    work() 

你可能想要做

if "1" in choice or "work" in choice: 
    work() 
+0

精彩!非常感謝! –