如何连续提示用户输入? [英] How to continuously prompt for user input?

查看:79
本文介绍了如何连续提示用户输入?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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])

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

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

我只能使用内置函数.

推荐答案

我会使用while循环来做到这一点.像这样:

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)

这篇关于如何连续提示用户输入?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆