Continually prompting user for input in Python

2019-02-21 01:36发布

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/functions in your solution. * The largest number entered cannot exceed 42. * When the sum of the numbers exceeds 179, print the sum of the numbers, the largest number entered and smallest number entered.

I just need some guidance, specifically for the "input_numbers" module. There must be an easier way to do this than to make a variable for each number. The code is not complete. I haven't even started on the two other modules yet. Thanks in advance.

def input_numbers():
    while True:
        num1 = raw_input("Enter a positive integer no greater than 42 ")
        if num1 <= 0:
            print "That is not a positive integer.  Try again "
        elif num1 > 42:
            print "The number cannot exceed 42.  Try again "

        num2 = raw_input("Enter another positive integer ")
            if num2 <= 0:
                print "That is not a positive integer.  Try again "
        elif num2 > 42:
            print "The number cannot exceed 42.  Try again " 

        num3 = raw_input("Enter another positive integer ")
        if num3 <= 0:
            print "That is not a positive integer.  Try again "
        elif num3 > 42:
            print "The number cannot exceed 42.  Try again "

        num4 = raw_input("Enter another positive integer ")
        if num4 <= 0:
            print "That is not a positive integer.  Try again "
        elif num4 > 42:
            print "The number cannot exceed 42.  Try again "

        num5 = raw_input("Enter another positive integer ")
        if num5 <= 0:
            print "That is not a positive integer.  Try again "
        elif num5 > 42:
            print "The number cannot exceed 42.  Try again "
        elif sum(num1, num2, num3, num4, num5) > 179:
            print_numbers()

add_numbers()

def add_numbers():
print_numbers()

def print_numbers():

input_numbers()

标签: python input
7条回答
Animai°情兽
2楼-- · 2019-02-21 02:23

here some well-known tricks:

    def min_and_max(your_num_list)
        min=your_num_list[0]
        max=your_num_list[0]
        for num in your_num_list:
            if num < min:
                min=num
            if max > num
                max=num
        return min,max
查看更多
登录 后发表回答