Python Forum

Full Version: i've got these very simple homework
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
my mind is not always the best so i hope you could help me with this:

Quote:Write a program that simulates a fortune cookie. The program
should display one of five unique fortunes, at random, each
time it’s run.

i could only come up with this start:
import random

# list of furtune cookie sayings

one = "For hate is never conquered by hate. Hate is conquered by love ."
two = "Your worst enemy has a crush on you!"
three = "You will make many changes before settling down happily."
four = "If you have something worth fighting for, then fight for it."
five = "In music, one must think with his heart and feel with his brain."

# choosing one randomly
cookie = random.randint(1, 5)
if cookie 
TNX

edit:
the solution should be without a 'list'
You already use randint method so a dictionary is one way:

cookies = {1: "For hate is never conquered by hate. Hate is conquered by love .",
    2: "Your worst enemy has a crush on you!",
    3: "You will make many changes before settling down happily.",
    4: "If you have something worth fighting for, then fight for it.",
    5: "In music, one must think with his heart and feel with his brain."}

print(cookies[cookie])
If it's simple, why can't you do it. If you can't do it, how do you know that it's simple.
##--------------------------------------------
## this is much better and easier using a list
##--------------------------------------------

import random
 
##--------------------------------------------
# list of furtune cookie sayings
##    Your program does not use a list.  If you are
##    supposed to use a list, then rewrite this code
##    i.e. cookie_list=[one, two, three, four]
##-------------------------------------------- 

one = "For hate is never conquered by hate. Hate is conquered by love ."
two = "Your worst enemy has a crush on you!"
three = "You will make many changes before settling down happily."
four = "If you have something worth fighting for, then fight for it."
five = "In music, one must think with his heart and feel with his brain."
 
# choosing one randomly
cookie = random.randint(1, 5)
if cookie == 1:
    print(one)
elif cookie==2:
    print(two)
etc
using list and random.choice()
import random
 
cookies = ["For hate is never conquered by hate. Hate is conquered by love.", 
           "Your worst enemy has a crush on you!", 
           "You will make many changes before settling down happily.", 
           "If you have something worth fighting for, then fight for it.", 
           "In music, one must think with his heart and feel with his brain."]
 
# choosing one randomly
cookie = random.choice(cookies)
print(cookie)
thank you both very much.

the first option is what i was needed

:)

ok, i've tried to write it but i get an error. can you tell me why?

  File "fortune_cookie.py", line 11
    elif cookie == 2:
       ^
SyntaxError: invalid syntax
# Write a program that simulates a fortune cookie. The program
# should display one of five unique fortunes, at random, each
# time it’s run.

import random

cookie = random.randint(1, 5)
if cookie == 1:
	print("For hate is never conquered by hate. Hate is conquered by love."
	
elif cookie == 2:
	print("Your worst enemy has a crush on you!")
	
elif cookie == 3:
	print("You will make many changes before settling down happily.")
	
elif cookie == 4:
	print("If you have something worth fighting for, then fight for it.")
	
elif cookie == 5:
	print("In music, one must think with his heart and feel with his brain.")

input("\n\nPress the enter key to exit.")
i was sure it would work
You are missing a close parentheses as the end of line 9.
@HakolYahol: this is the least pythonic (i.e. worst) approach of all suggestions
(Aug-08-2018, 11:54 AM)ichabod801 Wrote: [ -> ]You are missing a close parentheses as the end of line 9.

:) thanks for the correction