Continually prompting user for input in Python

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

    You can knock out all three function requirements by encapsulating each step of your program. Rather than having your loop inside of a function, we'll let main control the loop, and we'll control the flow by passing data into and out of function calls.

    Let's rework your input_numbers() function a bit.

    def get_input_number():
        num = int(raw_input("Enter a positive integer no greater than 42 "))
        if num <= 0 or num > 42:
            print "Invalid input.  Try again "
            get_input_number()
        else: 
            return num
    

    So, instead of having input_numbers control the loop as well as the input handling and validation, we have it do just what its name implies: It asks for input, validates it, and then, if it's good, it returns the value to the caller, but if it's bad, it writes a message, and calls itself again to the user can enter good input.

    The next function we'll set up is straight from your list of requirements. From all of the numbers that the user enters, we need to find the biggest one. From the language alone, we can determine that we're looking through a set of numbers, and thus, this is a good place to break out a list. Assuming we store all of the users input in a list, we can then pass that list to a function and perform operations on it, like so.

    def get_greatest_number(input_list):
        highest = input_list[0]
        for i in input_list:
            if i > highest:
                highest = i
        return highest
    

    We set the first element of the list to a variable highest and then check all other elements in the list against that initial value. If we find one that's bigger, we then reassign the highest variable to the element that was bigger. Once we do this for each element in the list, the number inside of highest will now be, just that, the highest number, and so, we'll return it to the main program.

    Similarly, we can do the same for finding the smallest.

    def get_smallest_number(input_list):
        smallest = input_list[0]
        for i in input_list:
            if i < smallest:
                smallest = i
        return smallest
    

    Finally, we get to our main loop. Here we declare an empty list, number_list to store all the numbers in. And we use the sum of that as our loop condition.

    if __name__ == '__main__':
        number_list = []
        while sum(number_list) < 179:
            number_list.append(get_input_number())
    

    In the body of the loop, we call our get_input_number() and append its result to the list we made. Once the sum of the numbers in the list exceed 179, the while loop will exit and we can finally show the user the results.

        print 
        print '-------------------------'
        print 'total of numbers entered: %d' % sum(number_list)
        print 'greatest number entered: %d' % get_greatest_number(number_list)
        print 'smallest number entered: %d' % get_smallest_number(number_list)
    

    Here we can the get_greatest_number and get_smallest_number we made, and we give them the list of numbers as an argument. They'll loop though the lists, and then return the appropriate values to the print statements.

提交回复
热议问题