2015-06-27 23 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