sturon
sturon

Reputation: 11

How can i read inputs till user decide to end

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

Answers (3)

Ben Morris
Ben Morris

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

jepio
jepio

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

inspectorG4dget
inspectorG4dget

Reputation: 113915

a = ' '
while a:  # loop exits when user just hits <ENTER>
    a=raw_input()
    # do stuff with a

Upvotes: 0

Related Questions