How to continuously prompt for user input?

前端 未结 5 1775
离开以前
离开以前 2020-11-29 14:02

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

相关标签:
5条回答
  • 2020-11-29 14:28

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

    0 讨论(0)
  • You've done most of the work, you just need this:

    while True:
        print interact()
    
    0 讨论(0)
  • 2020-11-29 14:37

    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)
    
    0 讨论(0)
  • 2020-11-29 14:41

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

    while True:
       some_method()
    
    0 讨论(0)
  • 2020-11-29 14:53

    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

    0 讨论(0)
提交回复
热议问题