-2

Hey I'm trying to create an infinite list in python that takes user input in till 0 is entered, I don't really know if this is the right way to do so, here's what I did:

    `n = input("Enter a list element separated by space ")
        while n == 0:
            break
        else:
            list = n.split() 
     print(list)`

Thank you!

Acc Sinx
  • 1
  • 3
  • 2
    The code you have posted is not a valid python code. Please submit a valid code and explain what is the problem. – balderman Oct 11 '20 at 08:30
  • 2
    Does this answer your question? [Asking the user for input until they give a valid response](https://stackoverflow.com/questions/23294658/asking-the-user-for-input-until-they-give-a-valid-response) – Wups Oct 11 '20 at 08:31
  • Please update your question with your real code. There are several mistakes in your post which will stop it from running. – quamrana Oct 11 '20 at 08:31
  • It will show NameError: name 'n' is not defined, coz instead of specifying a range for n say (1:5), i want it to print an infinite list but idk what number to put in for n – Acc Sinx Oct 11 '20 at 08:33
  • Please update your question with ‘n=5’ as an example. – quamrana Oct 11 '20 at 08:36
  • I just update my code! – Acc Sinx Oct 11 '20 at 08:42
  • hey wups, sorry dont really know how to reply to your message specifically, I'm lookin for similar things but more of how to convert user input into a list – Acc Sinx Oct 11 '20 at 08:47
  • Now you have completely changed your code. It looks nothing like your description of what you want to do. – quamrana Oct 11 '20 at 08:47
  • hey quamrana, just had a new idea all sudden so i changed it completely, all i want it's just keep take user input as a list and, when user enter 0 the list will stop – Acc Sinx Oct 11 '20 at 08:49

2 Answers2

1

This code will do what you originally described:

Numbers = []

while True:
    number = int(input("Please enter a number:"))
    if number == 0:
        break
    Numbers.append(number)

print(Numbers)
quamrana
  • 37,849
  • 12
  • 53
  • 71
  • yassss this is what im lookin for! thank you so much, but could you pls explain to me that why isn't my code running properly? – Acc Sinx Oct 11 '20 at 09:13
  • Lots of the comments have already tried to explain why your code isn’t running. Without seeing your *actual* code I can do no better. – quamrana Oct 11 '20 at 09:15
0

try this

l = []
while 1:
   n = input("Enter a list element seperated by a space")
   for x in n.split():
       if x == 0:
           break
       l.append(int(x))
Ekure Edem
  • 310
  • 2
  • 10