mardi 29 septembre 2020

How to randomly select quote from txt file Python

I have a .txt file that looks like this:

Better three hours too soon than a minute too late
Love all, trust a few, do wrong to none
A fool thinks himself to be wise, but a wise man knows himself to be a fool
There is nothing either good or bad but thinking makes it so
One touch of nature makes the whole world kin
We know what we are, but know not what we may be
It is not in the stars to hold our destiny but in ourselves
Love sought is good, but given unsought, is better
Some are born great, some achieve greatness and some have greatness thrust upon them
No legacy, is so reach, as honesty
Bieber is barber

I want to make the the code to select random quotes from txt and fill it on interface below :(The hide quote then reveal quote)

               ..:: C.O.V.I.D ::..
+----------------------------------------------------+
| _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _            |
| _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _     _  _       |
| _ _ _                                  _ _ _ _ _ _ |
+----------------------------------------------------+

Here is my code. I tried for few hours:

quotes = psphelper.readQuotesFromFile("quotes.txt");
#print("List of quotes");
no = 1;
for q in quotes:
 #   print("{0}. {1}" .format(no, q));
    no += 1;

guess_quote = quotes
guess_quote_answer = guess_quote

guess_quote_list = list(guess_quote)

for i in range(len(guess_quote_list)):
    if guess_quote_list[i] != ' ':
        guess_quote_list[i] = '_'
guess_quote = ''.join(guess_quote_list)

guess = 'a'
for i in range(len(guess_quote_answer)):
    if guess_quote_answer[i] == guess:
        guess_quote_list[i] = guess

title = "..:: C.O.V.I.D ::..";
width = 50;
print(title.center(width));

guess_quote = ''.join(guess_quote_list)    
psphelper.showQuoteScreen(guess_quote, 50)



Aucun commentaire:

Enregistrer un commentaire