Random fortune - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: Random fortune (/thread-24343.html) |
Random fortune - jackthechampion - Feb-10-2020 Im making a random fortune teller for my class this is what I have so far can someone fix it for me? Also if you could make it 10 fortunes it needs to be 10 lol ie. def MyFortune () : print ("Welcome to my cool fortune teller") print ("This program is guaranteed to predict your future") color = raw_input("Select a color: red, blue, green, or purple: ") if color == "red" or color == "green": number = int(raw_input("Select a number: 1, 2, 5, or 6: ")) if number == 1: print ("You will be very, very rich!") elif number == 2: print ("You will become a jedi master!") elif number == 5: print ("you will live a poor life") elif number == 6: print ("you will become president") else: print ("you must enter 1, 2, 5 or 6") elif color == "blue" or color == "purple": number = int(raw_input("select a number: 3, 4, 7, or 8")) if number == 3: print ("you will die") elif number == 4: print ("you will live a poor life") elif number == 7: print ("you will become a wizard") elif number == 8: print ("you will save the earth") else: print ("you must enter 3, 4, 7, 8") else: print "you must enter a correct color: red, blue, green or purple" RE: Random fortune - perfringo - Feb-10-2020 Just to give an idea: maybe use dictionary? >>> fortunes = {'red': {1: 'be happy', 2: "don't worry"}, ... 'blue': {3: 'monday', 4: 'tuesday'}} >>> color = 'red' >>> num = 2 >>> fortunes[color][num] # works like charm "don't worry" >>> fortunes['purple'][1] # but throws an error if there is no such a key /.../ KeyError: 'purple'One can use try...except to handle non-existing keys (alternatively perform input validation): try: fortune = fortunes[color][num] # do something with fortune except KeyError: # handle situation of incorrect input |