gpt4 book ai didi

Python错误: TypeError: unsupported operand type(s) for +: 'int' and 'str'

转载 作者:行者123 更新时间:2023-11-30 22:32:14 27 4
gpt4 key购买 nike

我正在尝试编写一个程序,用户可以输入他们希望它关闭的小时和分钟,然后,它需要本地时间以及小时和分钟,然后将两者相加以产生该时间程序关闭。
注意:我不希望它将我的输入和当前时间的数字作为字符串放在一起。我需要它来将数字相加。

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

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

str(hour)
str(minute)



def tick():
global time_now
time_now = time.strftime("%H:%M:%S")



def hours():
global hour_awake
hour_awake = str(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 = str(input("please enter in how many minutes you would like to have the alarm go off in. "))

def alarm_time():
alarm_hour = (hour_awake + hour)
alarm_minutes = (minute_awake + minute)
print (alarm_hour, alarm_minutes)


tick()

hours()
alarm_time()

最佳答案

如果我正确理解了你的问题,那么尝试将你的 str() 更改为 int() ,如下所示:

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



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 + hour)
alarm_minutes = (minute_awake + minute)
print (alarm_hour, alarm_minutes)


tick()

hours()
alarm_time()

关于Python错误: TypeError: unsupported operand type(s) for +: 'int' and 'str' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45539185/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com