3

I'm new to python programming and I'm doing some experiments with it. Hoping my question is not too silly: I'm writing a little program that adds inputs to a list and print it when the input equals to 4, using a while loop. the problem is it never stops to add inputs and print the list. My code is:

S=input()
L=[]
while S!=4:
    L.append(S)
    if S==4:
        break
print(L)
4
  • 2
    inputs are always of type string. when you type in 4 you are not adding 4 to the list but "4". You have to cast it to int first. Like int(input()) Commented Aug 31, 2017 at 7:40
  • Which version of Python are you using? Commented Aug 31, 2017 at 8:20
  • I'm using python 3.6 Commented Aug 31, 2017 at 14:48
  • Thank you, with int(input()) works perfectly Commented Aug 31, 2017 at 14:50

4 Answers 4

1

The problem is because you one time read the input S = 1 for example, after that S always 1 so S never be 4 and your loop is never stoop, you must add your input to while:

list = []
number = -1
while number != 4:
    number = int(input())
    list.append(number)
print(list)

And if you check that S != 4 in while you dont needifstatement inwhile`.

Sign up to request clarification or add additional context in comments.

Comments

1

input() function returns string objects, so they can never be equal to 4 which is an int object.

To get out of the problem, replace 4 by '4'.

This will consider the '4' as a string as the equality will hold when you enter 4 as input

Comments

1

There is another big problem with your code you don't update S anymore a more right answer would be:

S=input()
L=[]
while S!=4:
    L.append(S)
    if S=="4":
        break
    S = input()
print(L)

If you'd not want everything to block waiting for new input then you might consider throwing in threading but if it's just to allow the user to add values to an array and stop doing that by adding 4 this could help

1 Comment

This is with the int cast is offcourse if you want the array to contain int's, Which was not specificly mentioned by the creator
-1

You've created an infinite loop in your code. You're not changing the value of S after you enter the loop. You should add get an another input from inside the loop like so:

S=input()
L=[]
while S!=4:
   L.append(S)
   S = input()
print(L)

In addition, The if condition in your code is useless since It's already set in the while loop declaration

8 Comments

Well, the loop keeps running until the users insert the number 4. How exactly is this an infinite loop?
The input function returns strings, so S will never be an integer.
well in Python 3. input() does return a string, but in 2.7 it returns the type of whatever expression the user types in. I ran it on 2.7 and the python version is not specified in the question.
On SO you should assume Python 3 if no version is specified. And the OP is probably using Python 3 from the look of that print call, although of course that's no guarantee. However, the Python 2 input function should be avoided for security reasons. Also, anyone starting on Python now really ought to be be learning 3, not 2.
Well, what I were doing is write a code that runs endlessly until I digit "4". I was just trying to understand how Python works. Thanks @itaisls9! And yes, I'm using Python 3
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.