Python function that takes two values
本问题已经有最佳答案,请猛点这里访问。
这个函数取两个整数,x是小时,y是分钟。函数应将文本中的时间打印到最近的小时。这是我写的代码。
1 2 3 4 5 6 7 8 9 10 | 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) |
但是,我收到了这个错误消息。
1 2 3 4 | 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 |
号
您正在尝试连接字符串和整数对象!将对象
1 | print("the time is about quarter past" + str(y)) #similarly str(int(y)+1) |
你得铸成一根绳子。您试图将int和字符串连接在一起,这是不兼容的。
1 2 3 4 5 6 7 8 9 10 | 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) |
号