0

I'm having issues with my python code getting syntax error after adding an elif statement and I'm unable to get the reason why, no indentation issues are present in my code

# Your code here

class foodapp:

    cantidad_art = 1

    def articulosmenu(self, nombre, precio, costototal):
        self.nombre = nombre
        self.precio = precio
        self.costototal = precio * self.cantidad_art
        oracion = "Se han agregado:" + str(self.cantidad_art) + " del articulo " + str(self.nombre) + " cuyo valor es de " + str(self.precio) + ", por lo tanto el cliente debe pagar " + str(self.costototal)
        return oracion

appfood = foodapp
print("Bienvenido al App Food")
print("Menu \n 1. Hamburguesa \n 2. Pollo Frito")

articuloAComprar = input("Seleccion el articulo que quiere comprar: ")

if articuloAComprar == 1 :
 appfood.cantidad_art = eval(input("Ingrese la cantidad de articulos: "))
print(appfood.articulosmenu(appfood,"Hamburguesa", 7800, 0))

elif articuloAComprar == 2 :
 appfood.cantidad_art = eval(input("Ingrese la cantidad de articulos: "))
print(appfood.articulosmenu(appfood,"Pollo Frito", 4000, 0))

Can someone please enlighten me on where the error is? All I'm getting is

File "<string>", line 25
    elif articuloAComprar == 2 :
    ^
SyntaxError: invalid syntax

UPDATE:

After fixing some indent issues with your help I've got a new version; however, after entering the articuloAComprar input value the code just goes idle with no errors

class foodapp:

    cantidad_art = 1

    def articulosmenu(self, nombre, precio, costototal):
        self.nombre = nombre
        self.precio = precio
        self.costototal = precio * self.cantidad_art
        oracion = "Se han agregado:" + str(self.cantidad_art) + " del articulo " + str(self.nombre) + " cuyo valor es de " + str(self.precio) + ", por lo tanto el cliente debe pagar " + str(self.costototal)
        return oracion

appfood = foodapp

print("Bienvenido al App Food\n")
print("Menu \n 1. Hamburguesa \n 2. Pollo Frito")

articuloAComprar = input("Seleccion el articulo que quiere comprar: ")
if articuloAComprar == 1 :
    appfood.cantidad_art = input("Ingrese la cantidad de articulos: ")
    print(appfood.articulosmenu(appfood,"Hamburguesa", 7800, 0))

elif articuloAComprar == 2 :
    appfood.cantidad_art = input("Ingrese la cantidad de articulos: ")
    print(appfood.articulosmenu(appfood,"Pollo Frito", 4000, 0)) 
4
  • That print statement is messing with your structure. Commented Mar 4, 2022 at 1:34
  • @Mous are there any alternatives to fix it? I'm new to python and completely lost with this error. Commented Mar 4, 2022 at 1:36
  • What do you want the output to be if articuloAComprar==1, and what do you want the output to be if articuloAComprar==2 Commented Mar 4, 2022 at 1:38
  • @Mous, I'm pulling the output from oracion = "Se han agregado:" + str(self.cantidad_art) + " del articulo " + str(self.nombre) + " cuyo valor es de " + str(self.precio) + ", por lo tanto el cliente debe pagar " + str(self.costototal)return oracion from the upper class. I want the update to be Se han agregado:1 del articulo Pollo Frito cuyo valor es de 4000, por lo tanto el cliente debe pagar 4000 depending on the article they want to buy. Commented Mar 4, 2022 at 1:42

3 Answers 3

1

Python is an indentation based syntax, so unlike a langauge like c++ there's no brackets in order to contain blocks of code.

For example:

if someCondition:
    print("something")
elif otherCondition:
    print("other something")

As you can see there's nothing between the if and elif other than the indented blocks of code. This being said in your code, you don't follow this syntax:

if articuloAComprar == 1 :
 appfood.cantidad_art = eval(input("Ingrese la cantidad de articulos: "))

# The print statement below this comment is the issue.
print(appfood.articulosmenu(appfood,"Hamburguesa", 7800, 0))

elif articuloAComprar == 2 :
 appfood.cantidad_art = eval(input("Ingrese la cantidad de articulos: "))
print(appfood.articulosmenu(appfood,"Pollo Frito", 4000, 0))

In order to fix this, either indent that print statement or move it somewhere else, so that there's nothing blocking the if and elif.

tl:dr; Python uses indents to define code blocks, so when the indented code block is ended by a non-indented line of code, it does not recognize that line of code as a code block. Therefore your if and elif are not recognized as being "together" so the elif would be syntactically incorrect.

Sign up to request clarification or add additional context in comments.

Comments

0

The print statement

print(appfood.articulosmenu(appfood,"Hamburguesa", 7800, 0)

is separating the if and elif clauses of your conditional. This means that while your if clause parses properly, the elif clause isn't connected to it.

Exactly how you resolve this issue is up to you, but you need to either indent or remove the problematic print statement.

What's the intended output of your code if the condition is true or false?

1 Comment

Hi Mous, Thanks a lot for you guidance, when I remove the elif statement the code works perfectly; however, when adding the elif it breaks. Option : Se han agregado: articuloAComprarvalue del articulo Hamburguesa cuyo valor es de 7800, por lo tanto el cliente debe pagar self.costototal = precio * self.cantidad_art (grabbed from the class) Se han agregado: articuloAComprarvalue del articulo Hamburguesa cuyo valor es de 7800, por lo tanto el cliente debe pagar self.costototal = precio * self.cantidad_art( grabbed from the class)
0

You say you don't have any indentation errors, but based on the code you've posted, you absolutely do and that's the problem. Here is the fixed code:

if articuloAComprar == 1 :
    appfood.cantidad_art = input("Ingrese la cantidad de articulos: ") 
    print(appfood.articulosmenu(appfood,"Hamburguesa", 7800, 0))

elif articuloAComprar == 2 :
    appfood.cantidad_art = input("Ingrese la cantidad de articulos: ")
    print(appfood.articulosmenu(appfood,"Pollo Frito", 4000, 0))

There's a few things to note here, all regarding indentation. In python, whitespace is syntax. You need to be consistent with your indentation, even to the point of making sure not to mix tabs and spaces.

You have the same two issues with each conditional statement (the if and the elif).

if articuloAComprar == 1 :
 appfood.cantidad_art = input("Ingrese la cantidad de articulos: ")
print(appfood.articulosmenu(appfood,"Hamburguesa", 7800, 0))

elif articuloAComprar == 2 :
 appfood.cantidad_art = input("Ingrese la cantidad de articulos: ")
print(appfood.articulosmenu(appfood,"Pollo Frito", 4000, 0))

First, you only have a single space on the second line. This should be four spaces to be consistent.

Then, your print statement doesn't have any whitespace in front of it at all, so it is not a part of the if-elif statement.

Basically, you are interrupting your definition of the conditional statement with a random print() call outside of the conditional block.

If you have experience with C++ style programming languages, what you've effectively written is something like this (in pseudocode):

if (articuloAComprar == 1){
    get(input);
} print(output); else if (articuloAComprar == 2){
    get(input);
}
print(output);

UPDATE:

There is an additional problem with the code, which is that the input is not cast to a number but is compared to numbers.

Specifically, articuloAComprar will be the string "1" which is not the same as the number 1. Therefore neither condition is met. One way to fix this would be to compare to the strings "1" and "2", however you will also need to cast the input to integers when you get the quantity, otherwise you will not print the correct value.

This is because of this line: self.costototal = precio * self.cantidad_art

In the case that self.cantidad_art is set to the raw input, it will be a string, and multiplying a string in Python duplicates it. For example, 3 * "hello" = "hellohellohello". Since you'll need to cast the input to a number multiple times, I would suggest that you define a safe input-to-number function and use that instead of input. I would use something like this, which returns a zero if the input can't be parsed properly:

def get_int(message):
    try:
        return int(input(message));
    except ValueError:
        return 0

and then used as such:

articuloAComprar = get_int("Seleccion el articulo que quiere comprar: ")
if articuloAComprar == 1:
    appfood.cantidad_art = get_int("Ingrese la cantidad de articulos: ")
    print(appfood.articulosmenu(appfood,"Hamburguesa", 7800, 0))

elif articuloAComprar == 2:
    appfood.cantidad_art = get_int("Ingrese la cantidad de articulos: ")
    print(appfood.articulosmenu(appfood,"Pollo Frito", 4000, 0))
# It's good practice to check for invalid input from humans
else:
    print("Selección invalida")

2 Comments

Thanks a lot for your guidance. I'm relatively new to python, doing my 1st course at college with no guidance from the professor, I've added the suggested indentation and after that the code just goes idle after entering the value on input for articuloAComprar. No errors at all, any ideas? Thanks in advance for helping me out.
input() returns a string in Python, but you're comparing to integers. The string "1" and the number 1 are different, so neither of your conditional statements are true.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.