-3

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
Karl Knechtel
  • 62,466
  • 11
  • 102
  • 153
James Ocean
  • 145
  • 1
  • 2
  • 14
  • 2
    Don't call `int(y+1)` call `str(int(y)+1)` or better again use `str.format` – Padraic Cunningham Jun 24 '16 at 00:29
  • BTW, it should be `x`, not `y` in the `print` statements - please change the variable names to `hours` and `minutes` or the like to make things a bit more obvious. – Ken Y-N Jun 24 '16 at 00:34
  • 1
    Next time, try Googling the error message. – TigerhawkT3 Jun 24 '16 at 00:35
  • The error message seems crystal clear to me. Why the confusion? You're trying to concatenate a `str` and an `int` with the `+` operator. It's telling you you can't do that. You need to convert the `int` to a `str`. You're trying to do `"x" + 5` when you should be doing `"x" + "5"`. The error message basically told you this. Just read the error message, look at the code, think about it, and modify your code accordingly. – Tom Karzes Jun 24 '16 at 00:37
  • As suggested by @PadraicCunningham i found something like `print("the time is about {} o'clock".format(int(y)+1))` more elegant (and more performant?) than something like `print("some string" + str(some_var) + "some string")` (especially if their is more than one variable to format), but you will end with the same result. – mgc Jun 24 '16 at 00:43
  • 1
    Please don't vandalize your question after getting an answer. – MattDMo Jun 24 '16 at 01:03

2 Answers2

3

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)
UltraInstinct
  • 43,308
  • 12
  • 81
  • 104
1

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)