I have a while loop counting in centiseconds and printing the current_step var, always on the same line.
I want to run, for example,
x = True
while x is True:
pass #printing timer to CLI here
print('this is more code running while the timer is still running')
input('Press enter to stop the timer')
x = False
#When x becomes False, I want the while loop to terminate
I know this must involve subprocess or something of the likes, but I don't know what direction to point myself in for learning to solve this issue.
Here is the function for reference:
def timer(stawt, stahp, step, time_step):
from time import sleep
stawt = int(stawt)
stahp = int(stahp)
if stahp < 1:
stahp = 1
elif stahp > 1000:
stahp = 1000
stahp = stahp * 100 + 1
titerator = iter(range(stawt, stahp, step))
while True:
try:
current_step = str(next(titerator))
if int(current_step) < 99:
final_time = '0' + current_step[:0] + '.' + current_step[0:] + 's'
print('\r' + final_time, end='')
elif int(current_step) < 999:
final_time = current_step[:1] + '.' + current_step[1:] + 's'
print('\r' + final_time, end='')
elif int(current_step) < 9999:
final_time = current_step[:2] + '.' + current_step[2:] + 's'
print('\r' + final_time, end='')
else:
final_time = current_step[:3] + '.' + current_step[3:] + 's'
print('\r' + final_time, end='')
sleep(time_step)
except:
print(); break
seconds = int((int(current_step) / 100) % 60)
minutes = int((int(current_step) / 100) // 60)
if minutes < 1:
return ''
else:
final_time_human = str(minutes) + 'm ' + str(round(seconds)) + 's'
print(final_time_human + '\n')
def MAIN():
count_to = float(input('Enter max number of seconds to count:\n'))
print()
timer(0, count_to, 1, 0.01)
MAIN()
You need to use threading.
import threading
x = True
def thread_function():
while x is True:
pass #printing timer to CLI here
threading.Thread(target=thread_function).start()
# Continue with the other steps you want to take
# ...
# This will terminate the timer loop
x = False
Python threading documentation: https://docs.python.org/3/library/threading.html
If you want to print the time always at the same line you need to control terminal cursor. For more information checkout: How can move terminal cursor in Python?
Thanks to #dorukerenaktas, I have got this working. This is their answer adapted into my scriptlet:
import threading
from os import system
timer_thread = None
def timer(stawt, stahp, step, time_step):
from time import sleep
global run_timer
stawt = int(stawt)
stahp = int(stahp)
if stahp < 1:
print('Sorry, I limit min count to 1 second!\n')
stahp = 1
elif stahp > 1000:
print('Sorry, I limit max count to 1000 seconds!\n')
stahp = 1000
else:
print()
stahp = stahp * 100 + 1
titerator = iter(range(stawt, stahp, step))
def print_time():
while run_timer is True:
try:
current_step = str(next(titerator))
if int(current_step) < 99:
final_time = '0' + current_step[:0] + '.' + current_step[0:] + 's'
print('\r' + final_time, end='')
elif int(current_step) < 999:
final_time = current_step[:1] + '.' + current_step[1:] + 's'
print('\r' + final_time, end='')
elif int(current_step) < 9999:
final_time = current_step[:2] + '.' + current_step[2:] + 's'
print('\r' + final_time, end='')
else:
final_time = current_step[:3] + '.' + current_step[3:] + 's'
print('\r' + final_time, end='')
sleep(time_step)
except:
break
seconds = int((int(current_step) / 100) % 60)
minutes = int((int(current_step) / 100) // 60)
if minutes < 1:
return ''
else:
final_time_human = str(minutes) + 'm ' + str(round(seconds)) + 's'
print('\n' + final_time_human)
print_time()
def _init_timer():
global run_timer; run_timer = True
global timer_thread
print('Enter max number of seconds to count: ', end='')
count_to = float(input())
timer_thread = threading.Thread(target=timer, args=(0, count_to, 1, 0.01))
timer_thread.start()
print('\rPress enter to stop the timer:')
usr_input = input(); run_timer = False
system('clear')
_init_timer()
timer_thread.join()
print('\nGoodbye!')
Related
im rather new to python and found someones lottery simulation in github. After playing around with it for a while i wanted to add a counter, that counts the number of matches of your Number out of the total draws.
I don't know if it is because i did not write the code myself, but i can't seem to make it happen. I've tried some of pythons counter modules bu that did'nt seem to be the right thing.
Heres my code:
import random
import time
### TODO - Refactor GetDrawNumbers (add timers)
### TODO - Refactor CheckNumbers
def GetDrawNumbers():
drawNumbers = []
for i in range(6):
x = None
while (x == None or x in drawNumbers):
x = random.randint(1, 49)
drawNumbers.append(x)
return drawNumbers
def CheckNumbers(myTicket, actualNumbers):
numbersMatched = 0
for number in myTicket:
if number in actualNumbers:
numbersMatched += 1
return numbersMatched
### Script starts here
startTime = time.perf_counter()
myNumbers = [4, 8, 15, 16, 23, 42]
for draw in range(2000):
drawNumber = draw + 1
thisWeeksDraw = GetDrawNumbers()
numbersMatched = CheckNumbers(myNumbers, thisWeeksDraw)
##print("Week " + str(drawNumber) + " numbers : " + str(thisWeeksDraw) + " (" + str(numbersMatched) + " matched)")
if numbersMatched == 4:
print("Week " + str(drawNumber) + " numbers : " + str(thisWeeksDraw) + " (" + str(numbersMatched) + " matched)")
count = numbersMatched
print("Total matches: " + str(count))
endTime = time.perf_counter()
elapsedTime = endTime - startTime
print("Completed in " + str(elapsedTime) + " seconds!")
If anyone knows a way to implement a counter, that counts the number of times this the program gets 3,4,5 or 6 correct matches i would be super relieved! It's not that this project would be super important but solving the problem would be a milestone for me and my learning process!
Thanks in advance and best wishes!
How about this where I have added a check of the numbersMatched value and increment a counter whenever it is 3 or more
import random
import time
### TODO - Refactor GetDrawNumbers (add timers)
### TODO - Refactor CheckNumbers
def GetDrawNumbers():
drawNumbers = []
for i in range(6):
x = None
while (x == None or x in drawNumbers):
x = random.randint(1, 49)
drawNumbers.append(x)
return drawNumbers
def CheckNumbers(myTicket, actualNumbers):
numbersMatched = 0
for number in myTicket:
if number in actualNumbers:
numbersMatched += 1
return numbersMatched
### Script starts here
startTime = time.perf_counter()
myNumbers = [4, 8, 15, 16, 23, 42]
countOfThreeOrMoreMatched = 0
for draw in range(2000):
drawNumber = draw + 1
thisWeeksDraw = GetDrawNumbers()
numbersMatched = CheckNumbers(myNumbers, thisWeeksDraw)
##print("Week " + str(drawNumber) + " numbers : " + str(thisWeeksDraw) + " (" + str(numbersMatched) + " matched)")
if numbersMatched >= 3:
countOfThreeOrMoreMatched += 1
if numbersMatched == 4:
print("Week " + str(drawNumber) + " numbers : " + str(thisWeeksDraw) + " (" + str(numbersMatched) + " matched)")
print(f"Count with 3 or more matches {countOfThreeOrMoreMatched}")
count = numbersMatched
print("Total matches: " + str(count))
endTime = time.perf_counter()
elapsedTime = endTime - startTime
print("Completed in " + str(elapsedTime) + " seconds!")
I'm making a small program that shoots out math problems and requires an answer to pass. It works fine, but all the randint values I generate stay static for as long as the progran is running. I figured if I change:
Tehtävä = random.choice(Laskut)
Into a function it should refresh with the loop. Problem is I can't for the life of me figure out how to do that. Would it even work for what I'm trying? The randint values are determined in a seperate list. Heres the rest of the code:
Peli = 1
while Peli != 2:
pulma = 1
refresh = 1
Tehtävä = random.choice(Laskut)
while pulma == 1:
ratkaisu = float(input(Tehtävä.problem + "\n:"))
if ratkaisu == Tehtävä.answer:
pulma += 1
refresh += 1
print("oikein")
elif ratkaisu == "loppu":
pulma += 1
refresh += 1
Peli += 1
else:
print("väärin")
Here are the values I used:
import random
class Algebra:
def __init__(self, problem, answer):
self.problem = problem
self.answer = answer
#Muuttujat
#erotus ja summa
a = random.randint(1,99)
b = random.randint(1,99)
c = random.randint(1,99)
d = random.randint(1,99)
#jako ja kerto
e = random.randint(1,10)
f = e*random.randint(1,10)
g = random.randint(1,10)
#Kysymykset
Kysymys_M = [str(a) + "+" + str(b) + "-x=" + str(c),
str(a) + "-" + str(b) + "-x=" + str(a),
str(a) + "-" + str(b) + "-" + str(c) + "-x=" + str(d),
str(e) + "*x=" + str(f),
str(f) + ":x=" + str(e),
"x:" + str(e) + "=" + str(g)]
#Vastaukset
Vastaus_M = [a+b-c,
-b,
a-b-c-d,
f/e,
f/e,
e*g]
Laskut = [
Algebra(Kysymys_M[0], Vastaus_M[0]),
Algebra(Kysymys_M[1], Vastaus_M[1]),
Algebra(Kysymys_M[2], Vastaus_M[2]),
Algebra(Kysymys_M[3], Vastaus_M[3]),
Algebra(Kysymys_M[4], Vastaus_M[4]),
Algebra(Kysymys_M[5], Vastaus_M[5]),]
(If I have packed too much information please let me know.)
Whilst attempting CS50's PSET6, I was trying to create a double-half pyramid of a size specified by the user.
The pyramid is fine but there's a random new line after the user input and before the pyramid starts. How can I fix it? Any help is appreciated :D
The code is as follows
def main():
hashHeight = height()
create(hashHeight)
# get height
def height():
h = int(input("Height: "))
if h >= 1 and h <= 8:
return h
else:
height()
#print hash
def create(x):
for i in range(x + 1):
print(" " * (x - i) + "#" * i + " " + "#" * i)
main()
def main():
hashHeight = height()
create(hashHeight)
# get height
def height():
h = int(input("Height: "))
if h >= 1 and h <= 8:
return h
else:
height()
#print hash
def create(x):
for i in range(1, x + 1):
print(" " * (x - i) + "#" * i + " " + "#" * i)
main()
With the display I want the Banking command: input to start on a new string but when it gets to the display elif it does not. I know why, its because of the end= '' but I need to have the display be in one line for the assignment and I cant figure out a solution. Thanks for the help.
def main():
number_of_accounts = int(input("Number of accounts:\n"))
accounts = [0.0] * number_of_accounts
banking_command(accounts)
def banking_command(accounts):
from os import _exit as exit
active = True
while active:
banking_command = input('Banking command:\n')
banking_command = banking_command.split(' ')
if banking_command[0] == 'add':
monetary_amount = float(banking_command[2])
account_being_changed = int(banking_command[1])
accounts[account_being_changed - 1] += monetary_amount
elif banking_command[0] == 'subtract':
monetary_amount = float(banking_command[2])
account_being_changed = int(banking_command[1])
accounts[account_being_changed - 1] -= monetary_amount
elif banking_command[0] == 'move':
monetary_amount = float(banking_command[3])
transfer_money_out = int(banking_command[1])
transfer_money_in = int(banking_command[2])
accounts[transfer_money_out - 1] -= monetary_amount
accounts[transfer_money_in - 1] += monetary_amount
elif banking_command[0] == 'display':
i = 0
while i < len(accounts):
account_number = i + 1
print(str(account_number) + ":$" + str(accounts[i]) + " ", end= '')
i += 1
elif banking_command[0] == 'exit':
exit(0)
main()
Add a print() after the while loop.
elif banking_command[0] == 'display':
i = 0
while i < len(accounts):
account_number = i + 1
print(str(account_number) + ":$" + str(accounts[i]) + " ", end= '')
i += 1
print() # <-- end the line with the account display
I am fairly new to python, I am not sure on how to fix a index string out of range. it happens right after the while loop when I want to send mylist[i][0] to formatting function. Any pointer on my code in general would be awesome!
def formatting(str1):
if str1 == '?':
return True
else:
return False
while(i <= len(mylist)):
val = formatting(mylist[i][0])
if val == True:
str1 = mylist[i]
str2 = mylist[i+1]
i = i + 2
format_set(str1, str2)
else:
if format == True:
if (margin + count + len(mylist[i])) <= width:
if (i == (len(mylist)-1)):
list2.append(mylist[i])
print(" " * margin + " ".join(list2))
break
list2.append(mylist[i])
count += len(mylist[i])
i += 1
else:
print(" " * margin + " ".join(list2))
list2 = []
count = 0
else:
temp_margin = margin
temp_width = width
width = 60
margin = 0
if (margin + count + len(mylist[i])) <= width:
if (i == (len(mylist)-1)):
list2.append(mylist[i])
print(" " * margin + " ".join(list2))
margin = temp_margin
width = temp_width
break
list2.append(mylist[i])
count += len(mylist[i])
i += 1
else:
print(" " * margin + " ".join(list2))
list2 = []
count = 0
change
i <= len(mylist)
to
i < len(mylist)
In the last iteration of the while loop, i is referring to the last value. Hence,
str2 = mylist[i+1]
is trying to reference a string outside the allowed range and you get an error.
EDIT: Also, as Wcrousse mentioned, the while (i <= len(...)) should be changed to i < len(...) because indexes go from 0 - (length-1).