patternpythonMinor
Asking a user for the capital city of Peru, South America
Viewed 0 times
citysouththeperuuserforamericaaskingcapital
Problem
Can this Python code be written in a more Pythonic way?
'''
This scripts ask a user for the capital city of Peru, South America
'''
question = 'What is the capital city of Peru?'
answer = 'Lima'
# Ask question
print question
# Game continues as long as question is not answered
# Game ends after 3 failed attempts
end_game = False
# User has 2 attempts after entering the first wrong answer
attempts = 2
while end_game == False:
# Get user answer
user_answer = raw_input('Enter your answer: ').capitalize()
# Validate users anser
if user_answer == answer:
print 'You got it right'
end_game = True
else:
if attempts > 0:
print 'Wrong! Try again. You have %d attempts left' %(attempts)
attempts -= 1
else:
print 'You have no more attempts remaining'
print 'The correct answer is %s' %(answer)
print 'Oops!!! Game ended'
end_game = TrueSolution
It's kind of a limited code section, but let us go for a review ride:
With all of these changes we got the code looking like:
Do extend the list at will!
NB! When presenting code here at Code Review, the code is supposed to work, and in your case there was a little indentation fault. A good tip to avoid that is to paste the code into the edit box, mark the entire code text and then to hit Ctrl + K which indents the code properly.
- Have everything enclosed in
main()– The pythonic way of starting any code within a module is to have a main method called from the followingifstatement:if __name__ == '__main__':
- Good naming of variables – You seem to name your variables good, that is in
snake_case
- Not so good while loop condition – Two slight errors here, first of all if using something like a flag it would have been better to do
while not end_gameinstead of comparing against false. Even better would be to use your end of game indicator namely that there are no more attempts left:while attempts > 0:(or possiblywhile attempts_left:
- Introducing
elif– The combination ofelse:immediately followed by anifcan often be better written aselif(which reads as else if something)
- Introducing the improved
print()– In Python 3 the print is going to change into a function to make it everything a little more coherent, and whilst changing that, one could also get used to the format string syntax. Which changesprint 'answer is %s' %(answer)intoprint('answer is {}'.format(answer))
- Make functions multipurpose – Often it is good to make a function multi-purpose, albeit still letting it have one primary concern. In your case this could be asking for capital cities
With all of these changes we got the code looking like:
'''
Ask user for what is the capital city of a given land
'''
import random
def ask_for_capital_city(land, capital_city, attempts_left = 3):
"""Ask for the capital city of a land with 3 attempts by default"""
question = 'What is the capital city of {}?'.format(land)
answer = capital_city
# Ask question
print(question)
while attempts_left:
# Get user answer
user_answer = raw_input('Enter your answer: ').capitalize()
# Validate users answer
if user_answer == answer:
print('You got it right')
return True
elif attempts_left:
attempts_left -= 1
print('Wrong! Try again. You have {} attempts left'.format(attempts_left))
else:
print('You have no more attempts remaining.')
print('The correct answer is {}'.format(answer))
print('Oops!!! Game ended')
return False
def main():
capital_cities = [
('Peru', 'Lima'),
('the UK', 'London'),
('the US', 'Washington DC'),
('Norway', 'Oslo'),
('Spain', 'Madrid'),
('Portugal', 'Lisbon'),
('Brazil', 'Brasilia'),
]
# Your original code
ask_for_capital_city('Peru', 'Lima')
# Lets test for some more capital cities
correct_answers = 0
while ask_for_capital_city(*random.choice(capital_cities)):
correct_answers += 1
print('You got {} correct capital cities\n'.format(correct_answers))
print('Goodbye!')
if __name__ == '__main__':
main()Do extend the list at will!
NB! When presenting code here at Code Review, the code is supposed to work, and in your case there was a little indentation fault. A good tip to avoid that is to paste the code into the edit box, mark the entire code text and then to hit Ctrl + K which indents the code properly.
Code Snippets
'''
Ask user for what is the capital city of a given land
'''
import random
def ask_for_capital_city(land, capital_city, attempts_left = 3):
"""Ask for the capital city of a land with 3 attempts by default"""
question = 'What is the capital city of {}?'.format(land)
answer = capital_city
# Ask question
print(question)
while attempts_left:
# Get user answer
user_answer = raw_input('Enter your answer: ').capitalize()
# Validate users answer
if user_answer == answer:
print('You got it right')
return True
elif attempts_left:
attempts_left -= 1
print('Wrong! Try again. You have {} attempts left'.format(attempts_left))
else:
print('You have no more attempts remaining.')
print('The correct answer is {}'.format(answer))
print('Oops!!! Game ended')
return False
def main():
capital_cities = [
('Peru', 'Lima'),
('the UK', 'London'),
('the US', 'Washington DC'),
('Norway', 'Oslo'),
('Spain', 'Madrid'),
('Portugal', 'Lisbon'),
('Brazil', 'Brasilia'),
]
# Your original code
ask_for_capital_city('Peru', 'Lima')
# Lets test for some more capital cities
correct_answers = 0
while ask_for_capital_city(*random.choice(capital_cities)):
correct_answers += 1
print('You got {} correct capital cities\n'.format(correct_answers))
print('Goodbye!')
if __name__ == '__main__':
main()Context
StackExchange Code Review Q#107246, answer score: 8
Revisions (0)
No revisions yet.