0

I am running into this problem while trying to make a calculator, that takes time in seconds and gives an output of time in minutes:seconds, and it makes this error

error

This is my code:

timeInSeconds = 1823
timeInMinutes = timeInSeconds / 60
timeInRest = timeInSeconds % 60

str(timeInMinutes)
str(timeInRest)

print("Your time in minutes and seconds is " + timeInMinutes + ":" + timeInRest)
  • simply write this, print("Your time in minutes and seconds is " + str(timeInMinutes) + ":" + str(timeInRest)), or you need to store the str converted int values back to that var. here just casting to int will not store result to the org variable. – aTechieSmile Jan 05 '21 at 09:28

2 Answers2

0

Change

print("Your time in minutes and seconds is " + timeInMinutes + ":" + timeInRest)

To

print("Your time in minutes and seconds is " + str(timeInMinutes) + ":" + str(timeInRest))
Abhishek Gurjar
  • 7,426
  • 10
  • 37
  • 45
billz
  • 44,644
  • 9
  • 83
  • 100
0

You forgot to reassign the new values:

timeInSeconds = 1823
timeInMinutes = timeInSeconds / 60
timeInRest = timeInSeconds % 60

timeInMinutes = str(timeInMinutes)
timeInRest = str(timeInRest)

print("Your time in minutes and seconds is " + timeInMinutes + ":" + timeInRest)

Basically doing this :

str(timeInMinutes)

Does not mean your TimeInMinutes variable is now a string. You have to reassign it like so :

timeInMinutes = str(timeInMinutes)

I believe this is what you meant to do.


However, it could be useful to keep the int values for later. If that's the case, you could just create new variables like so :

timeInMinutesStr = str(timeInMinutes)
timeInRestStr = str(timeInRest)

print("Your time in minutes and seconds is " + timeInMinutesStr + ":" + timeInRestStr)

Or, you could just convert the values directly in the print, to avoid having to assign new variables that you might not use later :

print("Your time in minutes and seconds is " + str(timeInMinutes) + ":" + str(timeInRest))
Shiverz
  • 663
  • 1
  • 8
  • 23