Python Forum
i've got these very simple homework
Thread Rating:
  • 1 Vote(s) - 1 Average
  • 1
  • 2
  • 3
  • 4
  • 5
i've got these very simple homework
#1
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'
Reply
#2
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])
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#3
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
Reply
#4
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)
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#5
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
Reply
#6
You are missing a close parentheses as the end of line 9.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#7
@HakolYahol: this is the least pythonic (i.e. worst) approach of all suggestions
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

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

:) thanks for the correction
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Where can I get loooads of simple homework questions for kids? omar 1 975 Nov-09-2022, 02:33 PM
Last Post: Larz60+
  [TkInter]Simple Python Homework zaji_123 4 4,063 Jan-01-2021, 07:09 PM
Last Post: EliesBe
  Please help. Simple homework Asm0deus314 3 3,367 Feb-02-2020, 07:03 PM
Last Post: michael1789
  Homework Help - Simple Grading System Segovia 7 7,765 Jul-24-2018, 10:55 PM
Last Post: Segovia

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020