Jafar Ocean Jafar Ocean - 5 months ago 11
Python Question

Python function that takes two values

This function take two integers x is the hour and y is the minute. The function should print the time in the text to the nearest hour.
This is the code I have written.

def approxTime(x, y):
if int(y) <= 24:
print("the time is about quarter past" + int(y))
elif 25 >= int(y) <=40:
print("the time is about half past" + int(y))
elif 41 >= int(y) <= 54:
print("the time is about quarter past" + int(y+1))
else:
print("the time is about" + int(y+1) +"o'clock")
approxTime(3, 18)


However I am getting this error message.

Traceback (most recent call last): File
"C:/Users/Jafar/Documents/approxTime.py", line 14, in <module>
approxTime(3, 18) File "C:/Users/Jafar/Documents/approxTime.py", line 5, in approxTime
print("the time is about quarter past" + int(y)) TypeError: Can't convert 'int' object to str implicitly

Answer

You are trying to concatenate string and integer objects! Convert the object y (or y+1) to string and then append. Like:

print("the time is about quarter past" + str(y)) #similarly str(int(y)+1)
Comments