【问题标题】:Make a function to call while the program is running在程序运行时创建一个要调用的函数
【发布时间】:2016-05-11 21:18:47
【问题描述】:

例如,我有一个可以添加字母和从列表中删除字母的程序。代码如下:

my_list = ['a', 'b', 'c', 'd', 'e', 'f']

do = input("Press 'a' to append and 'r' to remove: ")

if do == 'a':
    letter = input("Enter a letter to append: ")
    my_list.append(letter)
    print (my_list)

elif do == 'r':
    letter = input("Enter a letter to append: ")
    my_list.remove(letter)
    print (my_list)

else:
    print ("Something gone wrong...")

要从列表中删除一封信,我必须告诉程序我要做什么,然后它会要求我删除一封信。有没有办法像这样调用我自己的函数(只是为了让程序更容易使用):

def removing(letter):
    my_list.remove(letter)
    print (my_list)

要像这样在控制台中使用该功能:

What are you going to do? removing(b)

【问题讨论】:

  • 如果您使用的是input() 而不是raw_input(),无论如何都应该这样做。来自input()input([prompt]) -> value Equivalent to eval(raw_input(prompt)) 的文档
  • 您希望将自己的代码注入正在运行的程序中?是的,它可以完成,但出于安全原因,很少有程序会支持它。最好通过管道之类的方式传递数据。
  • @zondo:您假设 OP 使用的是 Python 2,但情况可能并非如此
  • @cdarke 啊,现在我明白为什么我到处都能看到这个了。在那种情况下,@Andrew,你可以这样做eval(input("Enter a letter to append"))

标签: python


【解决方案1】:

这里有一个经过重新调整的建议。它要求用户输入

添加一些东西

删除一些东西

my_list = ['a', 'b', 'c', 'd', 'e', 'f']

choices = {'remove': my_list.remove,
           'append': my_list.append}

print my_list
while True:
    try:
        choice, item = raw_input('append <x> OR remove <x>\n').split()
        choices[choice](item)
    except (KeyError, ValueError):
        print('something went wrong...')
    print my_list

演示:

['a', 'b', 'c', 'd', 'e', 'f']
append <x> OR remove <x>
append z
['a', 'b', 'c', 'd', 'e', 'f', 'z']
append <x> OR remove <x>
remove d
['a', 'b', 'c', 'e', 'f', 'z']
append <x> OR remove <x>
remove y
something went wrong...
['a', 'b', 'c', 'e', 'f', 'z']

这应该会给你一个想法/让你开始。字典很容易扩展。

【讨论】:

  • 耶!这正是我所需要的!谢谢你。附:与 2.7 完美搭配,但当我使用 eval(input('append &lt;x&gt; OR remove &lt;x&gt;\n')).split() 时,在 3.5 上会出现“解析时出现意外 eof”错误
  • @Andrew 在 Python3 中,将所有print foo 语句更改为print(foo) 函数调用并使用input(不带eval)而不是raw_input。永远不要使用eval(input())
【解决方案2】:

为了好玩,您可以扩展@timgeb 的答案以同时接受多个参数。

my_list = ['a', 'b', 'c', 'd', 'e', 'f']

choices = {'remove': my_list.remove,
           'append': my_list.append}

def call_choice(name, *args):
    for arg in args: 
        choices[name](arg)

print my_list
while True:
    try:
        input_string = raw_input('append <x> OR remove <x>\n')
        call_choice(*input_string.split())
    except (KeyError, ValueError):
        print('something went wrong...')
    print my_list

演示:

['a', 'b', 'c', 'd', 'e', 'f']
append <x> OR remove <x>
append a b c d e f g
['a', 'b', 'c', 'd', 'e', 'f', 'a', 'b', 'c', 'd', 'e', 'f', 'g']
append <x> OR remove <x>
remove a b c
['d', 'e', 'f', 'a', 'b', 'c', 'd', 'e', 'f', 'g']
append <x> OR remove <x>
remove a
['d', 'e', 'f', 'b', 'c', 'd', 'e', 'f', 'g']
append <x> OR remove <x>
remove d
['e', 'f', 'b', 'c', 'd', 'e', 'f', 'g']
append <x> OR remove <x>
remove e f b c d e f g
[]

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多