Possible Duplicate:
Is there any way to do variable assignments directly inside a while(<here>) loop in Python?
Sorry about such a basic question. I'm trying to learn Python, but really haven't found an answer to this.
In Python, can I assign a value to a variable in a while
or if
statement? If so, how?
For example. I had to do this:
line_list = []
while True:
line = raw_input(">>> ")
if not line: break
line_list.append(line)
print line_list
But I'd like to do this:
line_list = []
while line = raw_input(">>> "):
line_list.append(line)
print line_list
The tutorials I'm looking at don't mention this one way or another, but this is a common construct in most other languages.