I’m making some quiz as a preparation to deploy fun games on Alexa. This would be one of them. Game is quite simple to prompt two random capital cities, and you need to answer which city is further north than the other.
Output Example:
shogokobayashi 100p $ python3 capital_quiz.py [Y]es or [N]o: City Yaounde is further north than City Gustavia. Your Answer: n Correct! Do you want to try again? [Y]es or any other key to quit y [Y]es or [N]o: City Washington is further north than City Ashgabat. Your Answer: y Correct! Do you want to try again? [Y]es or any other key to quit y [Y]es or [N]o: City Paris is further north than City Abu Dhabi. Your Answer: n Wrong. Do you want to try again? [Y]es or any other key to quit Thank you for playing. You answered correctly 2 times out of 3 trials.
import random from data_source import countryinfo def further_north(): capitals = countryinfo.capitals city_lat = {} for capital in capitals: city, latitude = capital['CapitalName'], capital['CapitalLatitude'] if city != "N/A": city_lat[city] = float(latitude) trial, correct = 0, 0 while True: trial += 1 city1, city2 = random.sample(city_lat.keys(), 2) print(f"[Y]es or [N]o: City {city1} is further north than City {city2}.") YesNo = "" while YesNo not in ["y", "n"]: YesNo = input("Your Answer: ").lower() if (YesNo == "y" and city_lat[city1] > city_lat[city2]) or (YesNo == "n" and city_lat[city1] < city_lat[city2]): print("Correct!") correct += 1 else: print("Wrong.") print("Do you want to try again? [Y]es or any other key to quit") if input().lower() != "y": break print(f"Thank you for playing. You answered correctly {correct} times out of {trial} trials.") if __name__ == "__main__": further_north()