[Tutor] How to end this program

Joel Goldstick joel.goldstick at gmail.com
Wed Aug 20 20:40:31 CEST 2014


On Wed, Aug 20, 2014 at 2:34 PM, Joel Goldstick
<joel.goldstick at gmail.com> wrote:
> On Wed, Aug 20, 2014 at 7:00 AM, abid saied <abidsaied at gmail.com> wrote:
>>
>>     # OK, I'm stuck on setting up an instruction that ends the program once you
>>     # say either 'yes' or 'no'. And if you don't say 'yes' or 'no', and you reach
>>     # the end, the program comes to an end. Please remember I'm a newbie and would
>>     # appreciate any advise in plain simple English, like I was 5 years old!
>>
>>
>>
>> import time
>>
>> def main():
>>     print()
>>     print("YES/NO Game ")
>>     time.sleep(2)
>>     print()
>>     print()
>>
>>     print("In this game, I will ask you a series of questions, if you answer YES or NO, you loose!")
>>     print()
>>     print("Remember to answer in Capitals!")
>>     print()
>>     time.sleep(2)
>>     print("Starting the game ...")
>>     print()
>>     time.sleep(2)
>>     name = input("What is your name? ")
>>     print()
>>     time.sleep(2)
>>     print("ok " + (name) + (", time for the questions..."))
>>     print()
>>     time.sleep(2)
>>
>>
>>     answer = input("How old are you?  ")
>>     if answer == "YES":
>>         print("...You said yes, you loose!")
>>     elif answer == "NO":
>>         print ("...You said NO, you loose!")
>>
>>
>>     answer = input("Do you like apples? ")
>>     if answer == "YES":
>>         print("...You said yes, you loose!")
>>     elif answer == "NO":
>>         print ("...You said NO, you loose!")
>>
> You repeat this pattern throughout your program, so it should be made
> a function like this:
>
> def ask_question(question):
>     answer = input(question)
>     answer = answer.upper()
>     if answer == "YES" or answer == "NO":
>         print "...You said %s, you loose!", answer
>         exit(0)    # this will end the program
>     return   # this will get you out of your function
>
>
> To ask all of your questions, create a list of questions like this:
>
> list_of_questions = ["first question", "second question", etc ... ]
>
> Now do this:
> for question in list_of_questions:
>     ask_question()
>
>
> then finish with what you have below.
>
>
>>
>>     print("OK, you win!You didn't say 'YES' or 'NO', well done!")
>>     time.sleep(2)
>>
>>
>>
>>
>> Abid Saied
>> abidsaied at gmail.com
>>
>>
>>
>> _______________________________________________
>> Tutor maillist  -  Tutor at python.org
>> To unsubscribe or change subscription options:
>> https://mail.python.org/mailman/listinfo/tutor
>
>
>
> --
> Joel Goldstick
> http://joelgoldstick.com



-- 
Joel Goldstick
http://joelgoldstick.com


More information about the Tutor mailing list