Tagged Questions
103
votes
12answers
135k views
do-while loop in Python?
I need to emulate a do-while loop in a python. But, unfortunately, following
straightforward code does not work:
l = [ 1, 2, 3 ]
i = l.__iter__()
s = None
while True :
if s :
print s
try :
...
25
votes
4answers
5k views
Else clause on Python while statement
I've noticed the following code is legal in Python. My question is why? Is there a specific reason?
n = 5
while n != 0:
print n
n -= 1
else:
print "what the..."
Thanks.
9
votes
2answers
502 views
Converting while to generator 3.4 times slow down
What is happening? Can somebody explain me what happens here, I changed in tight loop:
## j=i
## while j < ls - 1 and len(wordlist[j]) > lc: j+=1
j = next(j ...
7
votes
8answers
428 views
Create List With Numbers Getting Greater Each Time Python [closed]
How can I make a function that will create a list, increasing the amount of numbers it contains each time to a specified value?
For example if the max was 4, the list would contain
1, 2, 2, 3, 3, 3, ...
7
votes
6answers
1k views
returning out of for-loop
I'm pretty new at python and I was wondering if this:
def func(self, foo):
for foo in self.list:
if foo.boolfunc(): return True
return False
is good practice.
Can I return out of a ...
7
votes
8answers
5k views
Assignment in While Loop in Python?
I just came across this piece of code
while 1:
line = data.readline()
if not line:
break
#...
and thought, there must be a better way to do this, than using an infinite loop ...
6
votes
3answers
2k views
Is there any way to do variable assignments directly inside a while(<here>) loop in Python?
Is there any way to do this in python? I.e. have the variable assignment return the assigned value and compare that to an empty string, directly in the while loop.
No biggie if it isn't possible, just ...
6
votes
3answers
972 views
Assignment Condition in Python While Loop
In C, one can do
while( (i=a) != b ) { }
but in Python, it appears, one cannot.
while (i = sys.stdin.read(1)) != "\n":
generates
while (i = sys.stdin.read(1)) != "\n":
^
...
6
votes
3answers
4k views
python -> time a while loop has been running
i have a loop that runs for up to a few hours at a time. how could I have it tell me how long it has been at a set interval?
just a generic...question
EDIT: it's a while loop that runs permutations, ...
6
votes
2answers
193 views
While-loop with if-statement faster than while-loop
I am doing some testing regarding the speed of if-statements in loops and their effect on speed. Something I found was that consistently, the if-statement improved performance. My code:
import time
t ...
6
votes
4answers
211 views
Writing to a File with Python — ''While not done:" Confusing Me
I have less than a year of programming experience. While learning about reading and writing files I came across this tutorial: http://www.penzilla.net/tutorials/python/fileio/
The tutorial offers the ...
5
votes
3answers
2k views
Efficient and fast Python While loop while using sleep()
I am attempting to communicate with a device over serial using Pyserial. As commands need to be continually sent, they have to be placed in a while loop in Python.
I am currently using this code, ...
5
votes
3answers
350 views
Russian Peasant Multiplication Python 3.3
I need help with a program in python 3.3 that is supposed to do Russian peasant multiplication/ancient Egyptian multiplication. The assignment says," If "A" and "B" are the two integers to be ...
4
votes
3answers
104 views
Python while loop inconstancy
Never seen anything like this. Simple while loop:
t_end = 100.0
t_step= 0.1
time = 0
while time<=t_end:
time+=t_step
print time
Last 3 printed values:
...
99.9
100.0
100.1
Looks ...
4
votes
4answers
254 views
Why does raw_input create an infinite loop in this Learn Python the Hard Way exercise variant?
I'm trying to work my way through Learn Python the Hard Way, and trying to mess around where I can to further my education. I thought this would work: set up raw_input to set a limit for a while loop, ...