Ricky
Ricky

Reputation: 13

lists of list and random choice

The question I need help with is:

Write a program that stores the names of ten countries in column1 and their capitals in column2. The program should then pick a random country and ask the user for the capital.

Display an appropriate message to the user to show whether they are right or wrong.

So far I have

column1 = []
column2 = []

listoflist = [(column1)(column2)]
maxlength = 10

while len (column1) < maxlength:
    country = input("please enter a country: ")
    capital = input("please enter the capital of the country entered: ")
    column1.append(country)
    column2.append(capital)

for item in done:
    print (item[0],item[1])

if anyone can help please.

Upvotes: 0

Views: 667

Answers (4)

jwright0716
jwright0716

Reputation: 1

A list of lists can work, but a dictionary with key, value pairs works great for this.

In keeping with your original theme of user input, the logic works out well and you can use the random.choice function to pick your country while keeping track.

import random
data = {}
maxlength = 10

for _ in range(maxlength):
    country = input("please enter a country: ")
    capital = input("please enter the capital of the country entered: ")

    # using a dict, keeps things simple
    data[country] = capital

countries = list(data.keys())

picked = []
while len(picked) < maxlength:
    country = random.choice(countries)
    if country in picked:
        continue
    print("Capital for the country {0}?".format(country))
    user_said_capital_was = input("What do you think? ")
    if user_said_capital_was == data[country]:
        print("Correct!")
        picked.append(country)
    else: 
        print("Incorrect!")

Upvotes: 0

user4435153
user4435153

Reputation:

import random
dict1 ={"Turkey":"Istanbul","Canada":"Ottawa","China":"Beijing"}
list1=[key for key in dict1.keys()]
try:
    q=random.choice(list1)
except:
    print ("We are out of countries, sorry!")

while True:
    user=input("What is the capital city of {} ?: ".format(q))
    if user == dict1[q]:
        print ("Correct")
        list1.remove(q) #removing first choice so same country never asking again
        try:
            q=random.choice(list1)
        except:
            print ("We are out of countries,sorry!")
            break
    else:
        print ("Not correct")

Using dict and key-value system and list comprehension.

Upvotes: 0

RutledgePaulV
RutledgePaulV

Reputation: 2616

I believe your list of list setup is a little off for what you intend. Try something like this:

from random import shuffle
data = []
maxlength = 10

while len (data) < maxlength:
    country = input("please enter a country: ")
    capital = input("please enter the capital of the country entered: ")

    # for this scenario, probably better to keep rows together instead of columns.
    data.append((country, capital)) # using a tuple here. It's like an array, but immutable.

# this will make them come out in a random order!
shuffle(data)

for i in range(maxlength):
    country = data[i][0]
    capital = data[i][1]
    print("Capital for the country {0}?".format(country))
    user_said_capital_was = input("What do you think?")
    if user_said_capital_was == capital:
        print("Correct!")
    else: 
        print("Incorrect!")

Upvotes: 1

Simeon Visser
Simeon Visser

Reputation: 122516

You should write this as:

listoflist = [column1, column2]

In your code you're not correctly defining the list of lists and that leads to an error.

Upvotes: 0

Related Questions