Below is the description of my issue

Hi Team,

I wanted to print a list that contains integers keyed in through the console and get them appended to the list. When i press ‘enter’ w/o giving an integer the loop should exit and prints the list.

nums=[]
x=int(input())
while !x:
    nums=nums.append(x)
    x=int(input())

print(nums)

But the above is giving me an syntax error @ while !x:

Regards,
Suresh

Hi.
Please, format your code, I leave the instruction for that:

What syntax error are you getting?

Input In [147]
while !x:
^
SyntaxError: invalid syntax

Are you sure that can be written in Python?

yes we can, its just like
until we key in ‘Enter’ in the console all the entered integers should go in to a list.

Have you considered telling us what you want to do with this line specifically?

Because this is a pretty simple problem, yet here we are several comments deep and nobody could tell you because you only talk about the entire thing and not about the line with the error…

Anyway the Python syntax is “not x” - to check if x is a falsy value. Which I assume is what you try to do there.

Hi,

I wanted to take elements from the console and append them to a list until i press just “Enter” and print the list.

You already said that like 4 times.

What is this line supposed to do? THIS line - not the program, not the other lines, not your goal. Tell us what THIS specific line is supposed to do.
What is “!x”? What do you think this is? How do you think this is evaluated as a boolean?

I don’t know if you wanted to do it like this, but this is my code which works like you want:

nums = []
x = input("Put some number: ")
while True:
    if x == "":
        print(nums)
        break
    nums.append(int(x))
    x = input("Put some number: ")


So if I am understanding you correctly, you are trying to use !x to mean, “While x is an integer,” append x to nums and get another number. So there are a few problems here. First, if you want the while loop to stop once a non-number is entered, you need to set some end condition to the while loop. Here is one way to do it:

nums = []
number = True

while number:
    x = input("Enter a number. To exit, enter a letter: ")
    if not x.isdigit():
        break

    nums.append(int(x))

print(nums)

In the above, the user enters something in input and that gets assigned to x (as a string). I then use the string method .isdigit() to determine whether x is a positive, whole integer. If x is not a positive, whole integer, then the code runs the break statement and exits the while loop, taking the code to the print statement. So in the above, the end condition occurs when x is not a positive, whole integer. Your code, as it stands, does not have an end condition. I think you may have been trying to do that with the !x since != means, “Not equal to,” but !x is not valid syntax and does not mean, “If x is not an integer.”

You could also do something similar using an if-else statement, such as:

while number:
    x = input("Enter a number. To exit, enter a letter: ")
    if x.isdigit():
        nums.append(x)
    else:
        number = False

So if x is a positive, whole integer, append it to nums, otherwise set number to False and end the while loop.

Also note that if you want to include negative numbers and/or floats, using .isdigit won’t work, so you’d need to do something else if you wanted your code to handle those. But I the fundamental problem with your current while loop is that it does not have any end condition.

The other thing to be aware of in your code is that you’ll get an error if you use x = int(input() and the user puts in something other than an integer.

1 Like

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.