Python Forum

Full Version: Regular expressions help re.error: multiple repeat at position 23
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
This is a menu for someone to de-codify a food menu and know what the client has asked for. The issue is that it only gives back the answer correctly to the code: "RES71PQ". If I introduce the other testings I have to do, which are "RED4", "QEGRVN" and "TCAGR" it gives a big message but I don't understand it. Help! This is a really important homework. Thanks

import re

    def sabor1(pedido):
      if pedido[2]=="S":
        return "salado"
    else:
        return "dulce"

    def sabor2(pedido):
       if pedido[5]=="N":
          return "Vainilla"
       elif pedido[5]=="R":
          return "Fresa-chocolate"
       elif pedido[5]=="M":
          return "Caramelo"
    else:
        return "Chocolate"

def articulo(pedido):
    if pedido[3]==1 or pedido[3]==2 or pedido[3]==4 or pedido[3]==5:
        return "un"
    else:
        return "una"

def tamano(pedido):
    if pedido[4]=="P"or pedido[5]=="P":
        return "de tamaño pequeño"
    elif pedido[4]=="M"or pedido[5]=="M":
        return "de tamaño mediano"
    else:
        return "de tamaño grande"

def tamano2(pedido):
    if pedido[2]=="P":
        return "de tamaño: pequeño"
    else:
        return "de tamaño: grande"


def comida(pedido):
    if int(pedido[3])==1:
        return "Nidito"
    elif int(pedido[3])==2:
        return "Palito de queso"
    elif int(pedido[3])==3:
        return "Orejita"
    elif int(pedido[3])==4:
        return "Biscuit"
    elif int(pedido[3])==5:
        return "Crocante"
    elif int(pedido[3])==6:
        return "Enchilada"
    elif int(pedido[3])==7:
        return "Empanada"

def verificarPedidos(pedido):
   """
   Funcionaliad: Verifica el código que ingresó el usuario y devuelve el producto
   Entrada: Un código alfanumérico 
   Salida: El producto correspondiente al código anterior
   """
   if re.match ("^RE[D|S]{1}[1-6]{1}[PQ|MD|GD]*$",pedido):
        print ("Usted solicita una repostería de sabor "+sabor1(str(pedido))+", correspondiente a "+articulo(str(pedido))+" :"+comida(str(pedido))+","+tamano(str(pedido)))
   elif re.match ("^RE[D|S]{1}[1-6]{1}$",pedido):
        print ("Usted solicita una repostería de sabor "+sabor1(str(pedido))+", correspondiente a "+articulo(str(pedido))+" :"+comida(str(pedido))+".")
   elif re.match ("^RE[D|S]{1}7{1}[1|2]{1}[PQ|MD|GD]*$",pedido):
        print ("Usted solicita una repostería de sabor "+sabor1(str(pedido))+", correspondiente a "+articulo(str(pedido))+" : Empanada, "+tamano(str(pedido)))
   elif re.match ("^RE[D|S]{1}7{1}[1|2]{1}*$",pedido):
        print ("Usted solicita una repostería de sabor "+sabor1(str(pedido))+", correspondiente a "+articulo(str(pedido))+" : Empanada, ")
   elif re.match ("^QE{1}[GR|PQ]{1}[VN|FR|CM|CE]{1}$",pedido):
        print ("Usted solicita un queque de sabor de "+sabor2(str(pedido))+", "+tamano2(str(pedido)))
   elif re.match ("^TCAGR{1}$",pedido):
        print ("Usted solicita una torta chilena, de tamaño: grande. ")


        #Menu

def opcionverificarPedidos():
   """
   Funcionalidad: Validar y reconocer cual producto se desea 
   Entrada: Código 
   Salida: Qué es lo que el cliente pide 
   """
   print ("\n------------------------\n")
   print ("Según el menú mostrado anteriormente, eliga su pedido según corresponda el código de la comida con su pedido")
   print ("\n------------------------\n")
   pedido= input("Ingrese un código de comida: ")
   print ("")
   print ("")
   print (verificarPedidos(pedido))
   print ("")
   print ("")


def menuSpoon():
    print ("\n**************************\n")
    print ("≣≣≣≣≣≣≣≣ Menú Spoon™ ≣≣≣≣≣≣≣≣≣")
    print ("\n"*1)
    print ("Bienvenido al menú decodificador. Esperamos tenga una experiencia agradable. ")
    print ("\n**************************\n")
    print ("Los productos disponibles son:")
    print ("1. Repostería:")
    print ("2. Queques:")
    print ("3. Tortas Chilenas grandes ")   
    print ("")
    pedido=(input("Escoja un alimento y digite su código: "))
    if pedido!="":
       print(verificarPedidos(pedido))
       print("")
       print("")
    else:
        print ("El código ingresado no coincide con el inventario actual de productos Spoon™. Inténtelo nuevamente tomando en cuenta el orden en el que ingresa los datos. ")
    continuar=input("Desea decodificar un nuevo producto? ")

    if continuar=="Si" or continuar=="SI" or continuar=="sí" or continuar=="SÍ" or continuar=="si" or continuar=="Sí":
        print("")
        print("")

        menuSpoon()
    else:
        return


menuSpoon()

This is a really important homework pls help!
Sad
We need the big message you're getting. All of it. Exactly.

And is that the actual indentation of your file? Because if it is, that's your problem.
If they did not teach to name variables in English...

Variable names are part of function documentation, helping your reader to understand the code. Portuguese - you are discouraging your potential helpers from the international community.

Also, your code is too crowded, make it still less readable - see PEP-8 for guidance. And don't show the full code - show just places that don't work (again, helps your potential helpers to concentrate on your problem - and not on long strings most won't be able to read)

Still:
  • Use this site to practice RE and RT(F)M for guidance. Try to figure out the common pattern - that is the purpose of RE, not endless if/elif/else chain
  • re.match searches from the beginning of the string; ^ symbol is redundant.
  • {1} is a redundant and stupid quantity modifier; it actually says "this string (sub-RE) will appear once"; if some string(sub-RE) shows within RE without quantity modifier - it means that you expect it to show once
    E.g, TCAGR{1} means that you expect string TCAG, followed by one R (actually, string comparison will do). You crowd your RE with meaningless modifiers, making them still harder to read

Couple of generic pointers:
  • pedido[3]==1 or pedido[3]==2 or pedido[3]==4 or pedido[3]==5
    may be rewritten (take your pick)
    pedido[3] in [1, 2, 3, 4, 5]
    1 <= pedido[3] <= 5
    pedido[3] in range(1, 6)
  • Most of your choice function are candidates for dict
  • int(pedido[3]) == 1 or pedido[3] == '1'?
  • Indents - well, you were already told