I am trying to make a little program which can register a word in Spanish (I am from Spain), the meaning in English and other thing which I have called "Complicado". "Complicado" is not relevant at this point of the program. I am really confused with the behavior of the code. I don't know why \n is not working how I thought it works. Furthermore some lines are getting printed before I would like.
The problem is in the if eleccion == 2. For instance, when eleccion is equal to 2 I expected the prints would be:
Empieza el test...
Pregunta número 1
Empieza el test...Indica el equivalente castellano de la palabra: Hello (Or the other possibility)
But this is a capture of my console:

I attached below the code. I appreciate all the help possible
import json
import random
eleccion = input("¿Añadir palabras?. (1)\n¿Test?. (2)\nLista. (3)\n")
eleccion = int(eleccion)
if eleccion == 1:
esp = str(input("¿Español?: \n"))
ing = str(input("¿Inglés?: \n"))
f = open('ingles.json')
data = json.load(f)
data.append({"Esp":esp, "Ing":ing, "Complicado": 0})
with open("ingles.json", "w", encoding="utf-8", newline="") as file:
json.dump(data, file, indent=2)
f.close()
elif eleccion == 2:
#Test
aciertos = 0
preguntas = 0
print("Empieza el test...\n")
a = 1
f = open('ingles.json')
data = json.load(f)
while a != 0:
print("Pregunta número: "+str(preguntas+1) +"\n")
rand = random.randint(0,len(data)-1)
rand2 = random.choice([2, 3])
h = data[rand]
if rand2 == 2: #Preguntas el inglés dando el español
res = input("Indica el equivalente inglés de la palabra: " + h["Esp"]+ " ")
print("\n")
if res.upper() == h["Ing"].upper():
aciertos += 1
print("\nCorrecto \n")
else:
print("\nError \n")
elif rand2 == 3:
res = input("Indica el equivalente castellano de la palabra: " + h["Ing"] + " ")
print("\n")
if res.upper() == h["Esp"].upper():
aciertos += 1
print("\nCorrecto \n")
else:
print("\nError \n")
preguntas += 1
a = int(input("Para otra pregunta pulsa '1' "))
print("\n############\n")
f.close()
elif eleccion == 3:
print("Ha seleccionado leer todo el listado...\n")
f = open('ingles.json')
data = json.load(f)
for i in data:
resta = 20 - len(str(i["Esp"]))
vacio = ""
for n in range(resta):
vacio += " "
print(" " + str(i["Esp"]) + vacio +"| "+ str(i["Ing"]))
# Closing file
f.close()
One example of what could be in ingles.json is:
[ { "Esp": "Hola", "Ing": "Hello", "Complicado": 0 }, { "Esp": "Pedro", "Ing": "Peter", "Complicado": 0 }, ]