Python Forum
Input while interation in a list
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Input while interation in a list
#1
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.
Reply
#2
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"
Reply
#3
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]
Reply
#4
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
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#5
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]
Reply
#6
(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.
Reply
#7
user_input = []
for n in range(3):
    a = float(input('Insert a:'))
    user_input.append(a)
print(user_input)
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Undo interation to make a single list? DustinKlent 2 97 Yesterday, 03:41 AM
Last Post: DustinKlent
  user input for multi-dimentional list without a prior iteration using input() Parshaw 6 357 Sep-22-2020, 04:46 PM
Last Post: Parshaw
  taking input doesnt print as list bntayfur 2 359 Jun-04-2020, 02:48 AM
Last Post: bntayfur
  Help with calling list from user input farispython 5 740 Nov-03-2019, 03:13 PM
Last Post: Gribouillis
  with input remove a string from the list konsular 3 600 Oct-12-2019, 09:25 AM
Last Post: konsular
  Creating csv header from user-input list dvanommen 2 712 Aug-26-2019, 08:51 PM
Last Post: dvanommen
  Forcing input from pre-defined list. scotty501 11 1,715 Jun-18-2019, 01:49 PM
Last Post: scotty501
  Need help with reading input from stdin into array list Annie123 2 1,761 Mar-24-2019, 01:19 PM
Last Post: Annie123
  User input - list Ondra 3 901 Feb-15-2019, 11:29 AM
Last Post: Larz60+
  How do I permanently change a list after user's input? Ablazesphere 6 1,242 Oct-31-2018, 08:30 PM
Last Post: ichabod801

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020