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

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

问题描述

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

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.

这是函数的实际代码:

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天全站免登陆