I wanted to implement a class which has a GUI and a method. The method generates an identifier when the user enters a certain vehicle license plate, so that said identifier is personal and different for each of the entered license plates. The class manages the entry of vehicles. The date it entered, its license plate and its owner. The owner of the car will be identified by his ID. When a car enters, an identifier is generated for that car
import string
import random
import easygui as eg
class Taller:
def __init__(self):
self.size = size
self.chars = chars
def id_generator(self, size = 6, chars = string.ascii_uppercase + string.digits):
return ''.join(random.choice(chars) for _ in range(size))
campos = ['Nombre', "DNI", 'Correo', 'Teléfono', "Matrícula"]
datos = []
datos = eg.multenterbox(msg ='Taller', title ='Entrada de datos', fields = campos, values = ())
cadena = ''
if datos != None:
for a, b in zip(campos, datos):
cadena = cadena + a + ': ' + b + '\n'
eg.msgbox(cadena, 'Entrada de datos', ok_button = 'Finalizar')
# devuelve una cadena de 10 digitos entre letras mayusculas y numeros
print (id_generator(10))
# devuelve una cadena de 8 numeros
print (id_generator(8,string.digits))
# devuelve una cadena de 8 caracteres en minucula
print (id_generator(8,string.ascii_lowercase))
# devuelve una cadena de 3 digitos entre el 1 y el 5
print (id_generator(3,"12345"))
Doing a bit of debugging on your code, I've tried the following:
And the result was the following:
Giving me to understand that at some point the parameter
size
is string instead of integer.When using the function
int()
to convert from string to integer, the error was half solved:Because I followed the dilemma that size was sometimes string.
Then I realized that the problem was caused by the parameter
self
, indeed after removing it I didn't get any errors:This is because your function takes parameter
self
as the first parameter assigning size as the second and chars as the third:So your function should be as follows:
And then your code like this: