-2

i want to take input from user in and each value of the input is on consecutive line.this is to be implemented in python

while x=int(raw_input()):    ##<=showing error at this line
    print(x)
    gollum(x)
#the function gollum() has to be called if the input is present
martineau
  • 119,623
  • 25
  • 170
  • 301
  • 1
    Possible duplicate of [Asking the user for input until they give a valid response](http://stackoverflow.com/questions/23294658/asking-the-user-for-input-until-they-give-a-valid-response) – Morgan Thrapp Jul 21 '16 at 13:57
  • Might you share your precious "error at this line" with ussss? – deceze Jul 21 '16 at 13:58
  • 1
    the checks fro equality either with `if`, or `elif` or `while` use double equality signs `==` – Ma0 Jul 21 '16 at 13:58
  • @deceze the error is `SyntaxError: invalid syntax`, as the while does not get anything to test, an assignment not returning any value =/ – HolyDanna Jul 21 '16 at 14:02
  • @HolyDanna Side question: if an assignment does not return a value, why does `a = b = 42` work as it does in other languages where it works because `=` *does* return a value? – deceze Jul 21 '16 at 14:09
  • @deceze I do no know that you can chain variable assignment, but that does not mean it returns a value. In python prompt, every returned value is printed. – HolyDanna Jul 21 '16 at 14:14
  • @HolyDanna http://stackoverflow.com/q/38506873/476 :) – deceze Jul 21 '16 at 14:24
  • @deceze I was right, and thanks to you, I have the prove I am. I knew about assignments not having values, as I had to write a basic tiger compiler, and knowing if something has a value or not is needed when writing such a thing =p – HolyDanna Jul 21 '16 at 14:40

2 Answers2

0

The reason why your code does not work is why wants a condition or an object. As you are assigning a value (x=raw_input()), while does not find anything to test (an assignment does NOT return any value).
You can either request an input, and then do a while loop depending on the value of this input (that will be modified inside the while loop) :

x = int(raw_input())
while x:
    print(x)
    gollum(x)
    x = int(raw_input())
HolyDanna
  • 609
  • 4
  • 13
0

That gives you an error because x=int(raw_input()) doesn't return a boolean, and you need a boolean inside the while condition. You can try this one:

    while True:
        x = raw_input()
        if x=='':
            break
        x = int(x)
        print(x)
        gollum(x)

that way if you put an empty string (just an enter) the program just stops and doesn't give an annoying error :P

PedroNeves
  • 110
  • 10