Help with program to ask words

0

I'm doing a little program to ask me French words.

Actually, I tried something but I think it's all wrong.

I want to ask:
 - How many words do you want me to ask you?

This is to set Palabra: user_input ; Traduccion: user_input as many times as the user wants.

On the other hand, I would like the user's responses to be saved and then ask them to the azár.

This is what I have until now, which is nothing because I do not know how to continue (I've started doing nothing):

print ('Cuantas palabras quieres que te pregunte?')
numeropalabras = int(input(": "))

print (numeropalabras * "Palabra: ")
palabra = str(input(numeropalabras * ": ")
print (numeropalabras * "Traduccion: ")
traduccion = str(input(numeropalabras * ": ")

Update:

According to the answers and I tried to complete my program and now I have:

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, traduccion))
    respuesta =  str(input("\nIngrese su respuesta en francés: "))
    if respuesta == "{1}":
        print("Su respuesta es correcta.")
    else:
        print("Su respuesta es incorrecta.")

I have added a little bit, so I can compare if the answer is correct or incorrect. How do you compare 2 strings in Python? He gives me the wrong time all the time.

    
asked by JuanVan12 22.11.2016 в 10:11
source

2 answers

1

Hello! First of all I would recommend you to look a bit at some python manual since it will come to you in luxury, and you will be able to advance much more.

palabras = dict()
print ('Cuantas palabras quieres que te pregunte?')
numeropalabras = int(input(": "))
i = 0
while(i < numeropalabras):
    palabraEsp = raw_input("Introduzca palabra en Esp:")
    palabraFra = raw_input("Introduzca traduccion en Frances:")
    palabras.update({palabraEsp:palabraFra})
    i= i+1

print palabras

From here on, you can make progress on your own, encourage!

Edit: If a version of Python 3.X is used. use input instead of the raw_input function.

    
answered by 22.11.2016 / 11:34
source
1

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.")
    
answered by 22.11.2016 в 11:56