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 "", line 25 elif articuloAComprar == 2 : ^ SyntaxError: invalid syntax
Thanks in advance
CodePudding user response:
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.
CodePudding user response:
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?
CodePudding user response:
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 = 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))
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 = 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))
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);
CodePudding user response:
there are a lot of issues with your code - particularly with indentation and inconsistent brackets. For example:
print(appfood.articulosmenu(appfood,"Hamburguesa", 7800, 0)
print(appfood.articulosmenu(appfood,"Pollo Frito", 4000, 0)
are missing a closing bracket.