Hello. I am new to Python, and I am trying to make a program where one would say, “Who is your favorite charcter from (insert name of movie).” How would I make it so that it would randomly print a character (eg “Yes, (character) is the best character” or “NO. (character) is the worst character.” I believe this has something to do with lists…
… and dictionaries. Both data structures serve a major role in information architecture. Down the road when you get into API’s you’ll see how crucial well formed data structures really are, both in terms of data base and JSON. But this is getting over our heads. Let’s confine ourselves to raw code with built in data structures.
What have your learned about lists, thus far? (Hope you don’t mind me asking?)
from random import choice character = "Luke Skywalker" seq = ["Yes! %s is the best character.", "No! %s is the worst character."] print (choice(seq) % character)
Yes! Luke Skywalker is the best character. No! Luke Skywalker is the worst character.
The above is experimental. It does get to the root objective, though.
Hopefully you are racing through this course since the following is getting more advanced…
from random import choice seq = ["Yes! %s is the best character.", "No! %s is the worst character."] def response(x): return (choice(seq) % x) character = raw_input("Best character?") print (response(character))