Suppose I'd like to print those numbers where I've given inputs.
假设我想在我给出输入的地方打印这些数字。
Here is my program, I want to iterate numbers where I've given the input by using array
. How do you do it?
这是我的程序,我想迭代我使用数组给出输入的数字。你怎么做呢?
array = []
num = input("Enter the number of Element: \n")
num = int(num)
array = [num]
print("Enter the", num,"Element: ")
for i in range(0, num):
myNum = input()
myNum = int(myNum)
array += [myNum]
for j in range(array[i]):
print(j)
When I run this, it says:
当我运行它时,它说:
//Output:
Enter the number of Element:
3
Enter the 3 Element:
5
6
4
0
1
2
3
4
5
What's wrong here?
这有什么不对?
EDITED
Here is my new program:
这是我的新计划:
array = []
num = input("Enter the number of Element: \n")
num = int(num)
array = [num]
print("Enter the", num,"Element: ")
for i in range(0, num):
myNum = input()
myNum = int(myNum)
array = [myNum]
for j in array:
print(j)
//OUTPUT
Enter the number of Element:
3
Enter the 3 Element:
4
5
6
6
1 个解决方案
#1
In your new program you initialize the list array
with one element that contains the number of loops. I don't think that you want this value in your list.
在新程序中,使用包含循环数的一个元素初始化列表数组。我认为您不希望在列表中显示此值。
In the loop you keep overriding the list again and again. You have to append
the data to the list.
在循环中,您将一次又一次地覆盖列表。您必须将数据附加到列表中。
Then you might want to print your message "Enter the x. element" in your loop, not before.
然后,您可能希望在循环中打印消息“输入x。元素”,而不是之前。
num = int(input("Enter the number of elements: "))
array = []
for i in range(num):
print('Enter the {}. element: '.format(i))
array.append(int(input())
for j in array:
print(j)
#1
In your new program you initialize the list array
with one element that contains the number of loops. I don't think that you want this value in your list.
在新程序中,使用包含循环数的一个元素初始化列表数组。我认为您不希望在列表中显示此值。
In the loop you keep overriding the list again and again. You have to append
the data to the list.
在循环中,您将一次又一次地覆盖列表。您必须将数据附加到列表中。
Then you might want to print your message "Enter the x. element" in your loop, not before.
然后,您可能希望在循环中打印消息“输入x。元素”,而不是之前。
num = int(input("Enter the number of elements: "))
array = []
for i in range(num):
print('Enter the {}. element: '.format(i))
array.append(int(input())
for j in array:
print(j)