AttributeError: 'tuple' object has no attribute

Discuss how to use the Ren'Py engine to create visual novels and story-based games. New releases are announced in this section.
Forum rules
This is the right place for Ren'Py help. Please ask one question per thread, use a descriptive subject like 'NotFound error in option.rpy' , and include all the relevant information - especially any relevant code and traceback messages. Use the code tag to format scripts.
Post Reply
Message
Author
Psion
Regular
Posts: 25
Joined: Fri Dec 11, 2020 4:01 pm
Contact:

AttributeError: 'tuple' object has no attribute

#1 Post by Psion »

Hi, I'm trying to make a calendar with classes from this tutorial :https://www.youtube.com/watch?v=XoowRbpBbs4
But i'm getting an error:

Code: Select all

I'm sorry, but an uncaught exception occurred.

While running game code:
  File "game/script.rpy", line 16, in script
    $ calendar.AddTime (1)
  File "game/script.rpy", line 16, in <module>
    $ calendar.AddTime (1)
AttributeError: 'tuple' object has no attribute 'AddTime'

-- Full Traceback ------------------------------------------------------------

Full traceback:
  File "game/script.rpy", line 16, in script
    $ calendar.AddTime (1)
  File "E:\soft\renpy-7.3.5-sdk\renpy\ast.py", line 914, in execute
    renpy.python.py_exec_bytecode(self.code.bytecode, self.hide, store=self.store)
  File "E:\soft\renpy-7.3.5-sdk\renpy\python.py", line 2028, in py_exec_bytecode
    exec bytecode in globals, locals
  File "game/script.rpy", line 16, in <module>
    $ calendar.AddTime (1)
AttributeError: 'tuple' object has no attribute 'AddTime'

Windows-7-6.1.7601-SP1
Ren'Py 7.3.5.606
Chemist 1.0
Fri Jan 15 19:40:40 2021
my code:

Code: Select all


init python:
    class Calendar(object):
        def __init__(self, wd, d, h, m):
            self.d = d
            self.h = h
            self.m = m
            self.wd = wd

        def AddTime (self, h):
            self.h += h
            if m > 59:
                hadd, m = m%60
                h += hadd
            if h > 23:
                h = h%24
            return h,m

label lists:
    $ calendar = Calendar (['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday','Sunday'], 1, 8, 0)
return


label start:
    call lists
    $ GameRunning = True
    while GameRunning:
        'qwe  '
        $ calendar = (3, 5, 6, 7)
        $ calendar.AddTime (1)
        "It is [h]:[m]"
I doing almost same thing as shown in tutorial. Pls help.

User avatar
RicharDann
Veteran
Posts: 286
Joined: Thu Aug 31, 2017 11:47 am
Contact:

Re: AttributeError: 'tuple' object has no attribute

#2 Post by RicharDann »

Check this line:

Code: Select all

$ calendar = (3, 5, 6, 7)
Not sure what are you trying to do here, but it's overwriting the calendar variable, which contains a Calendar() object, with (3, 5, 6, 7), a tuple object.
The most important step is always the next one.

Psion
Regular
Posts: 25
Joined: Fri Dec 11, 2020 4:01 pm
Contact:

Re: AttributeError: 'tuple' object has no attribute

#3 Post by Psion »

Tnx for answer. I was trying to assign new variables to it ... Hm, as i understand i can only modify variables with functions?

So i removed this line $ calendar = (3, 5, 6, 7) and fixed some errors

Code: Select all

init python:
    class Calendar(object):
        def __init__(self, wd, d, h, m):
            self.d = d
            self.h = h
            self.m = m
            self.wd = wd
            
        def AddTime (self, h , m):
            self.h += h
            if m > 59:
                hadd, m = m%60
                h += hadd
            if h > 23:
                h = h%24
            return h,m

        def GetTime (self):
            if self.m > 59:
                hadd, m = m%60
                h += hadd
            if h > 23:
                h = h%24
            return h,m
            
label lists:
    $ d=1
    $ h=1
    $ m=0
    $ calendar = Calendar (['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday','Sunday'], 1, 8, 0)
return
            
label start:
    call lists
    $ GameRunning = True
    while GameRunning:
        'qwe  '
        $ calendar.AddTime (2, 30)
        $ h,m = calendar.GetTime()
        "It is [h]:[m]"
new error...

Code: Select all

I'm sorry, but an uncaught exception occurred.

While running game code:
  File "game/script.rpy", line 18, in script
    $ h,m = calendar.GetTime()
  File "game/script.rpy", line 18, in <module>
    $ h,m = calendar.GetTime()
  File "game/clock/event.rpy", line 30, in GetTime
    if h > 23:
UnboundLocalError: local variable 'h' referenced before assignment

-- Full Traceback ------------------------------------------------------------

Full traceback:
  File "game/script.rpy", line 18, in script
    $ h,m = calendar.GetTime()
  File "E:\soft\renpy-7.3.5-sdk\renpy\ast.py", line 914, in execute
    renpy.python.py_exec_bytecode(self.code.bytecode, self.hide, store=self.store)
  File "E:\soft\renpy-7.3.5-sdk\renpy\python.py", line 2028, in py_exec_bytecode
    exec bytecode in globals, locals
  File "game/script.rpy", line 18, in <module>
    $ h,m = calendar.GetTime()
  File "game/clock/event.rpy", line 30, in GetTime
    if h > 23:
UnboundLocalError: local variable 'h' referenced before assignment

Windows-7-6.1.7601-SP1
Ren'Py 7.3.5.606
Chemist 1.0
Fri Jan 15 20:22:28 2021

User avatar
_ticlock_
Miko-Class Veteran
Posts: 910
Joined: Mon Oct 26, 2020 5:41 pm
Contact:

Re: AttributeError: 'tuple' object has no attribute

#4 Post by _ticlock_ »

Hi, Psion,

1) Your errors is because you use variable h in method GetTime. You need to use class attribute self.h
2) It is recommended to use the default statement to 'declare' variables

Here is a better example of what you want to do:

Code: Select all

init python:
    class Calendar(object):
        weekdays = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday','Sunday']
        def __init__(self, wd, d, h, m):
            self.d = d
            self.h = h
            self.m = m
            self.wd = wd

        def AddTime (self, h , m):
            self.h += h
            self.m += m
            self.Update()

        def Update(self):
            if self.m > 59:
                hadd, self.m = divmod(self.m,60)
                self.h += hadd
            if self.h > 23:
                dadd, self.h = divmod(self.h,24)
                self.d += dadd
                self.wd += dadd
                self.wd = self.wd%7

        @property
        def GetTime (self):
            return '{}:{:02d}'.format(self.h,self.m)
        @property
        def GetDayWeekDay(self):
            return '{}, {}'.format(self.d, self.weekdays[self.wd])

default calendar = Calendar (0, 1, 8, 0)

label start:
    $ GameRunning = True
    while GameRunning:
        'qwe  '
        $ calendar.AddTime (2, 30)
        "It is [calendar.GetDayWeekDay], [calendar.GetTime]"

Psion
Regular
Posts: 25
Joined: Fri Dec 11, 2020 4:01 pm
Contact:

Re: AttributeError: 'tuple' object has no attribute

#5 Post by Psion »

_ticlock_ wrote: Fri Jan 15, 2021 3:22 pm
1) Your errors is because you use variable h in method GetTime. You need to use class attribute self.h
Thank you for whole ready for use code. Definetly gonna use it. Much better than mine. But can you tell me where exactly i should have used self.h in my code? So i can understand on example. Your code so different from mine, i dont see my mistake.

User avatar
_ticlock_
Miko-Class Veteran
Posts: 910
Joined: Mon Oct 26, 2020 5:41 pm
Contact:

Re: AttributeError: 'tuple' object has no attribute

#6 Post by _ticlock_ »

Psion wrote: Fri Jan 15, 2021 4:29 pm
_ticlock_ wrote: Fri Jan 15, 2021 3:22 pm
1) Your errors is because you use variable h in method GetTime. You need to use class attribute self.h
Thank you for whole ready for use code. Definetly gonna use it. Much better than mine. But can you tell me where exactly i should have used self.h in my code? So i can understand on example. Your code so different from mine, i dont see my mistake.
You used variable h in method GetTime, but this method does not have variable h. Here is how you could have changed your code to avoid the error:

Code: Select all

        def GetTime (self):
            if self.m > 59:
                hadd, self.m = self.m%60 # you should use hadd, self.m = divmod(self.m,60)
                self.h += hadd
            if self.h > 23:
                self.h = self.h%24
            return self.h,self.m
The code I provided is based on your code. I fixed some mistakes and made the code more organized.

PS: You should not do the same calculations in various methods. Instead, you can make another method (in my example it is method Update) and call this method whenever you need to repeat the same operations. However, in the example, you don't need to call Update in GetTime since it is already done in method AddTime.

Psion
Regular
Posts: 25
Joined: Fri Dec 11, 2020 4:01 pm
Contact:

Re: AttributeError: 'tuple' object has no attribute

#7 Post by Psion »

_ticlock_ wrote: Fri Jan 15, 2021 5:05 pm
You used variable h in method GetTime, but this method does not have variable h. Here is how you could have changed your code to avoid the error:
Ok, i understand now. I was using this clock viewtopic.php?f=51&t=21978 as base, and he didnt use self.h. Only h. Clearly he did there something different...
The code I provided is based on your code. I fixed some mistakes and made the code more organized.
For a noob like me, it was already too much :)

Anyway, thank you very much for your help. :)

Psion
Regular
Posts: 25
Joined: Fri Dec 11, 2020 4:01 pm
Contact:

Re: AttributeError: 'tuple' object has no attribute

#8 Post by Psion »

_ticlock_ wrote: Fri Jan 15, 2021 5:05 pm
Hey, can you help me again? I tried to take hour variable from class (i need to make stuff happen when specific time comes) but i again got tuple error (different one:()

Code: Select all

init python:
    class Calendar(object):
        weekdays = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday','Sunday']
        def __init__(self, wd, d, h, m):
            self.d = d
            self.h = h
            self.m = m
            self.wd = wd

        def AddTime (self, h , m):
            self.h += h
            self.m += m
            self.Update()

        def Update(self):
            if self.m > 59:
                hadd, self.m = divmod(self.m,60)
                self.h += hadd
            if self.h > 23:
                dadd, self.h = divmod(self.h,24)
                self.d += dadd
                self.wd += dadd
                self.wd = self.wd%7
            if self.d > 7:
                ndadd, self.d = divmod(self.d,8)
                self.d += ndadd

        @property
        def GetDayWeekDay(self):
            return '{}, {}'.format(self.d, self.weekdays[self.wd])
        @property
        def GetTime (self):
            return self.h, self.m

default calendar = Calendar (0, 1, 8, 0)

label start:
    $ GameRunning = True
    while GameRunning:
        'qwe  '
        $ calendar.AddTime (1, 30)
        $ h,m = calendar.GetTime()
        if h == 3:
            "Hey!"

Code: Select all

I'm sorry, but an uncaught exception occurred.

While running game code:
  File "game/clock/event.rpy", line 91, in script
    $ h,m = calendar.GetTime()
  File "game/clock/event.rpy", line 91, in <module>
    $ h,m = calendar.GetTime()
TypeError: 'tuple' object is not callable

-- Full Traceback ------------------------------------------------------------

Full traceback:
  File "game/clock/event.rpy", line 91, in script
    $ h,m = calendar.GetTime()
  File "E:\soft\renpy-7.3.5-sdk\renpy\ast.py", line 914, in execute
    renpy.python.py_exec_bytecode(self.code.bytecode, self.hide, store=self.store)
  File "E:\soft\renpy-7.3.5-sdk\renpy\python.py", line 2028, in py_exec_bytecode
    exec bytecode in globals, locals
  File "game/clock/event.rpy", line 91, in <module>
    $ h,m = calendar.GetTime()
TypeError: 'tuple' object is not callable

Windows-7-6.1.7601-SP1
Ren'Py 7.3.5.606
Chemist 1.0
Sun Jan 17 16:34:18 2021

User avatar
_ticlock_
Miko-Class Veteran
Posts: 910
Joined: Mon Oct 26, 2020 5:41 pm
Contact:

Re: AttributeError: 'tuple' object has no attribute

#9 Post by _ticlock_ »

If class method uses decorator @property, you should not use brackets when call it:

Code: Select all

$ h,m = calendar.GetTime
Or you can delete the decorator @property for the method

PS: why don't you just access the h variable directly:

Code: Select all

label start:
    $ GameRunning = True
    while GameRunning:
        'qwe  '
        $ calendar.AddTime (1, 30)
        if calendar.h == 3:
            "Hey!"

Psion
Regular
Posts: 25
Joined: Fri Dec 11, 2020 4:01 pm
Contact:

Re: AttributeError: 'tuple' object has no attribute

#10 Post by Psion »

_ticlock_ wrote: Sun Jan 17, 2021 10:08 am If class method uses decorator @property, you should not use brackets when call it:

Code: Select all

$ h,m = calendar.GetTime
Or you can delete the decorator @property for the method
Oh, tnx, didnt know that...
PS: why don't you just access the h variable directly:

Code: Select all

label start:
    $ GameRunning = True
    while GameRunning:
        'qwe  '
        $ calendar.AddTime (1, 30)
        if calendar.h == 3:
            "Hey!"
Damn... I tried it, i really did. And it didnt work ... Now it working ok...

Tnx again for your help.

Post Reply

Who is online

Users browsing this forum: No registered users