該函數採用兩個整數x是小時,y是分鐘。該函數應該將文本中的時間打印到最近的小時。 這是我寫的代碼。需要兩個值的Python函數
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
不要叫'INT(Y + 1)''調用STR(INT(Y)+1)'或再次更好的使用'str.format' –
BTW,這應該是'x',不'print'語句中的'y' - 請將變量名改爲'hours'和'minutes'或類似的東西來使事情變得更加明顯。 –
下次嘗試使用Google搜索錯誤消息。 – TigerhawkT3