mercredi 1 novembre 2017

Implementing a dictionary to show a case statement in Python 3.x

I'm novice to Python (3.x) and I am given the task of asking students country and capital names.

The program must...

  • Read a text file of countries and their capital cities (which I have a .csv file provided)

  • Ask 10 questions about the capitals of 10 countries

  • If user gets the capital city correct, they gain one mark

  • If user gets the capital city incorrect, they should be told the correct answer

  • At the end of the 10 questions, user receives feedback about their performance depending on how many marks out of 10 they achieved.

This feedback should be implemented using a dictionary to show that case-type statements can be simulated in Python (as Python doesn't use case/switch statements).

I have written a guide to implement for the feedback depending on the amount of marks user has achieved.

This is the code I have attempted so far with the listed countries and their capitals (which is also included in the .csv file), however I am a bit stuck:

countries = {
'United Kingdom',
'France',
'Germany',
'Spain',
'Portugal',
'Poland',
'Czech Republic',
'Belgium',
'Hungary',
'Sweden',
'Norway',
'Finland'

}

capitals = {
    'London',
    'Paris',
    'Berlin',
    'Madrid',
    'Lisbon',
    'Warsaw',
    'Prague',
    'Brussels',
    'Budapest',
    'Stockholm',
    'Oslo',
    'Helinski'

}

import csv
import random

ans = True

print("Press X to quit")

try:
        # a = append, w = write
    with open('capitals.csv', 'w', newline='') as csvfile:
        write = csv.writer(csvfile, dialect='excel')

    while ans:
        # Country Input
        country = input('What is the country?')
        if country != 'X':
            if country in countries:
                country_name = countries[country]
            else:
                country_name = 'Country is not listed.'
                print('The name of the country that you have chosen is', country)

            # Capital Input
            capital = input('What is the capital?')
            if capital != 'X':
                if capital in capitals:
                    capital_name = capitals[capital]
            else:
                capital_name = 'Capital is not listed.'
                print('The name of the capital that you have chosen is', capital)

                write.writerow([country, capital])

                def validmark():
                    global mark
                    if int(mark) >=0 and int(mark) <=10:
                        return True
                    else:
                        return False

    total = input('What is the maximum amount of marks? ')
    while not validmark(total):
        total = input('Re-enter maximum amount of marks ')

        if country =="X" or capital == "X":
            print("\nGoodbye") 
            ans = False

except OSError as err:
    print("Unable to access file")

I must report how an if and a case-type construct is used for this particular piece of code.

I am still quite new to Python and programming as a whole so if you could help me out that would be much appreciated, thanks.

Aucun commentaire:

Enregistrer un commentaire