Currently working with the Python lessons and I always try to apply some of the past lessons to make a block of code a bit more functional.
For lessons 17: review functions, I completed the lesson but set myself another challenge, namely creating a prompt in the console that’d ask the user whether he’d like to shut down the machine or not, after which the user can input either yes or no resulting in a ‘shutdown’ message to be printed or not.
Link to lesson: https://www.codecademy.com/courses/learn-python/lessons/functions/exercises/here-be-dragons?action=resume_content_item
Was wondering whether there is any way to make the code nicer, or more compact. I especially felt like the way how I made the user input case insensitive a bit messy and was wondering whether there was a better way to do it.
def shut_down(s): if s == "yes" or s == "y": print "Shutting down" return "Shutting down" elif s == "no" or s == "n": print "Shutdown aborted" return "Shutdown aborted" else: print "Please answer \"yes\" or \"no\"" return "Sorry" input = raw_input("""Do you want to shut this machine down? \"yes\" or \"no\" """) input = input.lower() shut_down(input)
Any help will be appreciated, have a nice day!