Python Forum
i've got these very simple homework - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Homework (https://python-forum.io/forum-9.html)
+--- Thread: i've got these very simple homework (/thread-12059.html)



i've got these very simple homework - HakolYahol - Aug-07-2018

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'


RE: i've got these very simple homework - wavic - Aug-07-2018

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])



RE: i've got these very simple homework - woooee - Aug-07-2018

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



RE: i've got these very simple homework - buran - Aug-07-2018

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)



RE: i've got these very simple homework - HakolYahol - Aug-08-2018

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


RE: i've got these very simple homework - ichabod801 - Aug-08-2018

You are missing a close parentheses as the end of line 9.


RE: i've got these very simple homework - buran - Aug-08-2018

@HakolYahol: this is the least pythonic (i.e. worst) approach of all suggestions


RE: i've got these very simple homework - HakolYahol - Aug-12-2018

(Aug-08-2018, 11:54 AM)ichabod801 Wrote: You are missing a close parentheses as the end of line 9.

:) thanks for the correction