0
I need help making my code work!
i need to make a magic 8 ball but can't figure out how to pull a random number 0-49 and pull a sentence from a string. I also want to make this happen when I press a button (making this on raspberry pi) or when I type fortune. if you need the list of answers let me know in the comments. I want to have this print to an LCD screen, if you could make it do that, that would be awesome here's what I have: x = randnum(0-49) XYZ = (put string selector here) print (XYZ)
4 Respostas
+ 8
import random
x = random.randint(0, 49)
XYZ = [] #your list here
print (XYZ[x])
#or even better
import random
XYZ = [] #your list here
print(random.sample(XYZ, 1) #Choose 1 random element from XYZ
If you want to do it under certain input conditions, put it in appropriate if blocks
+ 3
You can use dictionary, where each value is related to the key. Read more on lists or dictionaries here in Python course or in the Internet
+ 1
how would I do the list? (as I am new to Python, I do not know much) also, I would like to be able to type "Fortune" and get a number. then once it pulls the number, match the number to a set sentence. example:
say it picks 32, it would then go: 32 = hah, yeah right sucker! XYZ = 32
print (XYZ) then it would evaluate the value and print "hah, yeah right sucker!"
or could I make the list have sentences and it chooses one of the sentences from the list?
by the way I would need the library to to work for Python 3 idle, and if you can tell me, a way to put it into Python files so that I can use it in the Python command console.
and thank you for the answer I am not very good with Python as I am just starting to use it.
+ 1
ok.