2015-06-27 65 views
2

我該如何完成這項工作?我正在嘗試設置一個全局函數,以便稍後可以將其稱爲原始輸入,而不是在需要時調用原始輸入。設置函數調用python中的raw_input

我認爲我的精髓在於我需要的程度,但我很難理解如何格式化,或者甚至可能。

預先感謝您。

def choice_1(): 
     choice_1 == raw_input("> ") 
    def choice_1a():  
     choice_1a = raw_input("> ") 
    def choice_1b():  
     choice_1b = raw_input("> ") 

編輯:我不認爲我是在我的問題的目的很清楚。這裏是我正在處理的代碼的更新,也許這將清除一切。

print "You've arrived at your desk" 


def choice_1(one): 
    choice_1a = raw_input("< ") 
def choice_1a():  
    choice_1a = raw_input("> ") 
def choice_1b():  
    choice_1b = raw_input("> ") 

#Choice_1 
print "What do you want to do?" 
print "We can \n1. Read\n2. Draw\n3. Work on homework" 
print choice_1 
#choice 1 branch 1 
if choice_1 == "1": 
    print "What book should we read today?" 
    print "We can read\n1. Tom Sawyer\n2. Quantum Physics \n3. Ray Bradbury" 
    print choice_1a 
    if choice_1a == "1": 
     print "Great choice!" 
    if choice_1a == "2": 
     print "Heavy stuff there." 
    if choice_1a == "3": 
     print "Entertaining author, that one there!" 
    else: 
     print "Let's go to the library, maybe they'll have that one." 
#choice 1 branch 2 
if choice_1 == "2": 
    print "What would you like to draw?" 
    print "We can draw a\n1. Tiger\n2. Fish\n3. Bear " 
    print choice_1b 
    if choice_1b == "1": 
     print "You drew a Tiger!" 
    if choice_1b == "2": 
     print "You drew a Fish!" 
    if choice_1b == "3": 
     print "You drew a Bear!" 
    else: 
     print "Time for some improvisation." 

#choice 1 branch 3 
if choice_1 == "3": 
    print "" 

這是否清楚了一些混淆?

+1

看起來你所缺少的是一些'return'語句。 – SethMMorton

回答

1

這會做你想做的。作爲@SethMMorton注意到,您曾經在return

def choice(): 
    return raw_input('> ‘) 

順便說錯過了,這不是一個很好的事情,因爲,給別人讀你的代碼,它不會立即清楚什麼choice是這樣做。

我這樣做:

print 'Your name is :' + choice() 

和它的工作如預期。

編輯:你應該讓你如果這樣的語句:

if choice() == "1"

+0

這不起作用,不會返回錯誤代碼,但它會在函數中調用該函數,如果我說得對。 – Ppaisley

+0

@Ppaisley嗯,你究竟是怎麼打電話給這個的? – Rishav

+0

我需要先能夠調用raw_input,但是要感謝您的建設性意見 – Ppaisley

0
def multiplier(x, y): 
    ans = x * y 
    print ans 

這將是一個功能的使用,當準備使用它,你會 這樣稱呼它

multiplier(5, 10) 
     50 
+0

我試圖能夠調用「choice_1」並讓它在代碼中要求原始輸入,所以我可以在開始時「設置」我的函數代碼,而不是我想要的每個點。 編輯:也許我沒有正確把握def的目的?? – Ppaisley

+0

我看起來好吧給我10分鐘,讓我看看我能想出什麼 –

+0

呃這需要兩行輸入,並返回第二個。恐怕這不正確。 – Rishav