简体   繁体   English

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

[英]Why won't my Python Alarm Clock work?

I am trying to write a program that the user can enter in how many hours and minutes they want it to go off then, it take the local time and the hours and minutes and add the two together to produce the time for the program to go off. 我正在尝试编写一个程序,用户可以在该程序中输入要关闭的小时数和分钟数,然后花费当地时间和小时数和分钟数,并将两者加在一起即可得出程序运行的时间关。

when I run the program I get this error: 当我运行程序时,出现此错误:

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

The reason is that you set hour_awake to an int in def hours(): 原因是您在def hours():中将hour_awake设置为int def hours():

    hour_awake = int(input(......

and the time.strftime function returns a str (string). 并且time.strftime函数返回一个str (字符串)。 You cannot + an int and a str together. 您不能+一个int和一个str在一起。

EDIT: 编辑:

To add the number together, you need to int() your str s: 要将数字加在一起,需要将strint()

def alarm_time():
    alarm_hour = (hour_awake + int(time.strftime('%H')))
    alarm_minutes = (minute_awake + int(time.strftime('%M')))
    print (alarm_hour, alarm_minutes)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM