I've just started out Python and was trying to make a countdown timer and make it unbreakable as possible, however when I enter blank inputs, the while loop won't handle it, and this message would show up instead: invalid literal for int() with base 10: ''. It also pointed the error occurring at the line where it asks for count-down.
Any help will be appreciated.
while countdown == 0 or countdown == "":
print("We need a person to countdown.")
countdown = int(input("How many seconds would you like the countdown to be?: "))
while countdown > 30:
try:
countdown = int(input("Enter non-extreme values please: "))
except ValueError:
print("Enter possible value.")
while countdown > 0: #Countdown sequence
time.sleep(2)
countdown -= 1
print(countdown)
print("BLASTTT OFFFFFFFFFFFFFF!!!")
print("We have a liftoff...")
A bad string that can not be converted to an int, will raise a ValueError. You catch that exception and just repeat the prompt like this:
countdown = 0
while countdown <= 0:
try:
countdown = int(input("How many seconds would you like the countdown to be?: "))
except ValueError:
pass
As soon as a proper integer > 0 has been entered the while
will break.