我正在尝试编写一个程序,用户可以输入他们希望它离开多少小时和分钟,然后需要当地时间和小时和分钟,并将两者加在一起以产生时间计划结束。
当我运行程序时,我收到此错误:
line 30, in alarm_time alarm_hour = (hour_awake + time.strftime('%H')) TypeError: unsupported operand type(s) for +: 'int' and 'str'
from tkinter import *
import tkinter
import time
time_now = ''
hour = time.strftime('%H')
minute = time.strftime('%M')
int(hour)
int(minute)
def tick():
global time_now
time_now = time.strftime('%H:%M:%S')
print (time_now)
def hours():
global hour_awake
hour_awake = int(input("please enter in how many hours you would like to have the alarm go off in. "))
minutes()
def minutes():
global minute_awake
minute_awake = int(input("please enter in how many minutes you would like to have the alarm go off in. "))
def alarm_time():
alarm_hour = (hour_awake + time.strftime('%H'))
alarm_minutes = (minute_awake + time.strftime('%M'))
print (alarm_hour, alarm_minutes)
hours()
alarm_time()
tick()
答案 0 :(得分:2)
原因是您将hour_awake
设置为def hours():
hour_awake = int(input(......
并且time.strftime
函数返回str
(字符串)。您不能同时+
int
和str
。
要将数字加在一起,您需要int()
str
s:
def alarm_time():
alarm_hour = (hour_awake + int(time.strftime('%H')))
alarm_minutes = (minute_awake + int(time.strftime('%M')))
print (alarm_hour, alarm_minutes)