Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to do variable assignment inside a while(expression) loop in Python?

I have the variable assignment in order to return the assigned value and compare that to an empty string, directly in the while loop.

Here is how I'm doing it in PHP:

while((name = raw_input("Name: ")) != ''):
    names.append(name)

What I'm trying to do is identical to this in functionality:

names = []
while(True):
    name = raw_input("Name: ")
    if (name == ''):
        break
    names.append(name)

Is there any way to do this in Python?

like image 971
Andreas Karlsson Avatar asked Feb 12 '09 16:02

Andreas Karlsson


People also ask

Can you declare a variable inside a while loop?

Yes. you can declare a variable inside any loop(includes do while loop.

Can I use if condition in while loop in Python?

Python while loop boolean conditionTo check the boolean expression in while loop condition we can simply use while if condition. It checks if the 'a' variable is true and the loop will exit after 3 iteration values (n=n+1) must be terminated 3 times until n==4.

Can you assign a variable to a for loop?

Often the variable that controls a for loop is needed only for the purposes of the loop and is not used elsewhere. When this is the case, it is possible to declare the variable inside the initialization portion of the for.


3 Answers

from functools import partial

for name in iter(partial(raw_input, 'Name:'), ''):
    do_something_with(name)

or if you want a list:

>>> names = list(iter(partial(raw_input, 'Name: '), ''))
Name: nosklo
Name: Andreas
Name: Aaron
Name: Phil
Name: 
>>> names
['nosklo', 'Andreas', 'Aaron', 'Phil']
like image 139
nosklo Avatar answered Oct 20 '22 06:10

nosklo


You can wrap raw_input() to turn it into a generator:

def wrapper(s):
    while True:
        result = raw_input(s)
        if result = '': break
        yield result

names = wrapper('Name:')

which means we're back to square one but with more complex code. So if you need to wrap an existing method, you need to use nosklo's approach.

like image 25
Aaron Digulla Avatar answered Oct 20 '22 04:10

Aaron Digulla


No, sorry. It's a FAQ, explained well here:

In Pydocs, and Fredrik Lundh's blog.

The reason for not allowing assignment in Python expressions is a common, hard-to-find bug in those other languages.

Many alternatives have been proposed. Most are hacks that save some typing but use arbitrary or cryptic syntax or keywords, and fail the simple criterion for language change proposals: it should intuitively suggest the proper meaning to a human reader who has not yet been introduced to the construct.

An interesting phenomenon is that most experienced Python programmers recognize the while True idiom and don’t seem to be missing the assignment in expression construct much; it’s only newcomers who express a strong desire to add this to the language.

There’s an alternative way of spelling this that seems attractive:

line = f.readline() while line:
    ... # do something with line...
    line = f.readline()
like image 9
Philip Reynolds Avatar answered Oct 20 '22 04:10

Philip Reynolds