I have 2 lists containing countries and their capitals, and I assigned them to each other using the zip function. My program randomizes a country from the list and I ask the user to type in the capital in whichever country is shown.
import random
x = random.randrange(5)
country_names = ['Germany', 'Italy', 'Netherlands', 'France', 'Albania', 'Canada']
capital_list = ['Berlin', 'Rome', 'Amsterdam', 'Paris', 'Tirana', 'Ottawa']
dictionary_list = [ {"COUNTRY": country, "CAPITAL": capital,} for country, capital in
zip(country_names, capital_list, )]
item = random.choice(list(country_names))
print(item)
score = 0
while True:
user_input = input('Name ' + item + "'s" + ' Capital:')
user_input = str(user_input)
answer = False
for i in capital_list:
if user_input == i:
answer = True
if answer:
print("Correct")
score = score + 1
else:
print("Incorrect")
Here is an example output:
Albania
Name Albania's Capital: Tirana
Correct
Name Albania's Capital:
It is true that Albania's capital is Tirana, but the problem is that my program will always say 'correct' even if I give it the wrong capital.
How can my while loop actually check my dictionary_list and give a correct or incorrect answer? And how can I repeat the process without my program saying 'Name Albania's Capital' again?
I? You need to show us real code. But before that you need to reduce it to a few lines that don't make sense to you. Or rather than reduce, write a new little snippet of code to test your understanding. See How to Ask and how to create a minimal reproducible example.