-2

The error says: "TypeError: unsupported operand type(s) for Add: 'int' and 'str' on line 39". What does this mean and how do i fix this? Here is the code:

import time
TimeIsUp=0
print ("Timer")
h=int(input("Hours-"))
m=int(input("Minutes-"))
if m>59:
 while m>0 and m!=0:
  m-=60
  h+=1
 m+=60
 h-=1
s=int(input("Seconds-"))
if s>59:
 while s>0 and s!=0:
  s-=60
  m+=1
 s+=60
 m-=1
 while m>=0 and m!=0:
  m-=60
  h+=1
 m+=60
 h-=1
while TimeIsUp==0:
 s-=1
 if s<0 and m>0:
  s+=61
  m-=1
  if m<0 and h>0:
   m+=61
   h-=1
 else:
  if h>0:
   s+=61
   m+=59
   h-=1
  else:
   TimeIsUp==1
 print (h+":"+m+":"+s)
 time.sleep(1)
print ("Time's Up!")

The "time" import if from https://trinket.io/python (Because that is what im using to code Phython as a beginner).

Horizon
  • 5
  • 4

6 Answers6

5

TypeError: unsupported operand type(s) for Add: 'int' and 'str' on line 39". What does this mean

It means that on line #39 of your code, you're trying to add (the '+' operator) an integer and an string - which doesn't make sense. Reading your code, this is obviously this line:

 print h+":"+m+":"+s

Note that strings implement the '+' operator as string concatenation, while ints implement it as (of course) addition.

and how do i fix this?"

You could turn your ints into strings as mentionned in other comments or answers, but the proper solution here is to use string formatting operations:

print "{}:{}:{}".format(h, m, s)

or if you want to be more explicit

print "{hour}:{minutes}:{seconds}".format(hour=h, minutes=m, seconds=s)

str.format() will take care of necessary conversions etc, and also provides more advanced formatting operations (leading zeros for numerics etc).

bruno desthuilliers
  • 75,974
  • 6
  • 88
  • 118
3

You just need to cast you integer values inside the print function. The reason is that the print function requires a string and Python does not auto-cast integers when are concatenated with strings:

print str(h) + ":" + str(m) + ":" + str(s)
mttbrt
  • 121
  • 7
1

This seems the problem:

print h+":"+m+":"+s

Cant concat string with int unless wrapping ints with str() or one of the options in this post

Check https://www.journaldev.com/23642/python-concatenate-string-and-int

Ali Ben Zarrouk
  • 1,891
  • 16
  • 24
1

The error line is here:

print h+":"+m+":"+s, use str() to convert it

beepbeep-boop
  • 204
  • 1
  • 2
  • 11
1

It is very hard to keep track on the code without the full traceback and the actual line numbers, we just can't know for sure what you planned to do.

Anyway:

It's seems to be that the problem is in this line:

print h+":"+m+":"+s

Quick fix is to change this line of code to:

`print str(h)+":"+str(m)+":"+str(s)'

In that way, you convert the variable from int back to string.

And if you are using Python 3:

print(str(h)+":"+str(m)+":"+str(s))

By the way, Python provides us a better way of doing this staff without using converts/casts.

You should use format probably most of the times:

print("{hour}:{minutes}:{seconds}".format(hour=h, minutes=m, seconds=s)

You can read more about string formatting here.

Thank you @bruno desthuilliers for clarifying it.

Ido
  • 138
  • 9
  • Python has string formatting operations too, which are prefered over string concatenation. – bruno desthuilliers Mar 31 '20 at 10:15
  • Yes, that is correct. But I didn't want to complicate it because as he said before, he has just started programming. – Ido Mar 31 '20 at 10:20
  • 1
    better to learn doing things the right way right from the start. Explaining why the op gets this error and how to convert ints to strings is fine, but _also_ mentionning string formatting is better. – bruno desthuilliers Mar 31 '20 at 10:24
  • You are probably right. I have edited my answer, thank you :) – Ido Mar 31 '20 at 10:38
  • Well, thank to _you_ actually ;-) (nb SO is supposed to be a knowledge base, not a forum - IOW, when you answer a question, you want to try and make your answer useful not only for the OP but also to anyone else reading it). – bruno desthuilliers Mar 31 '20 at 10:41
0

Basically to got a data type error. To concat a string you need to make all the datatypes match. The variables h, m and s are currently integers so you can use the str() function to convert them to. string.

Arnav Motwani
  • 707
  • 7
  • 26
  • Actually `str` is a type, not a function. – bruno desthuilliers Mar 31 '20 at 10:25
  • @brunodesthuilliers yes string is a datatype but what I was talking about is the str() function that converts other datatypes into a string. – Arnav Motwani Mar 31 '20 at 10:33
  • We're talking about the same thing - `str()` IS a type, not a class. Just open your python shell and type `type(str)`, you'll find out that it's a class (instance of type `type` - yes, `type` is a type too), not a function (which would be an instance of type `function`). – bruno desthuilliers Mar 31 '20 at 10:35
  • Fair enough I was just stating what I read in the python docs which calls it a function but your explanation also check out and make sense @brunodesthuilliers – Arnav Motwani Mar 31 '20 at 17:23