Posts: 13
Threads: 4
Joined: Aug 2018
Aug-07-2018, 07:43 AM
(This post was last modified: Aug-07-2018, 07:43 AM by HakolYahol.)
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'
Posts: 2,953
Threads: 48
Joined: Sep 2016
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])
Posts: 536
Threads: 0
Joined: Feb 2018
Aug-07-2018, 05:15 PM
(This post was last modified: Aug-07-2018, 05:16 PM by woooee.)
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
Posts: 8,160
Threads: 160
Joined: Sep 2016
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)
Posts: 13
Threads: 4
Joined: Aug 2018
Aug-08-2018, 10:16 AM
(This post was last modified: Aug-08-2018, 10:48 AM by HakolYahol.)
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
Posts: 4,220
Threads: 97
Joined: Sep 2016
You are missing a close parentheses as the end of line 9.
Posts: 8,160
Threads: 160
Joined: Sep 2016
@HakolYahol: this is the least pythonic (i.e. worst) approach of all suggestions
Posts: 13
Threads: 4
Joined: Aug 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
|