How to get program to ask for input for either one or two answer depending on answers list has

Viewed 32

I made a quizz program where I make a dictionary out of a .txt file that has spanish and english words and have the program ask the equivalent for the spanish word in english.

some answers in my .txt file has spanish 2 answers for one english word.

library:la biblioteca
school:el colegio,la escuela
restaurant:el restaurante
movie theater:el cine
airport:el aeropuerto
museum:el museo
park:el parque
university:la universidad
office:la oficina,el despacho
house:la casa

example "school:el colegio,la escuela" Right now my program recognizez this and prints out "Enter 1 equivalent... Word[1]" twice but I want the output to be: (bold is input from user)

English word: museum 
Enter 1 equivalent Spanish word(s). Word [1]: **el museo** 
Correct!
---

English word: school 
Enter 2 equivalent Spanish word(s). Word [1]: **el colegio** 
Word [2]: **la escuela**
Correct!
rounds = int(input("How many words would you like to be quizzed on? "))
newdict = {key: db[key] for key in keys[:rounds]}
for keyword in keys[:rounds]:

    numberOfCorrectAnswers = len(db.get(keyword))
    spanishList = []
    print(keyword)
    for i in range(numberOfCorrectAnswers):
        spanishList.append(input("Enter 1 equivalent Spanish word(s). Word [1]: "))
    if check_quizWords(keyword, newdict, spanishList):
        correct
        print("Correct! \n")
        print("---")
        corrCount += 1
    print(" ")

How do I implement "Word [2]" for when it is asking for two answers from my dictionary and refrain from it whenever it only has only has in the list. If you need to see the dictionary or some other part of my code let me know. Sorry for bad english

1 Answers

Use string formatting (f strings in my example). See in docs

Add this in place of for loop

print(f"Enter {numberOfCorrectAnswers} equivalent Spanish word(s)")
for i in range(numberOfCorrectAnswers):
        spanishList.append(input(f"Word [{i}]: "))
Related