简体   繁体   English

如何打印数字及其平均值的列表?

[英]How to print a list of numbers and their average?

For my assignment, I have to create a program that lets the user enter several numbers. 对于我的作业,我必须创建一个程序,让用户输入多个数字。 I also want to create a list with all the numbers and their average. 我还想创建一个包含所有数字及其平均值的列表。

But how do I create a code for the list of numbers? 但是,如何为数字列表创建代码?

I should exit with a number such as 0 or -999. 我应该以0或-999之类的数字退出。

One line has invalid syntax. 一行语法无效。

print (number_list[i], end = " ")

Here is my code. 这是我的代码。

number_list = []
sum = 0.0

user_number = eval(input("Please enter a number (-999 quits): "))

# Loop until the user is ready to quit
while (user_number != -999):
    number_list.append(user_number)
    sum = sum + user_number
    user_number = eval(input("Please enter a number (-999 quits): "))

# Make sure the user entered something
if (len(number_list) != 0):
    # Compute average
    average = sum / len(number_list)

    # Do output
    print ("Using the numbers:")

    for i in range(len(number_list)):
        # Note the end = " " at the end will keep the output on
        #   the same line
        print (number_list[i], end = " ")

    # Note the \n at the start of this line is needed because
    #   the previous print statement ended with a comma.  This
    #   \n will move the cursor to the next line
    print ("\nThe average is:", average)
else:
    print ("No values were entered")

Because of Python's indentation rules, any compound statement needs to have at least one statement indented after it. 由于Python的缩进规则,任何复合语句都必须在其后至少缩进一个语句。

Let's focus on this section of your code: 让我们关注代码的这一部分:

for i in range(len(number_list)):
print (number_list[i], end = " ")
print ("\nThe average is:", average)
else:
print ("No values were entered")

A for loop is a compound statement as it needs stuff indented after it. for循环是一个复合语句,因为它需要缩进的内容。 You need to indent them accordingly. 您需要相应地缩进。 Something like: 就像是:

for i in range(len(eggs)):
    print(eggs[i])

The pythonic way to loop over stuff is just to use the value instead of getting the index and then finding it. 遍历内容的pythonic方法只是使用值而不是获取索引然后找到它。 Python's for loop is more like a foreach loop than an actual for loop. Python的for循环更像是foreach循环,而不是实际的for循环。 A remake would look like: 重新制作看起来像:

for spam in eggs:
    print(spam)

Also, you have a check for if there aren't any numbers. 另外,您还要检查是否没有数字。 Use a normal if statement for that, not one in the loop. 为此使用普通的if语句,而不是循环中的一个。 The else behind a loop will run when the main part ( while or for ) finishes without a break . else一个循环后面将运行时的主要部分( whilefor )没有完成break

This: 这个:

for spam in eggs:
    print(spam)
else:
    print("Nothing")

Is not the same as this: 与此不同:

if eggs:
    for spam in eggs:
        print(spam)
else:
    print("Nothing")

Here's the fixed section :D 这是固定部分:D

# If statement to check if list is truthy (nonempty)
if number_list:
    # Note the for loop's target 'number'
    for number in number_list:
        # Note indentation.
        print(number, end=" ")
    print("\nThe average is:", average)

else:
    print("No values were entered")

EDIT: You also have indentation errors after the while loop above this section. 编辑:本节上方的while循环后,您也有缩进错误。 I'll leave that to you to fix :) 我会留给你修复:)

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

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