Python函数,它有两个值

时间:2016-06-24 00:27:48

标签: python function python-3.x if-statement

此函数取两个整数x是小时,y是分钟。该函数应将文本中的时间打印到最近的小时。 这是我写的代码。

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)

但是我收到此错误消息。

  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

2 个答案:

答案 0 :(得分:3)

您正在尝试连接字符串和整数对象!将对象ConnectedThread(或y)转换为字符串,然后追加。像:

y+1

答案 1 :(得分:1)

你必须强制转换为字符串。您正在尝试将int和字符串连接在一起,这是不兼容的。

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