#! /usr/bin/env python # Load the sys module - we'll need it later to quit cleanly import sys # while just repeats as long as its condition is true... True is always True, so this just looks forever while True: # We initialise our age variable to None, because that's easy to test for and represents no value age=None # while again - this time, repeat this code block until age is something other than None while age is None: # Enter the age answer=raw_input("Please enter your age: ") # The next line might fail, so wrap it in try: except: try: # Do the cast - convert the string to an int. This can fail. age=int(answer) # If it failed... except: # Let them know print "Invalid age entered" # If the int() failed, age won't have been set, so it'll still be None, so the while loop repeats # If the int() worked, age will be something other than None, so we fall off the end of the while block # Ask a silly question... yn=raw_input("Would you like to display your age in seconds? ") # Convert the answer to lowercase and see if it starts with 'y' if yn.lower().startswith("y"): # If it does, do the maths, print the answer print age*365*24*60*60 # Or maybe it (in lowercase) starts with 'n'? elif yn.lower().startswith("n"): # Let them know that they didn't want their age printing print "Okay, no age in seconds for you" # Or maybe, just maybe, it (in lowercase, yes) starts with 'q' - for 'quit' elif yn.lower().startswith("q"): # Let them know that they asked to quit... print "And we're out of here..." # And go ahead and quit. sys.exit() # Hmm... it doesn't start with 'y', 'n', or 'q' - it's invalid! else: # So, let them know that as well... print "Invalid response"