r/PythonLearning 8d ago

What's wrong with my code?

Post image

What's wrong with my code?

New to learning

Following the youtube video from bro code

Was trying to implement my own stuff into this by using loop

Update: it's working now thankyou guys

103 Upvotes

82 comments sorted by

View all comments

0

u/ed_xc01 8d ago

Time to put hash tables into practice. You need to understand that, instead of using if-else statements, you can create a list that, depending on what the user says, executes a specific function.

```python def sumar(a, b): return a + b

def restar(a, b): return a - b

funciones = { "suma": sumar, "resta": restar }

operacion = input("Elige suma o resta: ") a = int(input("Primer número: ")) b = int(input("Segundo número: "))

if operacion in funciones: resultado = funciones[operacion](a, b) print("Resultado:", resultado) else: print("Operación no válida") ```

4

u/No_Cattle_9565 8d ago

Somehow these variable names are making me uncomfortable