Last answered:

14 Apr 2022

Posted on:

03 Nov 2021

1

Resolved: Dictionaries : Question 1 problem in the exercise

Hello,

I am finding it difficult to understand how these specific line of code came about under the 'WHILE' conditions. Why use the specific countries of united Kingdom and United States when they are others in the capitals. And not a general term like 'Keys' and 'Values'. Please properly define that line of code for me, because i want to really know how to apply it to other scenarios. 

capitals = {'France':'Paris','Spain':'Madrid','United Kingdom':'London',
            'India':'New Delhi','United States':'Washington DC','Italy':'Rome',
            'Denmark':'Copenhagen','Germany':'Berlin','Greece':'Athens',
            'Bulgaria':'Sofia','Ireland':'Dublin','Mexico':'Mexico City'
            }

while ('united kingdom' not in user_input and not user_input.isalpha()):
    if user_input == 'united states':
        break
    print('You must input a string')
    user_input = input('Which country would you like to check?:> ')

Dictionaries _ Question 1 problem in the exercise 

I used this instead. And it gave me same result. Its much better a safe zone

while ('Keys' not in user_input and not user_input.isalpha()):
    if user_input == 'Keys':
        break
    print('You must input a string')
    user_input = input('Which country would you like to check?:> ')





Second Option

2 answers ( 1 marked as helpful)
Instructor
Posted on:

03 Nov 2021

4

Hey Ashakah,

Thank you for your question and engagement in the Python Programmer Bootcamp course!

The purpose of this while-loop is to check whether the input is valid. This is done through the code not user_input.isalpha() which checks whether there is a character in the string that is not a letter. The reason why United States and United Kingdom are special is because they include a space between the words and a space is not considered a letter. This you can ensure by typing

' '.isalpha()

and noticing that the result is False. Therefore, by performing this check, we make sure to exclude 'united states' and 'united kingdom' as invalid inputs.

A more intuitive way, I believe, to write this while-loop would be the following:

while (not user_input.isalpha()):
   if (user_input == 'united kingdom' or user_input =='united states'):
       break
   print('You must input a string')
   user_input = input('Which country would you like to check?:> ')



Hope this helps!

Kind regards,
365 Hristina

Posted on:

14 Apr 2022

1

Thank you for clarifying this Hristina. It took me some time to understand why 'United Kingdom' and 'United States' were considered separately.

The following could be useful if there were many countries that had a space in the middle.

I tried using find( ) to ensure the while loop is activated only for cases that also have no spaces:  user_country.find(' ') < 0
find(' ') appears to give the number of non-space characters encountered before finding the first space.

(Used strip() to remove any unintended leading and trailing spaces and title() to bring variations of user input for a given value to the same format found in the dictionary)

while (not user_country.isalpha() and user_country.find(' ') < 0):
    print("You must input a string")
    user_country = input('Try again. Enter a country: ')
    user_country = user_country.strip().title()

Alternatively, adapting the more intuitive code shared by Hristina, I ended up using:

while (not user_country.isalpha()):
   if (user_country.find(' ') > 0):
       break
   print('You must input a string')
   user_country = input('Which country would you like to check?:> ')
   user_country = user_country.strip().title()








Submit an answer