不断提示用户输入Python [英] Continually prompting user for input in Python

查看:303
本文介绍了不断提示用户输入Python的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目标:*编写一个Python程序,反复提示输入正数,直到数字总和大于179.在解决方案中至少使用三个模块/功能. *输入的最大数字不能超过42. *当数字总和超过179时,打印数字总和,输入的最大数字和输入的最小数字.

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.

我只需要一些指导,特别是针对"input_numbers"模块的指导.比为每个数字创建变量,必须有一种更简单的方法来执行此操作.代码不完整.我什至还没有开始其他两个模块.提前致谢.

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()

推荐答案

您可以通过封装程序的每个步骤来消除所有三个功能要求.不是让您的循环进入函数内部,而是让main控制循环,并通过将数据传入和传出函数调用来控制流程.

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.

让我们稍微修改一下input_numbers()函数.

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

因此,与其让input_numbers控制循环以及输入处理和验证,不如它的名字所暗示的那样:它要求输入,对其进行验证,然后,如果输入正确,则返回值给调用者,但如果不好,它会写一条消息,然后再次调用它自己,用户可以输入良好的输入.

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

我们将列表的第一个元素设置为变量highest,然后对照该初始值检查列表中的所有其他元素.如果找到更大的元素,则将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

最后,我们进入主循环.在这里,我们声明一个空列表number_list来存储所有数字.然后,将其总和用作循环条件.

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())

在循环的主体中,我们调用get_input_number()并将其结果附加到我们创建的列表中.一旦列表中的数字总和超过179,while循环将退出,我们最终可以向用户显示结果.

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)

在这里,我们可以制作我们的get_greatest_numberget_smallest_number,并给他们数字列表作为参数.他们将遍历列表,然后将适当的值返回到print语句.

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.

这篇关于不断提示用户输入Python的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆