设置函数以在python

时间:2015-06-27 03:21:27

标签: python function 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 ""

这是否清楚一些混乱?

2 个答案:

答案 0 :(得分:1)

这将做你想要的。正如@SethMMorton所指出的那样,你错过了return

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

顺便说一句,这不是一件好事,因为对于阅读代码的人来说,choice正在做什么并不会立即清楚。

我这样做了:

print 'Your name is :' + choice()

它按预期工作。

编辑:您应该制作if语句:

if choice() == "1"

答案 1 :(得分:0)

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

这将是一个功能的用途,当你准备好使用它时   像这样称呼它

 multiplier(5, 10)
      50