python - 设置函数以在python中调用raw_input

标签 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 ""

这是否消除了一些困惑?

最佳答案

这会做你想做的。正如@SethMMorton 指出的那样,您错过了一个return

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

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

我这样做了:

print 'Your name is :' + choice()

它按预期工作。

编辑:您应该这样制作您的 if 语句:

if choice() == "1":

关于python - 设置函数以在python中调用raw_input,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31084816/

相关文章:

python - 无法在 Cron 中运行 Python 脚本

python - 使用 OpenCV 在 Python 中计算图像中的黑色像素数

javascript - 简单的查找和更新记录功能......我无法工作

python - 输入和用户输入验证

python - 使用 NOOP 命令检查 FTP 连接是否有效

c++ - 有条件地从 C++ 调用 Lua 函数

python - 一次调用两个定义的函数时出现矛盾的输出

Python raw_input ("") 错误

python - 键入文本而不显示文本

python - Django-filter 1.1.0 中的多个模型