error help import random
Terry Reedy
tjreedy at udel.edu
Fri Nov 20 15:15:42 EST 2015
On 11/20/2015 12:22 PM, Dylan Riley wrote:
> This is my fortune cookie program i wrote in python.
> the problem is it will not run past the first line of input.
> could someone please identify the error and explain to me why.
> here is the code:
>
> #the program silulates a fortune cookie
> #the program should display one of five unique fortunes, at randon, each time its run
>
> import random
>
> print(" \\ \\ \\ \\ DYLANS FORTUNE COOKIE \\ \\ \\ ")
> print("\n\n\tGood things come to those who wait")
> input("\nPress enter to see your fortune")
>
> fortune = random.randrange(6) + 1
> print(fortune)
> if fortune == 1:
> print("happy")
> elif fortune == 2:
> print("horny")
> elif fortune == 3:
> print("messy")
> elif fortune == 4:
> print("sad")
> elif fortune == 5:
> print("lool")
Use a dict instead of if-elif.
i = random.randrange(6)
fortunes = {0:'happy', 1:'horny', 2:'messy',
3:'sad', 4:'lool', 5:'buggy'}
print(i, fortunes[i])
> print("hope ypu enjoyed your fortune being told")
> input("\nPress enter to exit")
--
Terry Jan Reedy
More information about the Python-list
mailing list