Python Forum
Input while interation in a list - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Input while interation in a list (/thread-21590.html)



Input while interation in a list - doug2019 - Oct-06-2019

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.


RE: Input while interation in a list - jefsummers - Oct-06-2019

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"


Input values ​​of a variable multiple times in a list - doug2019 - Oct-06-2019

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]


RE: Input while interation in a list - buran - Oct-06-2019

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


List and variable - doug2019 - Oct-06-2019

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]



RE: Input while interation in a list - doug2019 - Oct-06-2019

(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.


RE: Input while interation in a list - buran - Oct-06-2019

user_input = []
for n in range(3):
    a = float(input('Insert a:'))
    user_input.append(a)
print(user_input)