为什么我的Python闹钟不起作用?

时间:2017-08-07 01:33:32

标签: python

我正在尝试编写一个程序,用户可以输入他们希望它离开多少小时和分钟,然后需要当地时间和小时和分钟,并将两者加在一起以产生时间计划结束。

当我运行程序时,我收到此错误:

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()

1 个答案:

答案 0 :(得分:2)

原因是您将hour_awake设置为def hours():

中的int
    hour_awake = int(input(......

并且time.strftime函数返回str(字符串)。您不能同时+ intstr

编辑:

要将数字加在一起,您需要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)
相关问题