samedi 6 août 2022

How to print each item in the dictionary randomly without repeating? [duplicate]

How could I print each question from the dictionary randomly without repeating?

Every time I run this command, I get repeated questions.

import random

capitals = {
    "Canada": "Toronto",
    "Spain": "Madrid",
    "Germany": "Berlin",
    "Belgium": "Brussels",
    "Australia": "Sydney",
}

for country, capital in capitals.items():
    country = random.choice(list(capitals.keys()))

    capital = capitals[country]
    question = input(f"What is the capital of {country}? : ")
    if question == capital:
        print("Congratulations!\n")

    else:
        print(f"Wrong! The correct answer: {capital}\n")



Aucun commentaire:

Enregistrer un commentaire