Continually prompting user for input in Python

前端 未结 7 2024
余生分开走
余生分开走 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:10

    def get_int(prompt=''):
        while True:
            try:
                return int(raw_input(prompt))
            except ValueError:
                pass
    
    def get_values():
        values = []
        total  = 0
        while total <= 179:
            val = get_int('Enter a positive integer <= 42: ')
            if 0 <= val <= 42:
                values.append(val)
                total += val
        return values
    
    def print_info(values):
        print 'Sum is {}'.format(sum(values))
        print 'Largest value is {}'.format(max(values))
        print 'Smallest value is {}'.format(min(values))
    
    def main():
        values = get_values()
        print_info(values)
    
    if __name__=="__main__":
        main()
    

提交回复
热议问题