我对python非常陌生,我需要使它成为我的程序从琐事游戏中随机选择一个问题,但不会在之后重复。 该游戏基于一本关于我收到的python的书中的问题。我如何从我的代码中随机选择一行而不重复
print "trivia game"
begin = raw_input("Would you like to begin?: ")
if begin == "Yes":
print "A) Getting your computer to do stuff"
print "B) A form of watching television"
print "C) Building a computer"
print "D) Assembling a computer with your bare hands"
q1 = raw_input("What is programming?: ")
if q1 == "A" or q1 == "a":
print "Well done you got it right! +1"
print " "
else:
print " no, you're wrong! +0"
print "A) African Standard Code For Information Interchange"
print "B) American Standard Code For Information Interchange"
print "C) American Subtle Codes For Information Interchange"
print "D) African Subtle Codes For Inside Interchange"
print " "
q2 = raw_input("What does ASCII stand for?: ")
if q2 == "b" or q2 == "B":
print "Well done! You got it right! "
print " "
else:
print "no, you're wrong! +0"
print "A) A hidden group of words"
print "B) A group of random letters"
print "C) The generation of random numbers by a definite computational process. "
print "D) The way to write code"
q3 = raw_input("What is pseudorandom?: ")
if q3 == "C" or q3 == "c":
print "Well done you got it right! +1"
print " "
else:
print " no, you're wrong! +0"
print "A) Only strings and loops"
print "B) Only loops"
print "C) Only strings"
print "D) Any type"
q4 = raw_input("What kinds of elements can you store with a tuple?: ")
if q4 == "D" or q4 == "d":
print "Well done you got it right! +1"
print " "
else:
print " no, you're wrong! +0"
print "A) Yes"
print "B) No"
print "C) Only if they are loops"
print "D) Only if they are strings"
q5 = raw_input("Do variables store the values you assign to them?: ")
if q5 == "A" or q5 == "a":
print "Well done you got it right! +1"
print " "
else:
print " no, you're wrong! +0"
print "A) One"
print "B) There is no limit"
print "C) 158,240"
print "D) 1000"
q6 = raw_input("How many paramters can a function have?: ")
if q6 == "B" or q6 == "b":
print "Well done you got it right! +1"
print " "
else:
print " no, you're wrong! +0"
print "A) Lists of numbers"
print "B) Complex information"
print "C) Simple information"
print "D) Loops"
q7 = raw_input("What type of information wold be best stored in a text file?: ")
if q7 == "C" or q7 == "c":
print "Well done you got it right! +1"
print " "
else:
print " no, you're wrong! +0"
print "A) To write a program with over 1000 lines of code"
print "B) To create a program that write programs"
print "C) To create program after program"
print "D) To create an instance of an object in an object-oriented programming (OOP) language."
q8 = raw_input("What does instantiate mean?: ")
if q8 == "D" or q8 == "d":
print "Well done you got it right! +1"
print " "
else:
print " no, you're wrong! +0"
print "A) It allows you to send the same message to objects of different classes related by inheritance and achieve different and appropriate results"
print "B) It lets you combine two programs"
print "C) It creates polygons"
print "D) It allows you to repeat a loop an infinite amount of times"
q9 = raw_input("How can polymorphism help a programmer?: ")
if q9 == "A" or q9 == "a":
print "Well done you got it right! +1"
print " "
else:
print " no, you're wrong! +0"
print "A) grid()"
print "B) create_widgets()"
print "C) root.title"
print "D) root.mainloop()"
q10 = raw_input("What widget method sets a widget's options?: ")
if q10 == "B" or q10 == "b":
print "Well done you got it right! +1"
print " "
else:
print " no, you're wrong! +0"
print "A) A compass"
print "B) A graph"
print "C) A grid"
print "D) A virtual loop system"
q11 = raw_input("What is a graphics coordinate system?: ")
if q11 == "C" or q11 == "c":
print "Well done you got it right! +1"
print " "
else:
print " no, you're wrong! +0"
print "A) A file with text about sound"
print "B) A looping sound"
print "C) A radio channel"
print "D) Where a sound is stored"
q12 = raw_input("What's a sound channel?: ")
if q12 == "D" or q12 == "d":
print "Well done you got it right! +1"
print " "
else:
print " no, you're wrong! +0"
print "A) True"
print "B) False"
q13 = raw_input("Can Python increase programmer productivity?: ")
if q13 == "a" or q13 == "A":
print "Well done you got it right! +1"
print " "
else:
print " no, you're wrong! +0"
print "A) True"
print "B) False"
q14 = raw_input("It's not important to use the right types in your programs.: ")
if q14 == "b" or q14 == "B":
print "Well done you got it right! +1"
print " "
else:
print " no, you're wrong! +0"
print "A) True"
print "B) False"
q15 = raw_input("You should plan your programs.: ")
if q15 == "A" or q15 == "a":
print "Well done you got it right! +1"
print " "
else:
print " no, you're wrong! +0"
print "A) True"
print "B) False"
q16 = raw_input("Constants aren't helpful.: ")
if q16 == "b" or q16 == "B":
print "Well done you got it right! +1"
print " "
else:
print " no, you're wrong! +0"
print "A) True"
print "B) False"
q17 = raw_input("Dictionary keys must be unique.: ")
if q17 == "a" or q17 == "A":
print "Well done you got it right! +1"
print " "
else:
print " no, you're wrong! +0"
print "A) True"
print "B) False"
q18 = raw_input("You should never use docstrings.: ")
if q18 == "b" or q18 == "B":
print "Well done you got it right! +1"
print " "
else:
print " no, you're wrong! +0"
print "A) True"
print "B) False"
q19 = raw_input("You should avoid a catch-all exception handling.: ")
if q19 == "a" or q19 == "A":
print "Well done you got it right! +1"
print " "
else:
print " no, you're wrong! +0"
print "A) True"
print "B) False"
q20 = raw_input("An object should never update its private attributes.: ")
if q20 == "b" or q20 == "B":
print "Well done you got it right! +1"
print " "
else:
print " no, you're wrong! +0"
print "A) True"
print "B) False"
q21 = raw_input("You should include only related programming elements in a single module.: ")
if q21 == "a" or q21 == "A":
print "Well done you got it right! +1"
print " "
else:
print " no, you're wrong! +0"
print "A) True"
print "B) False"
q22 = raw_input("You can't test which radio button in a group is selected.: ")
if q22 == "b" or q22 == "B":
print "Well done you got it right! +1"
print " "
else:
print " no, you're wrong! +0"
print "A) True"
print "B) False"
q23 = raw_input("You should create a batch file to run your graphics programs.: ")
if q23 == "a" or q23 == "A":
print "Well done you got it right! +1"
print " "
else:
print " no, you're wrong! +0"
print "A) True"
print "B) False"
q24 = raw_input("There is no such thing as a sound channel.: ")
if q24 == "b" or q24 == "B":
print "Well done you got it right! +1"
print " "
else:
print " no, you're wrong! +0"
else:
print "Invalid answer."
看看'while'和'for'循环:) –
我强烈建议不要将你的问题硬编码到python脚本中。 – sshashank124