Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/322.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
为什么赢了';我的Python闹钟坏了吗?_Python - Fatal编程技术网

为什么赢了';我的Python闹钟坏了吗?

为什么赢了';我的Python闹钟坏了吗?,python,Python,我正在尝试编写一个程序,用户可以输入他们希望程序启动的小时和分钟数,然后将本地时间和小时和分钟数相加,生成程序启动的时间 当我运行该程序时,出现以下错误: line 30, in alarm_time alarm_hour = (hour_awake + time.strftime('%H')) TypeError: unsupported operand type(s) for +: 'int' and 'str' 原因是您在def hours(): 并且time.strftime函

我正在尝试编写一个程序,用户可以输入他们希望程序启动的小时和分钟数,然后将本地时间和小时和分钟数相加,生成程序启动的时间

当我运行该程序时,出现以下错误:

line 30, in alarm_time alarm_hour = (hour_awake + time.strftime('%H')) TypeError: unsupported operand type(s) for +: 'int' and 'str'
原因是您在
def hours():

并且
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)

原因是您在
def hours():

并且
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)

作为旁注。。在学习python时,请尽量避免使用全局变量。他们可以学习,但使任何比短脚本更大的东西越来越难以管理,并且更容易受到bug的影响。同一个OP的同一个问题的可能重复作为旁注。。在学习python时,请尽量避免使用全局变量。它们可以学习,但是比短脚本更大的东西越来越难管理,也更容易受到bug的影响。同一个操作可能会重复同一个问题。所有这一切都是将我的输入和当前时间的数字作为字符串放在一起。我需要它来把数字加在一起。@CJPeine solution addedall做的就是把我的输入和当前时间的数字作为一个字符串放在一起。我需要它来把这些数字加在一起。@CJPeine solution added
def alarm_time():
    alarm_hour = (hour_awake + int(time.strftime('%H')))
    alarm_minutes = (minute_awake + int(time.strftime('%M')))
    print (alarm_hour, alarm_minutes)