As you only learn by programming and with headaches I do not want to give it to you but to give you some ideas so I'll show you just how to ask for the words and tag them in a diccionario .
Broadly a diccionario in Python is a data structure that allows you to save a unordered set of key-value pairs, taking into account that the keys are unique within the same dictionary. It closely resembles the dictionary of words and definitions of a language.
The simple way to create a dictionary is to use {} and to put the key-value pairs separated by a comma. Each key-value pair is separated by two points. An example would be:
mi_diccionario = {'perro':'animal', 'limonero':'planta'}
To add new pairs is done like this:
mi_dicionario[clave] = valor
If this is more or less clear, in your case what we would do is capture with a input() the words to enter, we declare an empty dictionary and within a for we ask for each word and its translation and we add it to the dictionary:
diccionario = {} #diccionario vacio, podria declarase también como diccionario = dict()
numeropalabras = int(input("¿Cuantas palabras quieres que te pregunte?: "))
for i in range(numeropalabras):
palabra = input("\nIngrese su palabra en español: ")
traduccion = input("Ingrese la traducción al francés de {0}: ".format(palabra))
diccionario[palabra] = traduccion
There are many ways to access the dictionary, one of them is to use the .items() method that returns tuples with couples (clave, valor)
For example:
diccionario = {}
numeropalabras = int(input("¿Cuantas palabras quieres que te pregunte?: "))
for i in range(numeropalabras):
palabra = input("\nIngrese su palabra en español: ")
traduccion = input("Ingrese la traducción al francés de {0}: ".format(palabra))
diccionario[palabra] = traduccion
for palabra, traduccion in diccionario.items():
print('La traducción de "{0}" es "{1}."'.format(palabra, traduccion))
I use the .format() method to format the% chains print() , if you want to see how it works.
With this you should be able to follow your.
Update:
To compare two simplemte string you need to do:
if string1 == strig2:
The problem is that you got confused when using {1} . This is to refer to the index of the tuple of words taken by the string method .format() . That is, it is used only with that method. You simply need to change:
if respuesta == "{1}":
by:
if respuesta == traduccion:
Other notes:
-
input() always returns a string so it is not necessary to cast with str()
-
On line print('\nCuál es la traducción "{0}"?'.format(palabra, traduccion)) just want to print variable palabra you do not need to pass format() variable traduccion just:
print('\nCuál es la traducción "{0}"?'.format(palabra))
Your code would be like this:
diccionario = {}
numeropalabras = int(input("¿Cuantas palabras quieres que te pregunte?: "))
for i in range(numeropalabras):
palabra = str(input("\nIngrese su palabra en español: "))
traduccion = str(input("Ingrese la traducción al francés de {0}: ".format(palabra)))
diccionario[palabra] = traduccion
for palabra, traduccion in diccionario.items():
print('\nCuál es la traducción "{0}"?'.format(palabra))
respuesta = input("\nIngrese su respuesta en francés: ")
if respuesta == traduccion:
print("Su respuesta es correcta.")
else:
print("Su respuesta es incorrecta.")