简体   繁体   English

我在 python 中的 for 循环和输入遇到问题

[英]I'm having a trouble with for loop and input in python

n=int(input("enter amount of values : "))
a=[]
for i in range (n):
 a[i]=int(input("number: "))

I don't know why python does some bad things happen around there can someone explain or show correct way.我不知道为什么 python 会在那里发生一些不好的事情,有人可以解释或显示正确的方法。

That doesn't work那行不通

I'm not sure but I think you can use append to append thing on list.我不确定,但我认为您可以在列表中使用append到 append 的东西。

Code:代码:

n=int(input("enter amount of values : "))
a=[]
for i in range (n):
    a.append(int(input("number: ")))

print(a)

Output: Output:

enter amount of values : 5
number: 3
number: 6
number: 2
number: 10
number: 6
[3, 6, 2, 10, 6]

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

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