variable is not saved in programming

Asked

Viewed 56 times

-1

I’m learning python and I’m trying to program a platform to play table rpg

my intention was to assemble a code that would take the name of a character that would be placed in an input and add it into a variable, so that this variable would be used as the player’s 'name'. The problem is that my code does not 'save' this variable.

i am using a tool called Pysimplegui to assemble a graphical interface

import PySimpleGUI as sg 
from random import randint

class ScreenRecord():
def __init__(self):
    
    layoutRecord = [
        [sg.Text('Enter character name'),sg.Input(size=(15,0),key='character')],
        [sg.Button('Enter',key = 'Enter'),]
    ]

    self.window = sg.Window('teste-mecanica').layout(layoutRecord)
    self.button,self.values = self.window.Read()

    def Enter(event):
        character = self.values['character']
        tela = ScreenDice()
    
    while True :
        event,values = self.window.Read()
        if event is None:
            break
        if event in ['Enter']:
            Enter(event)

class ScreenDice(ScreenRecord):
def __init__(self):
    
    db = {'size':(12,2)}

    layoutDice = [
        
        [sg.Button('Roll D10',key= 'd10',**db),sg.Button('Roll D20',key='d20',**db)],
        
    ]
    self.window = sg.Window('teste-dado').layout(layoutDice)

    self.button,self.values = self.window.Read()

    def RollDice(event):
        print(character ,'received', randint(1,dice),'in the dice')
            

    while True :
        event , values = self.window.Read()
        if event is None:
            break
        if event in ['d10'] :
            dice = 10
            RollDice(event)
        if event in ['d20'] :
            dice = 20
            RollDice(event)

the terminal returns me :

NameError: name 'character' is not defined

Obs: it’s my first time programming something, forgive me if it’s a stupid question

2 answers

0

That one Character is not defined. It’s just a loose word.

as if I wanted to access something that doesn’t exist.

example of error.

Python 3.8.3 (default, Jun 16 2020, 17:59:42) 
[GCC 7.5.0] on linux
Type "help", "copyright", "credits" or "license" for more information.

>>> print(oi)  <==== tentando imprimir a palavra solta sem referencia  

Traceback (Most recent call last): File "", line 1, in Nameerror: name 'hi' is not defined

Here’s your list.

DESCULPE A FALTA DE ATENÇÃO.

in class class Screenrecord(): to a variable within a function

def Enter(Event): Character = self.values['Character']

And the other class that inherits from Screenrecord but is not calling the Character variable the right way:

inheritance

  • but the line character = self.values['character'] should not solve the problem ?

  • in Traceback went bad, put a break point at the beginning of the code and go through line by line, and see where the error occurs if pq until the result is that the variable is not declared and as it was consulted any word.

0

your class Screendice(Screenrecord) is not referencing the object of the main class and because of this detail the "Character" is not defined as it was not called as expected

  • the correct form should be : class Screendice(Screenrecord): def init(self): Screenrecord.init(self) ? because I keep getting the same problem

Browser other questions tagged

You are not signed in. Login or sign up in order to post.