jeudi 3 mai 2018

using "random" in a python prog. with "while", "if" and elif"

Apologies in advance for the Python 101 nature of this question. I have yet to wrap my mind around while loops. This example is a homework assignment from a book for beginners: Write a program that randomly opens different fortune cookies and prints each message. The user only needs to hit 'enter' to open one cookie after another.

here is what I have so far...

import random
cookie_number=random.randrange(3)+1
prompt=""
while not prompt:
    prompt=input("Open Cookie")

if cookie_number==1:
    print("your lucky numbers are 3, 22, 107")

elif cookie_number==2:
    print("you are lucky in love today")

elif cookie_number==3:
    print("buy stocks today")

else:
    print("stay away from MSG")




Aucun commentaire:

Enregistrer un commentaire