+ 2

Hi I need help with the infinite loops test task, if any one can help me I'll appreciate.

I have tried to make a task, from python, more precise with while loops, the task is infinite loops, the task says this The given code uses an infinite loop to continuously take input from the user. During each iteration, user input is added to the list of items. Change the code to end the loop when the user enters 0. Output the resulting list after the while loop ends. Input example 1 2 3 0 Output example [1, 2, 3] Don't add 0 to the list. I managed to make a code that manages to do a part of the task and is this list = [] x = int (input ()) while not x == 0: list.append (x) print (list) break and it works but with one input, i can put that input in the list , but the problem is that I need to put more inputs and for the loop to finish if in any of those inputs there is the number 0, and that's what I don't know how to do it, I know how to do it with one input, but I need several input, I don't know how to make the loop while take several inputs.

24th Mar 2021, 1:24 AM
Alan Restrepo
Alan Restrepo - avatar
67 Réponses
+ 8
You have to input all your numbers at the same time. 0 has to be one of them so the program will end. Use 'enter' to put each number on a separate line, and then hit 'submit' after the last one. It's a quirk of SoloLearn. This might be helpful: https://code.sololearn.com/WhiNb9BkJUVC
24th Mar 2021, 2:34 AM
David Ashton
David Ashton - avatar
+ 5
l = [] while True: x = int(input()) .... .... # redacted # print(l) Note it's not a good idea to name a variable list
24th Mar 2021, 2:02 AM
ChaoticDawg
ChaoticDawg - avatar
+ 5
Don't use the name of built-in types or functions etc as a variable name. It will overwrite them and make them unusable further on in your code. for example; if I call a variable list like; list = [1,2 3,4] and the later in my code try to convert a string to a list like; my_string = list('Hello, World!') it will result in an error, because list is now equal to [,1,2,3,4] instead of the function with the name list.
24th Mar 2021, 2:10 AM
ChaoticDawg
ChaoticDawg - avatar
+ 3
technically, infinite loops are endless loops... as soon as you provide a way to exit (break) the loop, that's no more an infinite loop ^^ while True: is a potentially infinite loop... but not really an infinite loop if there's a reachable 'break' statement inside it ;P
24th Mar 2021, 1:57 AM
visph
visph - avatar
+ 3
anthony silver This is the code 😄 a=list() while True: n=int(input()) if n==0: break a.append(n) print("List is:",a)
25th Mar 2021, 5:12 AM
ᗩηιηɗуα ᗩɗнιкαяι
+ 2
anthony silver FYI, it is common to use 4 spaces for each indentation block in Python. This will also make your code PEP compliant. This is because, visually it is easier to identify which block of code each line belongs to. While you can use another indentation space count, such as 2 spaces, as long as they are uniform, it typically isn't recommended.
24th Mar 2021, 2:51 AM
ChaoticDawg
ChaoticDawg - avatar
+ 2
anthony silver Are you running the code in the general playground or in the code coach?
24th Mar 2021, 2:54 AM
ChaoticDawg
ChaoticDawg - avatar
+ 2
visph Python core > Control Structures > while loops 27.4 Practice Infinite Loops Pro
24th Mar 2021, 3:14 AM
ChaoticDawg
ChaoticDawg - avatar
+ 1
Your loop should be infinite. while True: # get input here Then only if x is equal to 0 do you break. if x == 0: break Then add to list. .... Then print list outside of, and after loop edited for additional clarity
24th Mar 2021, 1:33 AM
ChaoticDawg
ChaoticDawg - avatar
+ 1
Sure, but can you attempt to do it first with the outline that I provided and then post that code if you can't get it to work.
24th Mar 2021, 1:53 AM
ChaoticDawg
ChaoticDawg - avatar
+ 1
visph True!!
24th Mar 2021, 1:59 AM
ChaoticDawg
ChaoticDawg - avatar
+ 1
Yes , I'm having trouble with this task
24th Mar 2021, 2:00 AM
Alan Restrepo
Alan Restrepo - avatar
+ 1
David Ashton not in code coach tasks ;)
24th Mar 2021, 2:35 AM
visph
visph - avatar
+ 1
i.append(x) should be indented same as x=int(input()) and if x == 0: lines to be in the loop ;P
24th Mar 2021, 2:41 AM
visph
visph - avatar
+ 1
Ok I'm going to fix it, thanks for the help, I'm trying to do this task for a while
24th Mar 2021, 2:42 AM
Alan Restrepo
Alan Restrepo - avatar
+ 1
In the general playground, you have to enter all your inputs at the same time. So, when you click run the input dialog box pops up and then you enter; 1 2 3 0 Then click submit and you should get this in the output pane at the bottom; [1, 2, 3]
24th Mar 2021, 3:03 AM
ChaoticDawg
ChaoticDawg - avatar
+ 1
Just copy and paste this. Try running it in the playground if you're having issues, it's not the code. Lol
24th Mar 2021, 3:09 AM
ChaoticDawg
ChaoticDawg - avatar
+ 1
Yes bro thanks for your help and for your attention and for your patience, the code of ChaoticDawg worked fine, Jesus Crist bless you
24th Mar 2021, 3:22 AM
Alan Restrepo
Alan Restrepo - avatar
+ 1
the only differences between your last code and ChaoticDawg code are the number of spaces used for indentation and the list variable name ^^ you probably introduce some mistake when retyping it in code coach: that's a good reflex: better advised to retype than copy paste when learning, even if it is error prone... so you must be carreful to not make mistake ;P
24th Mar 2021, 3:38 AM
visph
visph - avatar
+ 1
I agree, you should try retyping over copy and paste. I was just running low on time before I had to leave while trying to help you.
24th Mar 2021, 5:09 AM
ChaoticDawg
ChaoticDawg - avatar