Help!


#1

import random
def loop():
country = ["United Arab Emirates", "Nigeria", "Ghana", "Pitcairn Islands", "Ethiopia", "Algeria", "Niue", "Jordan","Netherlands", "Andorra", "Turkey", "Madagascar", "Samoa", "Turkmenistan", "Eritrea", "Kazakhstan","Paraguay", "Greece", "Cook Islands", "Iraq", "Azerbaijan", "Mali", "Brunei", "Thailand","Central African Republic", "Gambia", "Guadeloupe", "Saint Kitts and Nevis", "China", "Lebanon","Serbia", "Belize", "Germany", "Switzerland", "Kyrgyzstan", "Guinea-Bissau", "Colombia", "Brazil","Slovakia", "Republic of the Congo", "Barbados", "Belgium", "Romania", "Hungary", "Argentina", "Burundi","Egypt", "Australia", "Venezuela", "Saint Lucia", "French Guiana", "United States Virgin Islands","Moldova", "Turks and Caicos Islands", "Guinea", "Denmark", "Senegal", "Syria", "Bangladesh","East Timor (Timor-Leste)", "Djibouti", "Tanzania", "Qatar", "Isle of Man", "Ireland", "Tajikistan","State of Palestine", "Akrotiri and Dhekelia", "Christmas Island", "Martinique", "Sierra Leone","Tuvalu", "Botswana", "Cayman Islands", "Ascension Island", "Guyana", "Gibraltar", "Guatemala","Saint Barthelemy", "Guam", "Bermuda", "Easter Island", "Vietnam", "Zimbabwe", "Somaliland", "Cuba","Finland", "Hong Kong", "Solomon Islands", "Pakistan", "Indonesia", "Saint Helena", "Israel","South Sudan", "Afghanistan", "Uganda", "Nepal", "Sudan", "Ukraine", "Rwanda","South Georgia and the South Sandwich Islands", "Jamaica", "Norfolk Island","Saint Vincent and the Grenadines", "Democratic Republic of the Congo", "Malaysia", "Kuwait", "Gabon","Malawi", "Peru", "Portugal", "Slovenia", "Togo", "United Kingdom", "Angola", "Zambia", "Luxembourg","Spain", "Marshall Islands", "Equatorial Guinea", "Maldives", "Mayotte", "Nicaragua", "Bahrain","Philippines", "Mozambique", "Saint Martin", "Lesotho", "Wallis and Futuna", "Swaziland", "Mexico","Belarus", "Somalia", "Monaco", "Liberia", "Uruguay", "Comoros", "Russia", "Oman", "Kenya", "Bahamas","Myanmar", "Chad", "India", "Palau", "Niger", "Cyprus", "Northern Cyprus", "Mauritania", "New Caledonia","Tonga", "Greenland", "Aruba", "Norway", "Canada", "Burkina Faso", "American Samoa","Federated States of Micronesia", "Panama", "French Polynesia", "Suriname", "France", "Sint Maarten","Cambodia", "Montserrat", "Montenegro", "Mauritius", "Papua New Guinea", "Vanuatu", "Haiti","Trinidad and Tobago", "Benin", "Czech Republic", "Cape Verde", "South Africa", "Kosovo", "North Korea","Ecuador", "Morocco", "Iceland", "Latvia", "Saudi Arabia", "British Virgin Islands", "Italy", "Dominica","Reunion", "Northern Mariana Islands", "Costa Rica", "Puerto Rico", "San Marino", "El Salvador", "Yemen","Chile", "Dominican Republic", "Sao Tome and Principe", "Bosnia and Herzegovina", "South Korea","Singapore", "Macedonia", "Bulgaria", "Sri Lanka", "Grenada", "Jersey", "Antigua and Barbuda","Guernsey", "Saint Pierre and Miquelon", "Falkland Islands", "Nagorno-Karabakh Republic", "Sweden","Bolivia", "Abkhazia", "Fiji", "Taiwan", "Estonia", "Kiribati", "Uzbekistan", "Georgia", "Honduras","Iran", "Bhutan", "Albania", "Transnistria", "Japan", "Faroe Islands", "Libya", "South Ossetia","Tunisia", "Mongolia", "Liechtenstein", "Malta", "Anguilla", "Vatican City", "Seychelles", "Austria","Laos", "Lithuania", "Poland", "United States", "New Zealand", "Cocos (Keeling) Islands", "Curacao","Namibia", "Ivory Coast", "Cameroon", "Nauru", "Armenia", "Croatia"]
capital = ["Abu Dhabi", "Abuja", "Accra", "Adamstown", "Addis Ababa", "Algiers", "Alofi", "Amman", "Amsterdam","Andorra la Vella", "Ankara", "Antananarivo", "Apia", "Ashgabat", "Asmara", "Astana", "Asuncion","Athens", "Avarua", "Baghdad", "Baku", "Bamako", "Bandar Seri Begawan", "Bangkok", "Bangui", "Banjul","Basse-Terre", "Basseterre", "Beijing", "Beirut", "Belgrade", "Belmopan", "Berlin", "Bern", "Bishkek","Bissau", "Bogota", "Brasilia", "Bratislava", "Brazzaville", "Bridgetown", "Brussels", "Bucharest","Budapest", "Buenos Aires", "Bujumbura", "Cairo", "Canberra", "Caracas", "Castries", "Cayenne","Charlotte Amalie", "Chisinau", "Cockburn Town", "Conakry", "Copenhagen", "Dakar", "Damascus", "Dhaka","Dili", "Djibouti", "Dodoma", "Doha", "Douglas", "Dublin", "Dushanbe", "East Jerusalem","Episkopi Cantonment", "Flying Fish Cove", "Fort-de-France", "Freetown", "Funafuti", "Gaborone","George Town", "Georgetown", "Georgetown", "Gibraltar", "Guatemala City", "Gustavia", "Hagatna","Hamilton", "Hanga Roa", "Hanoi", "Harare", "Hargeisa", "Havana", "Helsinki", "Hong Kong", "Honiara","Islamabad", "Jakarta", "Jamestown", "Jerusalem", "Juba", "Kabul", "Kampala", "Kathmandu", "Khartoum","Kiev", "Kigali", "King Edward Point", "Kingston", "Kingston", "Kingstown", "Kinshasa", "Kuala Lumpur","Kuwait City", "Libreville", "Lilongwe", "Lima", "Lisbon", "Ljubljana", "Lome", "London", "Luanda","Lusaka", "Luxembourg", "Madrid", "Majuro", "Malabo", "Male", "Mamoudzou", "Managua", "Manama", "Manila","Maputo", "Marigot", "Maseru", "Mata-Utu", "Mbabane", "Mexico City", "Minsk", "Mogadishu", "Monaco","Monrovia", "Montevideo", "Moroni", "Moscow", "Muscat", "Nairobi", "Nassau", "Naypyidaw", "N'Djamena","New Delhi", "Ngerulmud", "Niamey", "Nicosia", "Nicosia", "Nouakchott", "Noumea", "Nuku?alofa", "Nuuk","Oranjestad", "Oslo", "Ottawa", "Ouagadougou", "Pago Pago", "Palikir", "Panama City", "Papeete","Paramaribo", "Paris", "Philipsburg", "Phnom Penh", "Plymouth", "Podgorica", "Port Louis","Port Moresby", "Port Vila", "Port-au-Prince", "Port of Spain", "Porto-Novo", "Prague", "Praia","Pretoria", "Pristina", "Pyongyang", "Quito", "Rabat", "Reykjavik", "Riga", "Riyadh", "Road Town","Rome", "Roseau", "Saint-Denis", "Saipan", "San Jose", "San Juan", "San Marino", "San Salvador","Sana'a", "Santiago", "Santo Domingo", "Sao Tome", "Sarajevo", "Seoul", "Singapore", "Skopje", "Sofia","Sri Jayawardenepura Kotte", "St. George's", "St. Helier", "St. John's", "St. Peter Port", "St. Pierre","Stanley", "Stepanakert", "Stockholm", "Sucre", "Sukhumi", "Suva", "Taipei", "Tallinn", "Tarawa Atoll","Tashkent", "Tbilisi", "Tegucigalpa", "Tehran", "Thimphu", "Tirana", "Tiraspol", "Tokyo", "Torshavn","Tripoli", "Tskhinvali", "Tunis", "Ulaanbaatar", "Vaduz", "Valletta", "The Valley", "Vatican City","Victoria", "Vienna", "Vientiane", "Vilnius", "Warsaw", "Washington D.C.", "Wellington", "West Island","Willemstad", "Windhoek", "Yamoussoukro", "Yaounde", "Yaren", "Yerevan", "Zagreb"]
score = 0
ques = input('Would you like to pick a capital and give the country to that capital? If you say no, the computer will generate random capitals and quiz you. yes or no? (PUT YES OR NO IN DOUBLE QUOTES EX: "yes"')
if ques == "yes":
for x in range(1, 10):
quest = input("Pick a capital: ")
if quest in capital:
answe = input("What is the country to this capital?")
if answe in country:
if country.index(answe) == capital.index(quest):
print("You are correct")
score += 1
else:
print("You are wrong")
else:
print("The country you have entered was not found")
else:
print("The capital you have entered was not found")
print("You have recived", score, "point(s)")
elif ques == "no":
for i in range(1, 10):
r = random.randint(0, 246)
ans = country[r]
cap = capital[r]
r1 = random.randint(0, 246)
wrong = country[r1]
r2 = random.randint(0, 246)
wrong1 = country[r2]
r3 = random.randint(0, 246)
wrong2 = country[r3]
print ("What is the country to the capital,", cap)
res = [wrong, ans, wrong1, wrong2]
res.sort()
print (res)
user = input("Enter the answer here (use caps): ")
if user == ans:
print ("congratulations! You're right!! You get a point!!!")
score += 1
elif user not in res:
print (
"The response you have entered is not identified as a response. Please check"
" capitalization and spelling")
error(wrong, ans, wrong1, wrong2, score)
else:
print ("You're as dumb as a bag of poop! :)")
print("You have recived", score, "point(s)")
def error(wrong, ans, wrong1, wrong2, score):
res = [wrong, ans, wrong1, wrong2]
print (res)
user = input("Enter the answer here (use caps): ")
if user == ans:
print ("congratulations! You're right!! You get a point!!!")
score += 1
elif user not in res:
print (
"The response you have entered is not identified as a response. Please check capitalization and spelling")
else:
print ("You're as dumb as a bag of poop! :)")
def isCorrect(user, ans, res):
if user in res:
return true
else:
return false
loop()

This is my code and for some reason I'm getting an error when I run it this is what happens:

('What is the country to the capital,', 'San Salvador')
['Barbados', 'Cocos (Keeling) Islands', 'Easter Island', 'El Salvador']
Enter the answer here (use caps): El Salvador

Traceback (most recent call last):
File "C:\Users\angel\Documents\code\countriescapitals.py", line 75, in
loop()
File "C:\Users\angel\Documents\code\countriescapitals.py", line 40, in loop
user = input("Enter the answer here (use caps): ")
File "", line 1
El Salvador
^
SyntaxError: unexpected EOF while parsing


#2

I would suggest you rewrite this so that it is a dict or a list in a list,

Example

countries_captials = {"United Arab Emirates": "Abu Dhabi", "Nigeria": "Abuja"}

# OR

countries_captials = [["United Arab Emirates", "Abu Dhabi"], ["Nigeria", "Abuja"]

Then you would do the following to match,

Code:

country = "United Arab Emirates"
ask = raw_input("What is the capital of %s: \n" % country).lower()
if countries_captials[country].lower() == ask:
    print("Point")

# OR
from random import randint

country = countries_captials[0, randint(len(countries_captials)) - 1]
ask = raw_input("What is the capital of %s?: \n" % country[0])
if ask.lower() == country[1].lower():
    print("Point")

I would directly associate the data to one another so that you can avoid some complex checking code, if you do with the list of lists you have to search through the whole list each time you want to find a specific instance, while you can access the dict with the country or city depending on hwo you implement it.

I would suggest going with the dict for this because it would work better.

If you want to join both your lists programmatically then do this,

hold = dict()
for index, country in enumerate(country):
    # City must be in the same order and have the same number of entries
    hold[country] = city[index]
# This will save to current directory
with open("countries_cities.txt", "w+") as file:
    file.write(hold)

Then if you want to load that file.

hold = dict()
with open("countries_cities.txt", "r") as file:
    file.seek(0)
    hold = eval(str(file.readline()))

If you need any more help feel free to ask, I will try to do what I can.


#3