Search code examples
pythonpython-3.xmethodstracebackbuilt-in

why do i get TypeError: must be str, not builtin_function_or_method?


I've been given a task to present the current time, and a given future time. the user inputs the number of additional minutes he desires, and then i need to show that exact time.

I've tried the below:

import time


hours = int(time.strftime('%H:%M')[0:2])
minutes = int(time.strftime('%H:%M')[3:5])


def newminutes(minutes,adding):
    if minutes + adding >=60:
        return minutes + adding - 60
    else:
        return minutes + adding

def newhours(hours, minutes, adding):
    if minutes + adding >=60:
        newhour = hours + adding//60 + 1     
    else:
        newhour =  hours + adding //60
        if newhour >= 24:
            return newhour - 24
         else:
            return newhour

usersays = input("insert additional minutes: ")
usersay = int(usersays)
finalhours = newhours(hours,minutes,usersay)
finalminutes = newminutes(minutes,usersay)

print("Current Time: " +time.strftime + "\n" +
      "Future Time: "+ str(finalhours) + ":" + str(finalminutes))

After inputting the number of minutes, the output i get is always:

Traceback (most recent call last): File "C:\Users\DELL\Documents\Python projects\Ex2\test2.py", line 29, in +":" + str(newminutes(minutes,usersay))) TypeError: must be str, not builtin_function_or_method

I've been trying to fix this for hours with no sucess, please explain me where have i gone wrong. Thanks in advance!


Solution

  • The exception you get is because you are trying to use string concatenation with a builtin function:

    print("Current Time: " +time.strftime + "\n" +
          "Future Time: "+ str(finalhours) + ":" + str(finalminutes))
    

    time.strftime is a function.

    >>> time.strftime
    <built-in function strftime>
    

    You need to call it so it returns a string. Try replacing it with the following, which returns a string object:

    time.strftime("%b %d %Y %H:%M:%S")
    'Apr 05 2018 10:57:48'