简体   繁体   English

Python错误:TypeError:+:'int'和'str'的不支持的操作数类型

[英]Python Error: TypeError: unsupported operand type(s) for +: 'int' and 'str'

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. 我正在尝试编写一个程序,用户可以输入他们希望它离开的小时数和分钟数,它需要当地时间和小时和分钟,并将两者加在一起以产生程序的时间关闭。
NOTE: I don't want it to put together my input and the numbers for the current time as a string. 注意:我不希望它将我的输入和当前时间的数字组合成一个字符串。 I need it to add the numbers together. 我需要它来将数字加在一起。

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'

My Code: 我的代码:

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

If I have understood your question correctly, then try changing your str() to int() instead like this: 如果我已正确理解您的问题,那么尝试将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()

time.strftime('%H') already is an integer, but time.strftime('%H')已经是一个整数,但是
hour_awake is a string, by your own definition. hour_awake是一个字符串,由您自己定义。
Hence the TypeError. 因此TypeError。

Python does not perform automatic type conversions. Python不执行自动类型转换。 You must do that yourself. 你必须自己做。 All the operands (variables) must be of the same type to "add": 所有操作数(变量)必须与“add”的类型相同:
- Two strings will concatenate into one string, - 两个字符串将连接成一个字符串,
- two integers will perform arithmetic addition. - 两个整数将执行算术加法。

You need explicitly cast hour_awake to an integer via: int(hour_awake) . 您需要通过: int(hour_awake)显式地将hour_awake转换为整数。
Then you should be able to add them together: 然后你应该能够将它们添加到一起:

alarm_hour = int(hour_awake) + time.strftime('%H')

and

alarm_minutes = int(minute_awake) + minute

This would allow you to keep hour_awake and minute_awake as strings. 这将允许您将hour_awakeminute_awake保持为字符串。
Alternatively, if you do not need them stored as strings, then instead change your input line to: 或者,如果您不需要将它们存储为字符串,则将input行更改为:

hour_awake = int(input("please enter in how many hours you would like to have the alarm go off in. "))

.. and do the same for minute_awake. ..并为minute_awake做同样的事情。

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

相关问题 类型错误:不支持 / 的操作数类型:python 中的“str”和“int”错误 - TypeError: unsupported operand type(s) for /: 'str' and 'int' error in python Python Panda错误TypeError:/不支持的操作数类型:“ str”和“ int” - Python Panda Error TypeError: unsupported operand type(s) for /: 'str' and 'int' typeerror 不支持 / &#39;str&#39; 和 &#39;int&#39; 的操作数类型 - typeerror unsupported operand type(s) for / 'str' and 'int' TypeError:-:“ str”和“ int”的不受支持的操作数类型 - TypeError:unsupported operand type(s) for -: 'str' and 'int' + 不支持的操作数类型:“int”和“str”[TypeError] - unsupported operand type(s) for +: 'int' and 'str' [TypeError] TypeError: 不支持的操作数类型 -: 'str' 和 'int' - TypeError: unsupported operand type(s) for -: 'str' and 'int' 类型错误:-= 不支持的操作数类型:'int' 和 'str' - TypeError: unsupported operand type(s) for -=: 'int' and 'str' 类型错误:&lt;&lt;:&#39;str&#39; 和 &#39;int&#39; 的操作数类型不受支持 - TypeError: unsupported operand type(s) for <<: 'str' and 'int' TypeError:-:“ int”和“ str”不支持的操作数类型 - TypeError: unsupported operand type(s) for -: 'int' and 'str' 类型错误:不支持 / 的操作数类型:&#39;str&#39; 和 &#39;int&#39; (2) - TypeError: unsupported operand type(s) for /: 'str' and 'int' (2)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM