0

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.

5
  • 2
    And what exactly is wrong? What do you need help with? Commented Jan 10, 2015 at 19:07
  • 1
    Dictionary would be more helpful to keep record for country, capital format. Commented Jan 10, 2015 at 19:10
  • when i try to add column 1 and 2 to list of list it gives me an error Commented Jan 10, 2015 at 19:10
  • also this is homework for lists so it has to be in list format Commented Jan 10, 2015 at 19:11
  • 1
    You should read the task description again. Your program does not do what it should. Commented Jan 10, 2015 at 19:19

4 Answers 4

1

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!") 
Sign up to request clarification or add additional context in comments.

Comments

0

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.

1 Comment

ok thank you. That's a third of the problem sorted. you wouldn't happen to know why it only prints out the first two sets but nothing else would you?
0
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.

Comments

0

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!") 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.