Python

Learning Python: Learning Python the Hard Way

Yes/No function

A function to ask the user a yes/no question and return the result as a Boolean.

def yesno(question):
    answer = raw_input(question + " [yes/no]: ")
    if answer.lower() in ('y', 'yes'):
        return True
    elif answer.lower() in ('n', 'no'):
        return False

This is useful when you wish to ask the user before performing a task, for example "Would you like to exit?" could be programmed as follows:

while True:
    if yesno("Would you like to exit?"):
        break






5 comments:

  1. i Found Python Really Difficult at the Beginning , it was Like learning a new language

    ReplyDelete
  2. I do very much like how you implemented a yesno function into your HTML/Javascript layout for this page. Very clever, young padowan.

    ReplyDelete
  3. I found python difficult at first, but slowly slowly I started understanding it and enjoying it much more.

    ReplyDelete
  4. python is great module if you want to became a programmer but difficult in the beging, that why we should complain to keep python on our program even for 12 weeks

    ReplyDelete
  5. Our python with Saeed was good but it was too short. There is much more to learn from python lesson should have continued until the end of the foundation course.

    If anybody wants to learn python by themselves visit http://learnpythonthehardway.org/

    ReplyDelete