Python Forum
NameError: name 'edit' is not defined
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
NameError: name 'edit' is not defined
#1
I am trying to get the value from "edit", but I can't seem to get it to work.

edit = 0

def knapp_1(self):
        global edit
        
        if edit == 0:
            edit = 1
        else:
            edit = edit * 10 + 1

        self.tall_input.setPlainText(str(edit))
When i run the code, I get the error:
File "c:\Users\Albert\Desktop\retarded\kalkulator_gui.py", line 67, in knapp_1
if edit == 0:
NameError: name 'edit' is not defined

Is there something other than "global" that I can use to get the value?
Reply
#2
Yes, you can pass the value as a parameter. And since it looks like you want to change the value outside of the function, you would return the value. It looks like your function is part of a class, yes? So it would like this:

def knapp_1(self, edit):   # edit as a parameter
         
        if edit == 0:
            edit = 1
        else:
            edit = edit * 10 + 1
 
        self.tall_input.setPlainText(str(edit))
        return edit   # return the modified value
Then when you called it, it would look like this:

edit = self.knapp_1(edit)
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
Or, duh, since this is part of a class, you could just make edit an attribute of the class. So you would use self.edit everywhere instead of edit, and you wouldn't need to pass it as a parameter or return it.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#4
(Dec-17-2019, 04:03 PM)ichabod801 Wrote: Yes, you can pass the value as a parameter. And since it looks like you want to change the value outside of the function, you would return the value. It looks like your function is part of a class, yes? So it would like this:

def knapp_1(self, edit):   # edit as a parameter
         
        if edit == 0:
            edit = 1
        else:
            edit = edit * 10 + 1
 
        self.tall_input.setPlainText(str(edit))
        return edit   # return the modified value
Then when you called it, it would look like this:

edit = self.knapp_1(edit)

Thank you for the help, but i have found a new problem:

else:
    edit = edit * 10 + 1
In this part "edit" doesn't multiply by 10 as i should. If I chang out the "edit * 10" part with "1 * 10", it works as it should, but i have no idea what to do. If you have any suggestions, it would be much appreciated.
Reply
#5
That code looks fine, so I think your error is something else. Repost your current code, specify how you are calling the method, and what inputs you are using.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#6
(Dec-17-2019, 04:47 PM)ichabod801 Wrote: That code looks fine, so I think your error is something else. Repost your current code, specify how you are calling the method, and what inputs you are using.

Sorry, totally missed your second reply. That fixed the problem. Thank you so much!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  I'm getting a NameError: ...not defined. vonArre 2 283 Mar-24-2024, 10:25 PM
Last Post: vonArre
  Getting NameError for a function that is defined JonWayn 2 1,107 Dec-11-2022, 01:53 PM
Last Post: JonWayn
Question Help with function - encryption - messages - NameError: name 'message' is not defined MrKnd94 4 2,891 Nov-11-2022, 09:03 PM
Last Post: deanhystad
  [split] NameError: name 'csvwriter' is not defined. Did you mean: 'writer'? cathy12 4 3,326 Sep-01-2022, 07:41 PM
Last Post: deanhystad
  NameError: name ‘app_ctrl’ is not defined 3lnyn0 0 1,515 Jul-04-2022, 08:08 PM
Last Post: 3lnyn0
  NameError: name 'hash_value_x_t' is not defined Anldra12 5 1,921 May-13-2022, 03:37 PM
Last Post: deanhystad
  NameError: name 'cross_validation' is not defined tmhsa 6 13,352 Jan-17-2022, 09:53 PM
Last Post: TropicalHeat
  NameError: name “x” is not defined ... even though x is defined campjaybellson 7 14,981 Oct-20-2021, 05:39 PM
Last Post: deanhystad
  NameError: name 'Particle' is not defined in Pygame drunkenneo 4 3,389 Aug-15-2021, 06:12 PM
Last Post: bowlofred
  NameError: name 'u1' is not defined (on parser code Python) Melcu54 1 2,888 Jul-26-2021, 04:36 PM
Last Post: snippsat

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020