0

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?

5
  • Careful with ` if user_input == I:` . Maybe you mean i instead of I? Commented Nov 30, 2021 at 19:47
  • Grammarly corrected that but I know it should be lower case :) Thanks for letting me know XD @RobertoT Commented Nov 30, 2021 at 19:48
  • What is 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. Commented Nov 30, 2021 at 19:49
  • try printing the values of both 'i' and 'user_input' and see what you get Commented Nov 30, 2021 at 19:55
  • Check answer for @Tim Robert. It is much faster with an only dictionary. Your code was saying always correct because you were interating over the whole set of countries and capitals, so you were always guessing the right answer even if you replied with other capital. With dictionary is faster and you avoid that. Your programm was asking always for the same capital because you select the item outside the loop. Commented Nov 30, 2021 at 20:07

2 Answers 2

2

This does what you ask:

import random

country_names = ['Germany', 'Italy', 'Netherlands', 'France', 'Albania', 'Canada'] 
capital_list = ['Berlin', 'Rome', 'Amsterdam', 'Paris', 'Tirana', 'Ottawa'] 

dictionary = dict(zip(country_names, capital_list))

score = 0

while True:
    item = random.choice(list(country_names))
    print(item)

    user_input = input('Name ' + item + "'s capital:")

    if dictionary[item] == user_input:
        print("Correct")
        score = score + 1
    else:
        print("Incorrect")
Sign up to request clarification or add additional context in comments.

Comments

1

Multiple problems here. Most importantly, you have

    answer = False
    for i in capital_list:
        if user_input == i:
            answer = True

This code just looks at each capital in the list of capitals and returns True if what the user typed is in that list. You make no effort to see if it is the right country or not.

A better data structure might be a dictionary, in which the keys are the countries, and the value is the capital:

dictionary_list = [ country: capital for country, capital in zip(...)]

then you can write

item = random.choice(list(country_names))
expected_answer = dictionary_list[item]

and the only correct answer is expected_answer

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.