简体   繁体   English

Python2.7-:将输入的用户输入值存储在列表中

[英]Python2.7-: Storing user input values input in a list

I am new to python and came around a scenario explained below-: 我是python的新手,遇到了以下情况的解释:

This is one from the .pdf I am referring to learn. 这是我要学习的.pdf文件中的一个。 Would be great if anyone could guide or share some other resources. 如果有人可以指导或共享其他资源,那将是很棒的。

A program which repeatedly reads numbers until the user enters “done”. 重复读取数字直到用户输入“完成”的程序。 Once “done” is entered, print out the total, count, and average of the numbers. 输入“完成”后,打印出总数,总数和平均值。 If the user enters anything other than a number, detect their mistake using try and except and print an error message and skip to the next number. 如果用户输入的不是数字,请使用try和except检测到他们的错误,并显示错误消息并跳至下一个数字。

Enter a number: 4
Enter a number: 5
Enter a number: bad data
Invalid input
Enter a number: 7
Enter a number: done

16 3 5.333333333333333*

I am unable to store the values into list. 我无法将值存储到列表中。

Tried going with this logic-: 尝试遵循以下逻辑:

while True:
    line = input('Enter Number-: ')
    if type(line) == int():
        continue
    if line == 'done':
        break
    print(line)
print('Done!')

Just need to know how to store into lists without using spaces or commas, The user should be able to enter the value as shown in example above and those should get stored in a list. 只是需要知道如何在不使用空格或逗号的情况下存储到列表中,用户应该能够输入如上例所示的值,并且这些值应该存储在列表中。

Thanks in advance. 提前致谢。

In Python 2.7, input will evalulate any entry and will fail if the input is not a correct Python type to begin with. 在Python 2.7中,输入将评估任何条目,并且如果输入不是正确的Python类型,输入将失败。 It's better to use raw_input here as any entry will be considered a string. 最好在此处使用raw_input ,因为任何条目都将被视为字符串。 If you move to Python 3, raw_input was removed and input acts how raw_input did. 如果您使用Python 3, raw_input删除raw_input ,而input raw_input一样执行操作。 So your example expects you to give it '45' or 'done' instead of 45 or done . 因此,您的示例希望您将其设置为'45''done'而不是45或“ done

But the reason you're unable to store any values into a list is because you're not adding them to a list in the first place. 但是,您无法将任何值存储到列表中的原因是因为您没有首先将它们添加到列表中。 But since we've also switched to raw_input, we don't know if the entry is a valid number or not. 但是,由于我们也已切换到raw_input,因此我们不知道该条目是否为有效数字。 So we need to try to convert it to a number and if it isn't one, then check to see if it's the keyword telling the code to stop. 因此,我们需要尝试将其转换为数字,如果不是,请检查是否是指示代码停止的关键字。

values = []  # make an empty list
while True:
    line = raw_input('Enter Number-: ')  # all entries here are considered strings
    try:
        num = int(line)  # convert to an integer
        values.append(num)  # add to list
        continue  # return to input query
    except:  # int(line) threw an error, so not a valid number input
        if line == 'done':  # check if should stop
            break  # get out of loop
        else:  # anything else
            print 'bad data, invalid input'
            continue  # return to input query

print 'Done!\n'     
print 'total:', sum(values)
print 'count:', len(values)
print 'average:', sum(values) / float(len(values))

If you're entering more than just integers, you may wish to change num = int(line) to num = float(line) to handle decimal inputs, as int only accepts integers. 如果您输入的不仅仅是整数,您可能希望将num = int(line)更改为num = float(line)以处理十进制输入,因为int仅接受整数。

Enter Number-: 4
Enter Number-: 5
Enter Number-: 
bad data, invalid input
Enter Number-: 7
Enter Number-: done
Done!

total: 16
count: 3
average: 5.33333333333

The Tutorial may also be helpful in learning Python. 教程可能也有助于学习Python。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM