why does my python while loop infinitely? - python

I am trying to do a while loop that execute a function while the verification is not changed.
my code looks like this:
from time import sleep
verif = 0
num = 5
def doso(num, verif):
if num%11 == 0:
verif += 1
elif num%14 == 0:
verif += 1
print(num)
return verif
while verif == 0:
doso(num, verif)
num += 1
sleep(1)
so for now it run infinitely... i would like if it stoped when it find a multiple of 11 or 14
**its an example

Try updating the variable:
while verif == 0:
verif = doso(num, verif)
num += 1
sleep(1)

To avoid running into an XY problem, note that you do not need verif at all. You can simply do:
num = 5
while True:
if num%11 == 0 or num%14 == 0:
break
else:
num += 1

Related

Python Restarting a loop

How can I restart the loop in the following code after it hits 1 since in 3N+1 it goes 1>4>2>1?
Code:
import math
import random
num = 1
NumTF = False
play = True
while play:
if num % 2 == 0:
num = num / 2
else:
num = 3 * num + 1
print(num)
if num == 1:
play = False
if play == False:
num += 1 and play == True
I'm assuming that you want to end the loop, because ...Collatz Conjecture.
All you have to do is add this simple if statement to the end:
elif num == 1:
play = False
after the if num%2 == 0 statement, so the while loop ends. Currently, your doing this after your doing num = 3*num+1, which makes it 4, so that case never happens. as rv.kvetch mentioned, the play==True does unexpected things, so just delete everything after print(num), as those are unnecessary.

Threading in Python with Timed While Loop

A similar question was asked earlier but it didn't provide the correct answer.
I am trying to code to test threading in Python in which a ticker ticks every second. I am trying to keep the ticker function named 'clicking' running in a thread whose output is continously being incremented by one every second.
import time
import threading
import queue
q = queue.Queue()
apple = 0
orange = 0
rate = 1
clix = 0
def clicking(clix, rate):
while True:
time.sleep(1)
clix += rate
q.put(clix)
threading.Thread(target=clicking, args=(clix, rate)).start()
curr = q.get()
print(curr)
print('\nClicker Starting...')
endgame = False
while not endgame:
print(f'Clix: {curr}')
print('1. Apple : 10 clix | 2. Orange : 8 clix | 3. Exit')
ch = int(input('\nPurchase ID: '))
if ch == 1 and curr >= 10:
print(f'You have {curr} clix.')
print('Got an Apple!')
apple += 1
rate += 1.1
curr -= 10
elif ch == 2 and curr >= 8:
print('Got an Orange!')
orange += 1
rate += 1.2
curr -= 8
elif ch == 3:
endgame = True
stopflag = True
else:
print('Need more Clix')
But my otuput is always 1 instead of incrementing every second by defined rate. What am I missing? I even tried return clix in place of q.put(clix) but didn't work.
the problem is that you are not updating the curr variable inside the while loop. But do notice that when you write "curr = q.get()" inside the while loop it will get the next value in the queue and not the last value (as I suppose you intend). I guess a more straightforward approach is to keep track on the seconds increment inside your while loop using time.time()
import time
apple = 0
orange = 0
rate = 1
clix = 0
curr = 0
last_ts = time.time()
print('\nClicker Starting...')
endgame = False
while not endgame:
ts = time.time()
curr += (ts - last_ts) * rate
last_ts = ts
print(f'Clix: {curr:.0f}')
print('1. Apple : 10 clix | 2. Orange : 8 clix | 3. Exit')
ch = int(input('\nPurchase ID: '))
if ch == 1 and curr >= 10:
print(f'You have {curr:.0f} clix.')
print('Got an Apple!')
apple += 1
rate *= 1.1 # I guess you meant x1.1
curr -= 10
elif ch == 2 and curr >= 8:
print('Got an Orange!')
orange += 1
rate *= 1.2 # I guess you meant x1.2
curr -= 8
elif ch == 3:
endgame = True
stopflag = True
else:
print('Need more Clix')
this way you can exit properly also, notice that on your example even when the loop breaks the thread continues.
but in case you want to maintain a background thread, I suggest creating a class and storing class variables for the current counter and run condition.

How can I check for a key press at any point during a loop?

I am trying to make a timer that counts down to 0, then starts counting up. I am using the time and keyboard modules.
The keyboard module from PyPi.
Everything works as expected, and I am able to press a button to close the program, but it only works at the beginning of each iteration. Is there a way for it to check for a key press at any point while the loop is running? Do I need to be using a different module?
This is my code:
import time
import keyboard
m = 2
s = 0
count_down = True
while True:
if keyboard.is_pressed('q'):
break
print(f"{m} minutes, {s} seconds")
if count_down:
if s == 0:
m -= 1
s = 60
s -= 1
elif not count_down:
s += 1
if s == 60:
m += 1
s = 0
if m == 0 and s == 0:
count_down = False
time.sleep(1)
Using callback is common approach in such case, here is solution:
import time
import keyboard
m = 2
s = 0
count_down = True
break_loop_flag = False
def handle_q_button():
print('q pressed')
global break_loop_flag
break_loop_flag = True
keyboard.add_hotkey('q', handle_q_button)
while True:
if break_loop_flag:
break
print(f"{m} minutes, {s} seconds")
if count_down:
if s == 0:
m -= 1q
s = 60
s -= 1
elif not count_down:
s += 1
if s == 60:
m += 1
s = 0
if m == 0 and s == 0:
count_down = False
time.sleep(1)
If you want to do any two things in parallel, independently of another, you need to consider using multiprocessing. However, even if you do, your loop will either still need to check if a key has been registered in the other process, or you need to terminate the process running the loop forcefully, which may result in unexpected outcomes.
However, in your case, since there are no side effects like files being written, this would work:
import time
import keyboard
from multiprocessing import Process
def print_loop():
m = 2
s = 0
count_down = True
while True:
print(f"{m} minutes, {s} seconds")
if count_down:
if s == 0:
m -= 1
s = 60
s -= 1
elif not count_down:
s += 1
if s == 60:
m += 1
s = 0
if m == 0 and s == 0:
count_down = False
time.sleep(1)
def main():
p = Process(target=print_loop)
p.start()
# this loop runs truly in parallel with the print loop, constantly checking
while True:
if keyboard.is_pressed('q'):
break
# force the print loop to stop immediately, without finishing the current iteration
p.kill()
if __name__ == '__main__':
main()

Loop stops even though only 1 of 2 variables is true

So I'm attempting to make a Brainfuck interpreter, however in the while loop that I am using to execute the Brainfuck loop, it is breaking out even though only one condition is true.
Example:
+++[>+<-]
Should result in:
[0, 3]
However, when the loop begins at [, it will create a new cell so the structure goes from [3] to [3, 0]. Thus, the current working cell is 0 and the loop is breaking out. However, I have it to only break if it is 0 and the current character is ].
cells = [0] # Array of data cells in use
brainfuck = str(input("Input Brainfuck Code: ")) # Brainfuck code
workingCell = 0 # Data pointer position
count = 0 # Current position in code
def commands(command):
global cells
global workingCell
if command == ">":
workingCell += 1
if workingCell > len(cells) - 1:
cells.append(0)
elif command == "<":
workingCell -= 1
elif command == "+":
cells[workingCell] += 1
elif command == "-":
cells[workingCell] -= 1
elif command == ".":
print(chr(cells[workingCell]))
elif command == ",":
cells[workingCell] = int(input("Input: "))
def looper(count):
global cells
global workingCell
print("START LOOP", count)
count += 1
looper = loopStart = count
while brainfuck[looper] != "]" and cells[workingCell] != 0: # This line is causing trouble
if brainfuck[looper] == "]":
looper = loopStart
commands(brainfuck[looper])
count += 1
looper += 1
return count
while count < len(brainfuck):
if brainfuck[count] == "[":
count = looper(count)
print("END LOOP", count)
else:
commands(brainfuck[count])
count += 1
Thank you in advance.
I have it to only break if it is 0 and the current character is ]
If that's what you want, you have the logic in your while wrong. It should be:
while not (brainfuck[looper] == "]" and cells[workingCell] == 0):
And according to deMorgan's Laws, when you distribute not across and, you invert each of the conditions and change and to or, so it should be:
while brainfuck[looper] != "]" or cells[workingCell] != 0:
If this is confusing, you could just write:
while True:
if brainfuck[looper] == "]" and cells[workingCell] == 0:
break
This mirrors what you said in the description exactly.

one function generating numbers for another

I would like to create function that will count up & count down and I would like to access the counted value every let's say 500ms from another function.
How can I return this value 'a' every 500ms so I can read it externaly every e.g. 500ms ?
PS. I am using Python 2.7
This is my code so far with yield usage but it does not give what I want:
import time
class PLCApplication(object):
def generate_data(self):
a = 0
countup = True
while a >= 0:
time.sleep(0.5)
if countup == True:
a += 2
else:
a -= 2
if a < 0:
countup = True
a += 2
if a == 12:
countup = False
a -= 2
yield a
while True:
plc = PLCApplication()
b = plc.generate_data()
for z in b:
time.sleep(0.5)
print 'z...', z
EDIT:
This is the functionality I wanted to achieve. Thanks:
import time
from drawnow import *
import matplotlib.pyplot as plt
x = []
y = []
plt.ion()
class PLCApplication(object):
def generate_data(self):
a = 0
countup = True
while a >= 0:
time.sleep(0.5)
if countup: # no need to do test if it equals True
a += 2
else:
a -= 2
if a < 0:
countup = True
a += 2
if a == 12:
countup = False
a -= 2
yield a
def makefig(self):
plt.ylim(-10,30)
plt.plot(x, 'ro-', label='testgraph')
plt.grid(True)
if __name__ == '__main__':
plc = PLCApplication()
cnt = 0
for t in plc.generate_data():
i = t
x.append(int(i))
cnt=cnt+1
if cnt > 20:
x.pop(0)
print x, cnt
drawnow(plc.makefig)
UPDATE: updated answer according to C14L's comment, original answer below.
import time
class PLCApplication(object):
def generate_data(self):
a = 0
countup = True
while a >= 0:
time.sleep(0.5)
if countup: # no need to do test if it equals True
a += 2
else:
a -= 2
if a < 0:
countup = True
a += 2
if a == 12:
countup = False
a -= 2
yield a
def do_stuff(b):
print b
if __name__ == '__main__':
plc = PLCApplication()
for t in plc.generate_data():
do_stuff(t)
Original answer:
You need to print a at an approriate place inside the while loop:
[...]
while a >= 0:
print a
time.sleep(0.5)
[...]
This example from the Python Coroutine page seems to do that in a non-blocking way, using new asyncio.
import asyncio
import datetime
#asyncio.coroutine
def display_date(loop):
end_time = loop.time() + 5.0
while True:
print(datetime.datetime.now())
if (loop.time() + 1.0) >= end_time:
break
yield from asyncio.sleep(1)
loop = asyncio.get_event_loop()
# Blocking call which returns when the display_date() coroutine is done
loop.run_until_complete(display_date(loop))
loop.close()

Categories