autoincrement in python3

-1

I am a beginner in python3, I have the following code that I am doing and paneas I go in the second option

def menu():
print ('Selecciona una opcion')
print ('1. Ver')
print ('2. Agregar')
print ('3. Eliminar')
print ('4. Modificar')
articulos ={'items':'id':0,'nombre':'elemento1','precio':12000,'cantidad':50},'id':1,'nombre':'elemento2','precio':12000,'cantidad':50}]}
arti = articulos.get('items')
menu()

opcion = input('>>')
 if opcion == '1':
 print ('Items del inventario')
 print (arti)
elif opcion == '2':
print ('Agregar item')
#Aqui el problema
igg = arti.append('items{id}') + int(1)
#Aqui el problema
nombre2=input('digite el nombre >>')
precio2=input('digite el precio >>')
cantidad2=input('digite la cantitdad actual >>')
arti.append({'id':igg,'nombre':nombre2,'precio':precio2,'cantitdad':cantidad2})
print (arti)
elif opcion == '3':
print ('Hola3')
elif opcion == '4':
print ('Hola4')  
else:
print ('No has pulsado el rango')

What I want is to take the last id of the list and add one, which would be the new id, but I do not know how to do that part of choosing the last number of the last id, please help!

Thanks !!

    
asked by Diego Monsalve 14.04.2018 в 03:02
source

2 answers

0

Neither am I an expert, but I order things a little according to what I think is more visually clear, I leave it in option 2, the rest I leave it without programming, I hope it helps you.

#listas para cada dato del diccionario.
itm=[]
nom=[]
pre=[]
cant=[]
#diccionario
articulos = {'item:':itm,'nombre':nom ,'precio':pre,'cantidad':cant}
def menu(dic_art,itm,nom,pre,cant):
    opcion = 0
    print ('Selecciona una opcion')
    print ('1. Ver')
    print ('2. Agregar')
    print ('3. Eliminar')
    print ('4. Modificar')
    while opcion != 1 | opcion != 2 | opcion != 3 | opcion != 4:

        try:
            opcion=int(input(":"))
            if opcion == 1:
                print('los items del inventario son:')
                print(dic_art['nombre'])
            elif opcion == 2:
                #puedes necesitar limitar las entradas solo a números
                #en los casos que corresponda
                itm.append(len(itm)+1) # aquí está el tema del id que necesitabas
                nom.append(input('ingrese el nombre: ' ))
                pre.append(input('ingrese el precio: '))
                cant.append(input('ingrese la cantidad: '))
                articulos = {'item:':itm,'nombre':nom ,'precio':pre,'cantidad':cant}

        except:
                print ('1. Ver')
                print ('2. Agregar')
                print ('3. Eliminar')
                print ('4. Modificar')
        fin = input('1 para otra consulta o cualquier tecla para salir ')
        try :
            a=int(fin)
            if a ==1:
                menu(dic_art,itm,nom,pre,cant)
            else:
                break
        except:
            break


menu(articulos,itm,nom,pre,cant)
    
answered by 17.04.2018 в 20:46
0

Although I recommend you take a look at sqlalchemy, pandas and databases what you are looking for seems to be a list of data or a dictionary list. I give you an example that also includes more readable checks.

def inputdigit(texto) -> int:
    digit = input(texto)
    while not digit.isdigit():
        print('Debe introducir un número valido')
        digit = input(texto)
    return int(digit)

def menu():
    numero = True
    while numero:
        select = input(
            'Selecciona una opcion\r\n'
            '1. Ver\r\n'
            '2. Agregar\r\n'
            '3. Eliminar\r\n'
            '4. Modificar\r\n'
            '0. Salir\r\n'
            '>> '
           )
        if select not in '01234':
            print('Por favor debe marcar un número del 1 al 4 o 0 para salir')
        else:
            numero = False
    return int(select)


articulos = []


opcion = menu()
while opcion > 0:

    if opcion == 1:
        for index in range(len(articulos)):
            print(index, articulos[index])

        if len(articulos) == 0:
            print('No hay articulos')

    if opcion == 2:
        articulo = {
            'nombre': input('Nombre del artículo\r\n>>'),
            'precio': inputdigit('precio del articulo\r\n>>'),
            'cantidad': inputdigit('Cantidad del articulo\r\n>>')
        }
        articulos.append(articulo)

    if opcion == 3:
        # codigo para borrar el articulo
        pass

    if opcion == 4:
        # codigo para modificar el articulo
        pass

    opcion = menu()

print('El programa ha finalizado')

Being a list you can access its elements and work with them by index

    
answered by 24.04.2018 в 10:44