Python Forum

Full Version: Input while interation in a list
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi! How do I code an while iteration and input its values ​​in a list? Example:
count=0
while (count<5):
      print(count)
      count+1
x=[], but for count+1
x=[0,1,2,3,4]
I would be very grateful if someone could help me.
When you know how many times you want to go through the loop, better to use a for statement. While is more for loops where you are less sure of the number of iterations and want to check for a condition.
x = []
for count in range(5):
    print(count)
    x.append(count)
print (x)
Output:
0 1 2 3 4 [0, 1, 2, 3, 4]
To use while, instead, your code is closer
x = []
count = 0
while count<5 :
    print (count)
    x.append(count)
    count+=1
print (x)
Gives same output, but you can see the code is slightly longer because you are manually handling "count"
How do I code a list with which I input the variable several times? Example:

1 a=float(input("Insert a:")
2 x=[a,a,a]
Please, don't start new threads unnecessarily. If the answer you get does not satisfy, elaborate further and explain better what your goal is. Also you have been advised to use BBcode tags
How do I code a list with which I input the value of the variable umpteen times? Example:

a=float(input("Insert a:")
x=[a,a,a]
(Oct-06-2019, 05:21 PM)buran Wrote: [ -> ]Please, don't start new threads unnecessarily. If the answer you get does not satisfy, elaborate further and explain better what your goal is. Also you have been advised to use BBcode tags
Sorry for my failure. My english is not very good and I am learning to use this forum.
user_input = []
for n in range(3):
    a = float(input('Insert a:'))
    user_input.append(a)
print(user_input)