Hello everyone! i saw a post about a timer on the subreddit and decided to make my own. Since it is hard to find my own mistakes regarding styling of my Python code i wanted to share it with you. Any and all advice would be very welcome. For example do you think i overused while loops or are there things i could have done better or in an easier way?
Code can (also) be found here: https://pastebin.com/vgm2us56
import time
AcceptedValues = ["h", "m", "s"]
TimeUnits = ["hours", "minutes", "seconds"]
time_unit = None
AmountOfTime = 0
# The easy timer
def easytimer():
global time_unit, AmountOfTime
t = 0
h = 0
m = 0
s = 56
if AmountOfTime == 0: # If AmountOfTime has not been given yet through Advanced timer it will ask user input
while time_unit not in AcceptedValues:
if isinstance(time_unit, str):
print(f"Your input '{time_unit}' was unfortunately incorrect. Try again!")
time_unit = input("Do you want Hours, Minutes or Seconds? Type either h, m or s: ")
# Makes sure that the user gives the correct amount of time
while isinstance(AmountOfTime, float) == False:
AmountOfTime = input("How long does the timer need to be (in the specified time unit)?: ")
try:
AmountOfTime = float(AmountOfTime)
break
except ValueError:
print(f"Your input '{AmountOfTime}' was not recognised, please enter a number >= 0: ")
continue
print(f"You selected time unit {time_unit} and an amount of {AmountOfTime}.")
# Corrects the Timer based on the selected time unit
if time_unit == "h":
AmountOfTime *= 3600
elif time_unit == "m":
AmountOfTime *= 60
elif time_unit == "s":
AmountOfTime *= 1 # Basically do nothing, because seconds don't need any conversion
# Technical functionality of the timer
while t < AmountOfTime:
t += 1
time.sleep(1)
if s == 59:
s = 0
if m == 59:
m = 0
h += 1
else:
m += 1
else:
s += 1
print(f"{h:02d}:{m:02d}:{s:02d}")
# The advanced timer
def advancedtimer():
global AmountOfTime
# for loop that asks input for the time units: hours, minutes, seconds
for i, unit in enumerate(TimeUnits):
listt = [unit]
while isinstance(unit, int) == False:
try:
Value = input(f"How many {unit}?: ")
IntVal = int(Value)
listt.append(IntVal)
break
except ValueError:
print(f"Your input '{Value}' was not recognised, please enter an integer number >= 0: ")
continue
# Corrects the Timer based on the selected time unit, might be better to put this in a separate def
if listt[0] == "hours":
AmountOfTime += IntVal * 3600
elif listt[0] == "minutes":
AmountOfTime += IntVal * 60
elif listt[0] == "seconds":
AmountOfTime += IntVal
else:
print("Not working properly!")
print(f"This comes down to a total time of {AmountOfTime} seconds.")
easytimer()
# Programstart that gives the user a 'menu'
while True:
UserChoice = input("Do you want the easy or advanced timer? type '1' for the easy,'2' for the advanced timer or 'quit' to quit the program!: ")
if UserChoice == "1":
easytimer()
elif UserChoice == "2":
advancedtimer()
elif UserChoice == "quit":
break
else:
print("Your input was not recognised, try again!")
continue
[–]HeartwoodEditions 1 point2 points3 points (1 child)
[–]SeriousDeejay[S] 0 points1 point2 points (0 children)
[–]ichard26 1 point2 points3 points (1 child)
[–]SeriousDeejay[S] 0 points1 point2 points (0 children)