I have been using a website called 'CSNewbies' to learn Python and one of the tasks was to create a way for a user to check how many days a country has been independent. The countries are limited to Fiji, Samoa and Australia.

I actually was really proud of the code I wrote because it ended up working. I tried making it a little better at the end by making a fail safe in case the user inputs a different country. However, I wanted to know if my code was too much. Did i write too much at any point?

I feel like for only 3 countries it was quite a big bit of code. Can this be improved?

UPDATE - I asked this in a different forum and was redirected here but I got an answer before the forum was closed. I will type the better code below as I have a few lines I do not understand if anybody could help.

The code that i wrote:

# import necessary whatver
from time import strftime
from datetime import date

# set independence days of each country
fiji = date(1970,10,10)
samoa = date(1962,12,13)
australia = date(1901,1,1)

# get todays date
todays_date = date.today()

# get a difference from today and independence days
difffiji = todays_date - fiji
diffsamoa = todays_date - samaoa
diffaustralia = todays_date - australia

# independence checker
print('--Independence Checker--')

#create funct so i can call it at then end as a fail-safe and then it restarts the programme
def indcheck():
    count = input('Choose between Fiji, Samoa,Australia:').lower()
    if count == 'fiji':
        print(f'Fiji has been independent for {difffiji.days} days.')
    elif count == 'samoa':
        print(f'Samoa has been independent for {diffsamoa.days} days.')
    elif count == 'australia':
        print(f'Australia has been independent for {diffaustralia.days} days.')
        print('Please pick either Fiji, Samoa or Australia!')

  • 1
    \$\begingroup\$ Do you think that count is a good name for a variable containing a country name? \$\endgroup\$
    – md2perpe
    Commented Jun 26, 2022 at 19:37
  • \$\begingroup\$ To be honest i misspelt country at first and then i just went with it for some reason but i agree that was stupid \$\endgroup\$ Commented Jun 26, 2022 at 20:45
  • \$\begingroup\$ A very, very useful rule of thumb for when you should comment and what type of things you should write in comments: "code describes how the program works, comments describe why the author wrote that code". Comments should describe the human intent that led you to writing that specific code. The names you use in your function should be just descriptive enough to make it clear to a new reader what the purpose of each variable & function is. \$\endgroup\$
    – iono
    Commented Jun 27, 2022 at 1:56
  • \$\begingroup\$ The second very, very useful rule of thumb I'd recommend to you is this: train yourself to look for repetitive code, and then turn every repetition into an abstraction. The moment you, as a human writing code for a computer, have to repeat yourself, you should immediately think "is this repetition something that a computer can do, instead of the code author?" In this case, your repetition could be automated by using a "table"/"hash map" to hold the the names of countries and their independence days, and then using a "loop" or "iteration" to step through each entry and perform tasks. \$\endgroup\$
    – iono
    Commented Jun 27, 2022 at 2:01

1 Answer 1


Delete all of your comments. None of them make the code more obvious than just reading it.

strftime is unused so don't import it.

Consider using a dictionary of country names to independence dates instead of separate variables.

Don't do three separate subtractions - just do one.

Your "fail-safe" is a good idea, but you should not recurse; loop instead.

Do not hard-code the country names in your prompt. Just use the keys of your dict instead.

Add a __main__ guard.


from datetime import date

    'Fiji': date(1970, 10, 10),
    'Samoa': date(1962, 12, 13),
    'Australia': date(1901, 1, 1),

def ind_check() -> None:
    print('--Independence Checker--')
    prompt = (
        'Choose between '
        + ', '.join(INDEPENDENCE.keys())
        + ': '

    while True:
        country = input(prompt).title()
        independence = INDEPENDENCE.get(country)
        if independence:

    age = date.today() - independence
    print(f'{country} has been independent for {age.days} days.')

if __name__ == '__main__':
  • \$\begingroup\$ Thanks for the reply! The majority made sense. I am new to coding and not just python so this may be an obvious question. But what does the “-> None:” do? \$\endgroup\$ Commented Jun 26, 2022 at 20:39
  • \$\begingroup\$ When you where typing out the prompt you used “independence.keys” what does the “keys” mean \$\endgroup\$ Commented Jun 26, 2022 at 20:39
  • 1
    \$\begingroup\$ The rest i will try and research myself what a prompt and a main guard is but thank you for the help I appreciate it. \$\endgroup\$ Commented Jun 26, 2022 at 20:40
  • \$\begingroup\$ But what does the “-> None:” do? - it's a PEP484 type hint indicating that there is no return from the function. \$\endgroup\$
    – Reinderien
    Commented Jun 26, 2022 at 22:17
  • \$\begingroup\$ what does the “keys” mean - this is the collection of keys to the dictionary, i.e. the country names. \$\endgroup\$
    – Reinderien
    Commented Jun 26, 2022 at 22:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.