Python function that takes two values

Question:

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
Asked By: James Ocean

||

Answers:

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)
Answered By: UltraInstinct

You have to cast to a string. You are trying to concatenate an int and a string together, which are not compatible.

def approxTime(x, y):
     if int(y) <= 24:
         print("the time is about quarter past" + str(y))
     elif 25 >= int(y) <=40:
         print("the time is about half past" + str(y))
     elif 41 >= int(y) <= 54:
         print("the time is about quarter past" + str(y+1))
     else:
         print("the time is about" + str(y+1) +"o'clock")
approxTime(3, 18)
Answered By: Vincent Rodomista
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.