如何持续提示用户输入?

发布于 2024-10-30 09:36:55 字数 679 浏览 0 评论 0原文

我正在编写一个函数,提示输入,然后根据输入返回不同的结果,然后再次要求输入。我已经让它返回正确的值,但我不确定如何让它再次提示输入。

下面是该函数的实际代码:

def interact():
    command = raw_input('Command:')
    command = command.split(' ')
    if command[0] == 'i':
        bike_name =  command[1] + ' ' + command[2]
        return get_product_id(products, bike_name)
    if command [0] == 'n':
        return get_product_name(products, command[1])
    if command[0] == 'c':
        return compute_cost(products, part, command[1])
    if command[0] == 'p':
        return get_parts(products, command[1])

在包含 return 的每一行中,它只是调用先前定义的函数。 productspart 是之前定义的字典。

我只能使用内置函数。

I'm writing a function that prompts for input and then returns different results based on the input and then asks for input again. I've got it returning the correct values, but I'm not sure how to make it prompt for input again.

Here's the actual code of the function:

def interact():
    command = raw_input('Command:')
    command = command.split(' ')
    if command[0] == 'i':
        bike_name =  command[1] + ' ' + command[2]
        return get_product_id(products, bike_name)
    if command [0] == 'n':
        return get_product_name(products, command[1])
    if command[0] == 'c':
        return compute_cost(products, part, command[1])
    if command[0] == 'p':
        return get_parts(products, command[1])

In each line with return in it, it is simply calling up a previously defined function. The products and part are dictionaries, defined previously.

I can only use the builtin functions.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

大姐,你呐 2024-11-06 09:36:55

我会用 while 循环来做。像这样:

while True:
    com = raw_input('Command:').split()
    if len(com) == 0:
        break
    elif com[0] == 'i':
        bike_name =  command[1] + ' ' + command[2]
        return get_product_id(products, bike_name)

I would do it with a while loop. Like This:

while True:
    com = raw_input('Command:').split()
    if len(com) == 0:
        break
    elif com[0] == 'i':
        bike_name =  command[1] + ' ' + command[2]
        return get_product_id(products, bike_name)
呆° 2024-11-06 09:36:55

你已经完成了大部分工作,你只需要这个:

while True:
    print interact()

You've done most of the work, you just need this:

while True:
    print interact()
暗喜 2024-11-06 09:36:55

一种方法是将其放入 while 循环中,然后检查退出输入是否会中断。

One way is to put it in a while loop, and then also check for an exit input to break out.

千と千尋 2024-11-06 09:36:55

无需花费如此多的精力来编写自己的命令行解释器。
看看这个: http://docs.python.org/2/library/cmd.html

There is no need to take so much pain and write your own command line interpreter.
Look at this: http://docs.python.org/2/library/cmd.html

梦幻的味道 2024-11-06 09:36:55

在(无限)循环内调用该方法:

while True:
   some_method()

Call the method inside an (end-less) loop:

while True:
   some_method()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文