Python Forum
Regular expressions help re.error: multiple repeat at position 23
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Regular expressions help re.error: multiple repeat at position 23
#1
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
Reply
#2
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.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
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
Test everything in a Python shell (iPython, Azure Notebook, etc.)
  • Someone gave you an advice you liked? Test it - maybe the advice was actually bad.
  • Someone gave you an advice you think is bad? Test it before arguing - maybe it was good.
  • You posted a claim that something you did not test works? Be prepared to eat your hat.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Recursive regular expressions in Python risu252 2 1,258 Jul-25-2023, 12:59 PM
Last Post: risu252
  Why do I have to repeat items in list slices in order to make this work? Pythonica 7 1,354 May-22-2023, 10:39 PM
Last Post: ICanIBB
Sad Regular Expressions - so close yet so far bigpapa 5 966 May-03-2023, 08:18 AM
Last Post: bowlofred
  Repeat request by else stsxbel 2 1,182 Jul-30-2022, 03:34 PM
Last Post: stsxbel
  How to move multiple columns to initial position SriRajesh 4 1,439 Jul-02-2022, 10:34 AM
Last Post: deanhystad
  List Creation and Position of Continue Statement In Regular Expression Code new_coder_231013 3 1,675 Jun-15-2022, 12:00 PM
Last Post: new_coder_231013
  get out of while loop and stop repeat Frankduc 11 2,995 Apr-26-2022, 10:09 PM
Last Post: deanhystad
  Avoid multiple repeat in indent Frankduc 8 2,906 Jan-18-2022, 05:46 PM
Last Post: Frankduc
  python error: bad character range \|-t at position 12 faustineaiden 0 3,698 May-28-2021, 09:38 AM
Last Post: faustineaiden
  Having trouble with regular expressions mikla 3 2,592 Mar-16-2021, 03:44 PM
Last Post: bowlofred

Forum Jump:

User Panel Messages

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