Continually prompting user for input in Python

前端 未结 7 2022
余生分开走
余生分开走 2021-01-16 16:22

Objective: * Write a python program that repeatedly prompts for input of a positive number until the sum of the numbers is greater than 179. Use at least three modules/fun

相关标签:
7条回答
  • 2021-01-16 17:20

    You can repeatedly poll the number in a loop, and add the inputs into a list, e.g.

    def input_numbers():
        user_inputs = []  # a list of all user's inputs
        while True:
            num = raw_input("Enter a positive integer no greater than 42 ")
            try:
                num = int(num)  # convert input string to integer
                if num <= 0:
                    print "That is not a positive integer.  Try again "
                elif num > 42:
                    print "The number cannot exceed 42.  Try again "
                user_inputs.append(num)
            except ValueError:  # conversion to integer may fail (e.g. int('string') ?)
                print 'Not a Number:', num
    
            if some_condition_regarding(user_inputs):
                break  # eventually break out of infinite while loop
    
    0 讨论(0)
提交回复
热议问题