如何获取用户输入并在 Python 中的“for”循环中应用?

How to take a user input and apply in 'for’ loop in Python?

我是 Python 的初学者。
我正在尝试从用户那里获取输入并在 for 循环中使用它。但它不起作用。我也尝试过使用范围函数,但它也没有用!
还有其他方法吗?

这是代码:

numbers = int(input('10 numbers: '))
sum_of_all_number = 0
result = ''
for each_number in numbers:
    sum_of_all_number += each_number
    result = sum_of_all_number / 10
print(f'The average is {result}')

输出:

10 numbers: 1234567890
Traceback (most recent call last):
  File "C:\Users\admin\Documents\Python\school project duplii.py", line 4, in <module>
    for each_number in numbers:
TypeError: 'int' object is not iterable

使用这个:

numbers = int(input('10 numbers: '))
sum_of_all_number = 0
result = 0
count = 0
for each_number in range(numbers):
    sum_of_all_number += each_number
    count += 1
result = sum_of_all_number / count
print(f'The average is {result}')

我不知道你想做什么。但我认为这就是你想要完成的。

sum_of_all_number = 0
result = 0
for each_number in range(10):
    numbers = int(input("ENter any Number: "))
    sum_of_all_number += number

result = sum_of_all_number / 10
print(f'The average is {result}')

要在不更改背后逻辑的情况下修复代码,请执行以下操作:

numbers = input('10 numbers: ')
sum_of_all_number = 0
result = ''
for each_number in numbers:
    sum_of_all_number += int(each_number)
    result = sum_of_all_number / 10
print(f'The average is {result}')

输出

10 numbers: 1234567890
The average is 4.5

当然 result/average 不能在循环内,所以只需:

numbers = input('10 numbers: ')
sum_of_all_number = 0
for each_number in numbers:
    sum_of_all_number += int(each_number)
result = sum_of_all_number / 10
print(f'The average is {result}')