开发者

Writing a random-generating quiz, need to recall the object that was randomly generated

I'm very new to python, so I'm sure my code is prodigiously inefficient, etc, but I'm just feeling my way around and learning by doing at the moment, so excuse my general ignorance.

My quiz currently takes a series of quotes and randomly prints the first 20 characters of them (via slice), the idea being that the quizee recites the rest.

I would like to add the ability to fetch the entire quote, but I don't know how to tell python to recall the last object the random module generated.

Here is my code:

import random
control = True

JCIIiii34 = "Valiant men never taste of death but once"
SonnetSeventeen = "If I could write the beauty of your eyes,\nAnd in fresh numbers number all your graces,\nThe age to come would say this poet lies,\nSuch heavenly touches ne'er touched earthly faces."
SonnetEighteen = "So long as men can breath and eyes can see,\nSo long lives this, and this gives life to thee."
JCIii101 = "I had as lief not be as live to be\nIn awe of such a开发者_高级运维 thing as I myself"


print "After quote type 'exit' to leave, or 'next' for next quote"

while control:
    quote = random.choice([SonnetSeventeen,SonnetEighteen,JCIIiii,JCIii])
    print quote[:20]
    var = raw_input ("\n")
    if var == "exit":
        control = False
    elif var == "next":
        print "\n"
        control = True

So what I'd like to be able to do is add one more elif statement saying

elif var == "answer":
     print #the full quote

Where the print statement calls to the object that was originally sliced, but prints it in full.

I have read the python library entry for the random module and found no answer.

I'm also open to more efficient ways of doing this (I know I can store the quotes in a separate file, but haven't figured that out yet).

Thanks in advance for any help!


You can use quote itself:

while control:
    quote = random.choice([SonnetSeventeen,SonnetEighteen,JCIIiii34,JCIii101])
    print quote[:20]
    var = raw_input ("\n")
    if var == "exit":
        control = False
    elif var == "next":
        print "\n"
        control = True
    elif var == "answer":
        print quote


quote[:20] doesn't destroy your original quote. So plain print quote should work!

(BTW. If you still need it, you do not have to recall the last value from random, you can just store the last value it returned)


Same answer as Emilio, but you could also use print[20:] to just print the rest of the quote that was not displayed previously. Also, to simpifly what Emilio said, you could just do:

  while True:
    quote = random.choice([SonnetSeventeen,SonnetEighteen,JCIIiii34,JCIii101])
    print quote[:20]
    var = raw_input ("\n")
    if var == "exit":
        break
    elif var == "next":
        print "\n"
    elif var == "answer":
        print quote[20:]

In the "elif var == 'next' " statement, you don't need to assign control to True since it is already True when it reaches that point.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜