Reputation: 11
How can i write in python a code which will be doing excatly the same thing as:
while(cin>>a){//some operations on "a"}
in c++? I try:
while(a=raw_input()):
but IDLE throws a syntax error on "=". I don't want to use
while True:
Upvotes: 0
Views: 224
Reputation: 626
The syntax error IDLE gives on '=' is that in a conditional statement it needs to be '==', as in
while a == raw_input(''):
code code code
Upvotes: 0
Reputation: 2281
Use an iterator with a sentinel:
for a in iter(raw_input, ""):
# do stuff with a
this will keep running the first argument to iter
(raw_input()
) until the result (a
) is equal to the second argument (""
). This way the loop terminates when the user just presses enter.
Upvotes: 5
Reputation: 113915
a = ' '
while a: # loop exits when user just hits <ENTER>
a=raw_input()
# do stuff with a
Upvotes: 0