CJ Peine
CJ Peine

Reputation: 67

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

Upvotes: 2

Views: 2028

Answers (2)

SherylHohman
SherylHohman

Reputation: 17890

time.strftime('%H') already is an integer, but
hour_awake is a string, by your own definition.
Hence the TypeError.

Python does not perform automatic type conversions. You must do that yourself. All the operands (variables) must be of the same type to "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).
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.
Alternatively, if you do not need them stored as strings, then instead change your input line to:

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.

Upvotes: 0

a-coder
a-coder

Reputation: 53

If I have understood your question correctly, then try changing your str() to int() instead like this:

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

Upvotes: 3

Related Questions