ola I am having difficulty in knowing how to link a question with a response of 2 separate lists (a list of questions and one of answers):

Asked

Viewed 60 times

1

I need to make a program similar to the application "Anki", where the person will put a word in English and soon after its translation.

The program must read all questions and choose one randomly to be returned to the user, the person must answer and the program must tell whether it is right or wrong. If he’s wrong he should return the correct answer.

The problem is that I cannot link the question with the right answer so that I can make the program read whether it is right or not. I’d like some help with that.

Follow what I’ve done so far:

import random
R = []
P = []
i = 0
num = int(input("informe quantas cartas serão digitadas:"))
for i in range(num):
    p = str(input("digite a palavra número {0}".format(i+1)))
    r = str(input("digite a resposta número {0}".format(i+1)))
    i = i + 1
    P.append(p)
    R.append(r)
print("serão sorteadas as cartas digitadas no bloco de notas e você deverá colocar a tradução")
print("para iniciar aperte a tecla ENTER")
input()
print(random.choice(P))

2 answers

4

As the question and the answer are in separate lists, when drawing a question it is difficult to find the corresponding answer. There are several ways, I’ll mention a few:

  • Instead of using random.choice() to draw a question, use random.randrange() to draw a numerical index - so you can use it in the two lists:

     idx = random.randrange(len(P))
     pergunta = P[idx]
     resposta = R[idx]
    
  • An idea that seems cleaner to me would be to use only a list - Put the question and answer in the same item of the same list:

     P.append((p, r))
    

    ... next, random.choice() would return both at the same time:

     pergunta, resposta = random.choice(P)
    

0


I thought of a simple way to meet your requirements. Take a look at Python dictionaries, they are widely used and are quite versatile. I refactored your code and came to a conclusion that worked perfectly:

import random

cartas = dict()
i = 0
num = int(input("informe quantas cartas serão digitadas:"))
for i in range(num):
    p = str(input("digite a palavra número {0}: ".format(i+1)))
    r = str(input("digite a resposta número {0}: ".format(i+1)))
    cartas[p] = r

print("serão sorteadas as cartas digitadas no bloco de notas e você deverá colocar a tradução")
print("para iniciar aperte a tecla ENTER")
input()

carta_da_vez = random.choice(list(cartas))
print(carta_da_vez)
traducao = input("Qual a tradução da palavra acima?\n")
if traducao == cartas[carta_da_vez]:
    print("Parabéns! Você acertou!")
else:
    print(f"Que pena, a resposta certa era: {cartas[carta_da_vez]}!")

Realize that inside the for, after reading the questions and answers, a key is created in the dictionary cards, that key being the word, and for that key is designated a value, that would be the answer.

It is worth remembering that the for in Python traverses the elements of a data structure, not just increments a variable. Therefore, it is not necessary to i = i + 1.

At the end, a word is chosen by random.choice(). Because this word is the key to a dictionary, we have access to its value. Verification is done through the value of the selected key.

Below is a link to documentation about dictionaries: https://docs.python.org/3/tutorial/datastructures.html#Dictionaries

I hope I’ve helped!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.