Reputation: 1
When I run this code:
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'
}
country = input('Please write a country >>> ')
country = country.title()
if country.isalpha():
if country in capitals:
print(f'The capital of {country} is', capitals.get(country))
else:
print('Sorry, I couldn\'t find this country in my list')
else:
print('There\'s something wrong, please check the country name you\'ve entered')
and enter 'united states'
or 'united kingdom'
as input, it returns;
Please write a country >>> United Kingdom
There's something wrong, please check the country name you've entered
Couldn't find what I was doing wrong... Anyone could give me a hand?
Upvotes: 0
Views: 56
Reputation: 136
The problem is that the isaplha()
method takes into account the white spaces in the input strings 'united states' and 'united kingdom' and returns false
.
White spaces are not considered to be alphabet.
You can try this:
if country.replace(" ","").isalpha(): #Removing white spaces
if country in capitals:
print(f'The capital of {country} is', capitals.get(country))
else:
print('Sorry, I couldn\'t find this country in my list')
else:
print('There\'s something wrong, please check the country name you\'ve entered')
This will remove the white spaces in input strings while checking the condition.
Upvotes: 1