Alarm clock problem in general
Ask the user for the time now (in hours), and ask for the number of hours to wait. Your program should output what the time will be on the clock when the alarm goes off.
current_time = input("what is the current time (in hours)?")
wait_time = input("How many hours do you want to wait")
print(current_time)
print(wait_time)
Next to figure out what the time will be after waiting wait_time number of hours
current_time = input("What is the current time (in hours 0 - 23)?")
wait_time = input("How many hours do you want to wait")
print(current_time)
print(wait_time)
final_time = current_time + wait_time
print(final_time)
The problem:
Python is doing string concatenation, not integer addition
current_time_str = input("What is the current time (in hours 0-23)?")
wait_time_str = input("How many hours do you want to wait")
current_time_int = int(current_time_str)
wait_time_int = int(wait_time_str)
final_time_int = current_time_int + wait_time_int
print(final_time_int)
Testing on boundary conditions
current_time_int = 10
wait_time_int = 20
we get final_time_int
of 30
current_time_str = input("What is the current time (in hours 0-23)?")
wait_time_str = input("How many hours do you want to wait")
current_time_int = int(current_time_str)
wait_time_int = int(wait_time_str)
final_time_int = current_time_int + wait_time_int
final_answer = final_time_int % 24
print("The time after waiting is: ", final_answer)
The process of debugging is much more like being a detective.
Everyone is a suspect (Except Python)!
Find clues.
Error Messages
Print Statements
%reset -f
current_time_str = input("What is the current time (in hours 0-23)?")
wait_time_str = input("How many hours do you want to wait")
current_time_int = int(current_time_str)
wait_time_int = int(wait_time_int)
final_time_int = current_time_int + wait_time_int
print(final_time_int)
Statistics about errors
Message | Number | Percent |
---|---|---|
ParseError | 4999 | 54.74% |
TypeError | 1305 | 14.29% |
NameError | 1009 | 11.05% |
ValueError | 893 | 9.78% |
ParseError
Parse errors happen when you make an error in the syntax of your program.
Usually ParseErrors can be traced back to missing punctuation characters, such as parenthesis, quotation marks, or commas.
Paretheses must be balanced.
current_time_str = input("What is the current time (in hours 0-23)?")
wait_time_str = input("How many hours do you want to wait"
current_time_int = int(current_time_str)
wait_time_int = int(wait_time_str)
final_time_int = current_time_int + wait_time_int
print(final_time_int)
TypeError
TypeErrors occur when you you try to combine two objects that are not compatible.
For example you try to add together an integer and a string.
Usually type errors can be isolated to lines that are using mathematical operators, and usually the line number given by the error message is an accurate indication of the line.
a = input(u'wpisz godzine')
x = input(u'wpisz liczbe godzin')
int(x)
int(a)
h = x // 24
s = x % 24
print (h, s)
a = a + s
print ('godzina teraz %s' %a)
NameError
Name errors almost always mean that you have used a variable before it has a value.
Often NameErrors are simply caused by typos in your code.
str_time = input("What time is it now?")
str_wait_time = input("What is the number of nours to wait?")
time = int(str_time)
wai_time = int(str_wait_time)
time_when_alarm_go_off = time + wait_time
print(time_when_alarm_go_off)
Another example
n = input("What time is it now (in hours)?")
n = imt(n)
m = input("How many hours do you want to wait?")
m = int(m)
q = m % 12
print("The time is now", q)
And another
present_time = input("Enter the present timein hours:")
set_alarm = input("Set the hours for alarm:")
int (present_time, set_time, alarm_time)
alarm_time = present_time + set_alarm
print(alarm_time)
ValueError
Value errors occur when you pass a parameter to a function and the function is expecting a certain type, but you pass it a different type.
Test different inputs running the following example
current_time_str = input("What is the current time (in hours 0-23)?")
current_time_int = int(current_time_str)
wait_time_str = input("How many hours do you want to wait")
wait_time_int = int(wait_time_int)
final_time_int = current_time_int + wait_time_int
print(final_time_int)
You need to keep track of the types of your variables, and understand what types your function is expecting.
You can do this by writing comments in your code, or by naming your variables in a way that reminds you of their type.
print
statements are your friends.