Python 琐事游戏随机化
我想随机化我的问题,但当我建立一个列表并添加随机选择功能时,它只是把它放入一个无限循环,反复打印同一个问题 我试着创建一个列表,让它随机选择10个选项,结果重复了一遍。因此,我删除了重复代码,我将向您展示我现在拥有的,但其中不再包含列表和random.choicePython 琐事游戏随机化,python,Python,我想随机化我的问题,但当我建立一个列表并添加随机选择功能时,它只是把它放入一个无限循环,反复打印同一个问题 我试着创建一个列表,让它随机选择10个选项,结果重复了一遍。因此,我删除了重复代码,我将向您展示我现在拥有的,但其中不再包含列表和random.choice def main(): import time import sys import random name1 = input("What is your first name:") mes
def main():
import time
import sys
import random
name1 = input("What is your first name:")
message1 = "Welcome, %s"
print(message1 % (name1))
time.sleep(1.5)
question1 = "Which US state is closest to Russia?"
print(question1)
print("A: Hawaii")
time.sleep(1)
print("B: Alaska")
time.sleep(1)
print("C: Oregon")
time.sleep(1)
ans1 = input("What is your answer (A/B/C):")
time.sleep(1)
question2 = "Which reggae singer star died in 1981?"
print(question2)
print("A: Peter Tosh")
time.sleep(1)
print("B: Bob Marley")
time.sleep(1)
print("C: Jimmy Cliff")
time.sleep(1)
ans2 = input("What is your answer (A/B/C):")
time.sleep(1)
question3 = "In which city was Martin Luther King, Jr. assasinated?"
print(question3)
print("A: Nashville, TN")
time.sleep(1)
print("B: Memphis, TN")
time.sleep(1)
print("C: Savannah, GA")
time.sleep(1)
ans3 = input("What is your answer (A/B/C):")
time.sleep(1)
question4 = "What is the smallest member of the flute family?"
print(question4)
print("A: Concert flute")
time.sleep(1)
print("B: Soprano flute")
time.sleep(1)
print("C: Piccolo")
time.sleep(1)
ans4 = input("What is your answer (A/B/C):")
time.sleep(1)
question5 = "What is the national animal of Canada?"
print(question5)
print("A: Moose")
time.sleep(1)
print("B: Beaver")
time.sleep(1)
print("C: Horse")
time.sleep(1)
ans5 = input("What is your answer (A/B/C):")
time.sleep(1)
question6 = "What does RAM stand for when it comes to computers?"
print(question6)
print("A: Random Access Memory")
time.sleep(1)
print("B: Random Article Meme")
time.sleep(1)
print("C: Random Archival Memory")
time.sleep(1)
ans6 = input("What is your answer (A/B/C):")
time.sleep(1)
question7 = "Which philosopher said, 'I think, therefore I am'?"
print(question7)
print("A: Rene Descartes")
time.sleep(1)
print("B: John Locke")
time.sleep(1)
print("C: Plato")
time.sleep(1)
ans7 = input("What is your answer (A/B/C):")
time.sleep(1)
question8 = "Which country has the longest coastline?"
print(question8)
print("A: The United States")
time.sleep(1)
print("B: Australia")
time.sleep(1)
print("C: Canada")
time.sleep(1)
ans8 = input("What is your answer (A/B/C):")
time.sleep(1)
question9 = "What is the name of a male swan?"
print(question9)
print("A: cob")
time.sleep(1)
print("B: pen")
time.sleep(1)
print("C: cygnet")
time.sleep(1)
ans9 = input("What is your answer (A/B/C):")
time.sleep(1)
question10 = "What is the capital of Australia?"
print(question10)
print("A: Sydney")
time.sleep(1)
print("B: Melbourne")
time.sleep(1)
print("C: Canberra")
time.sleep(1)
ans10 = input("What is your answer (A/B/C):")
time.sleep(1)
print("Let's tally up what you got correct!")
time.sleep(2)
correct = 0
wrong = 0
accuracy = 0
if ans1 == 'b':
print("1. is correct!")
correct = correct + 1
accuracy = accuracy + 10
time.sleep(1)
elif ans1 == 'B':
print("1. is correct!")
correct = correct + 1
accuracy = accuracy + 10
time.sleep(1)
elif ans1 == 'B.':
print("1. is correct!")
correct = correct + 1
accuracy = accuracy + 10
time.sleep(1)
elif ans1 == 'b.':
print("1. is correct!")
correct = correct + 1
accuracy = accuracy + 10
time.sleep(1)
elif ans1 == 'a':
print("1. is wrong! The answer is: B")
wrong = wrong + 1
time.sleep(1)
elif ans1 == 'A':
print("1. is wrong! The answer is: B")
wrong = wrong + 1
time.sleep(1)
elif ans1 == 'A.':
print("1. is wrong! The answer is: B")
wrong = wrong + 1
time.sleep(1)
elif ans1 == 'a.':
print("1. is wrong! The answer is: B")
wrong = wrong + 1
time.sleep(1)
elif ans1 == 'c':
print("1. is wrong! The answer is: B")
wrong = wrong + 1
time.sleep(1)
elif ans1 == 'C':
print("1. is wrong! The answer is: B")
wrong = wrong + 1
time.sleep(1)
elif ans1 == 'C.':
print("1. is wrong! The answer is: B")
wrong = wrong + 1
time.sleep(1)
elif ans1 == 'c.':
print("1. is wrong! The answer is: B")
wrong = wrong + 1
time.sleep(1)
else:
print("QUESTION 1. UNKNOWN ERROR")
wrong = wrong + 1
time.sleep(1)
if ans2 == 'b':
print("2. is correct!")
correct = correct + 1
accuracy = accuracy + 10
time.sleep(1)
elif ans2 == 'B':
print("2. is correct!")
correct = correct + 1
accuracy = accuracy + 10
time.sleep(1)
elif ans2 == 'B.':
print("2. is correct!")
correct = correct + 1
accuracy = accuracy + 10
time.sleep(1)
elif ans2 == 'b.':
print("2. is correct!")
correct = correct + 1
accuracy = accuracy + 10
time.sleep(1)
elif ans2 == 'a':
print("2. is wrong! The answer is: B")
wrong = wrong + 1
time.sleep(1)
elif ans2 == 'A':
print("2. is wrong! The answer is: B")
wrong = wrong + 1
time.sleep(1)
elif ans2 == 'A.':
print("2. is wrong! The answer is: B")
wrong = wrong + 1
time.sleep(1)
elif ans2 == 'a.':
print("2. is wrong! The answer is: B")
wrong = wrong + 1
time.sleep(1)
elif ans2 == 'c':
print("2. is wrong! The answer is: B")
wrong = wrong + 1
time.sleep(1)
elif ans2 == 'C':
print("2. is wrong! The answer is: B")
wrong = wrong + 1
time.sleep(1)
elif ans2 == 'C.':
print("2. is wrong! The answer is: B")
wrong = wrong + 1
time.sleep(1)
elif ans2 == 'c.':
print("2. is wrong! The answer is: B")
wrong = wrong + 1
time.sleep(1)
else:
print("QUESTION 2. UNKNOWN ERROR")
wrong = wrong + 1
time.sleep(1)
if ans3 == 'b':
print("3. is correct!")
correct = correct + 1
accuracy = accuracy + 10
time.sleep(1)
elif ans3 == 'B':
print("3. is correct!")
correct = correct + 1
accuracy = accuracy + 10
time.sleep(1)
elif ans3 == 'B.':
print("3. is correct!")
correct = correct + 1
accuracy = accuracy + 10
time.sleep(1)
elif ans3 == 'b.':
print("3. is correct!")
correct = correct + 1
accuracy = accuracy + 10
time.sleep(1)
elif ans3 == 'a':
print("3. is wrong! The answer is: B")
wrong = wrong + 1
time.sleep(1)
elif ans3 == 'A':
print("3. is wrong! The answer is: B")
wrong = wrong + 1
time.sleep(1)
elif ans3 == 'A.':
print("3. is wrong! The answer is: B")
wrong = wrong + 1
time.sleep(1)
elif ans3 == 'a.':
print("3. is wrong! The answer is: B")
wrong = wrong + 1
time.sleep(1)
elif ans3 == 'c':
print("3. is wrong! The answer is: B")
wrong = wrong + 1
time.sleep(1)
elif ans3 == 'C':
print("3. is wrong! The answer is: B")
wrong = wrong + 1
time.sleep(1)
elif ans3 == 'C.':
print("3. is wrong! The answer is: B")
wrong = wrong + 1
time.sleep(1)
elif ans3 == 'c.':
print("3. is wrong! The answer is: B")
wrong = wrong + 1
time.sleep(1)
else:
print("QUESTION 3. UNKNOWN ERROR")
wrong = wrong + 1
time.sleep(1)
if ans4 == 'c':
print("4. is correct!")
correct = correct + 1
accuracy = accuracy + 10
time.sleep(1)
elif ans4 == 'C':
print("4. is correct!")
correct = correct + 1
accuracy = accuracy + 10
time.sleep(1)
elif ans4 == 'C.':
print("4. is correct!")
correct = correct + 1
accuracy = accuracy + 10
time.sleep(1)
elif ans4 == 'c.':
print("4. is correct!")
correct = correct + 1
accuracy = accuracy + 10
time.sleep(1)
elif ans4 == 'a':
print("4. is wrong! The answer is: C")
wrong = wrong + 1
time.sleep(1)
elif ans4 == 'A':
print("4. is wrong! The answer is: C")
wrong = wrong + 1
time.sleep(1)
elif ans4 == 'A.':
print("4. is wrong! The answer is: C")
wrong = wrong + 1
time.sleep(1)
elif ans4 == 'a.':
print("4. is wrong! The answer is: C")
wrong = wrong + 1
time.sleep(1)
elif ans4 == 'b':
print("4. is wrong! The answer is: C")
wrong = wrong + 1
time.sleep(1)
elif ans4 == 'B':
print("4. is wrong! The answer is: C")
wrong = wrong + 1
time.sleep(1)
elif ans4 == 'B.':
print("4. is wrong! The answer is: C")
wrong = wrong + 1
time.sleep(1)
elif ans4 == 'b.':
print("4. is wrong! The answer is: C")
wrong = wrong + 1
time.sleep(1)
else:
print("QUESTION 4. UNKNOWN ERROR")
wrong = wrong + 1
time.sleep(1)
if ans5 == 'b':
print("5. is correct!")
correct = correct + 1
accuracy = accuracy + 10
time.sleep(1)
elif ans5 == 'B':
print("5. is correct!")
correct = correct + 1
accuracy = accuracy + 10
time.sleep(1)
elif ans5 == 'B.':
print("5. is correct!")
correct = correct + 1
accuracy = accuracy + 10
time.sleep(1)
elif ans5 == 'b.':
print("5. is correct!")
correct = correct + 1
accuracy = accuracy + 10
time.sleep(1)
elif ans5 == 'c':
print("5. is wrong! The answer is: B")
wrong = wrong + 1
time.sleep(1)
elif ans5 == 'C':
print("5. is wrong! The answer is: B")
wrong = wrong + 1
time.sleep(1)
elif ans5 == 'C.':
print("5. is wrong! The answer is: B")
wrong = wrong + 1
time.sleep(1)
elif ans5 == 'c.':
print("5. is wrong! The answer is: B")
wrong = wrong + 1
time.sleep(1)
elif ans5 == 'a':
print("5. is wrong! The answer is: B")
wrong = wrong + 1
time.sleep(1)
elif ans5 == 'A':
print("5. is wrong! The answer is: B")
wrong = wrong + 1
time.sleep(1)
elif ans5 == 'A.':
print("5. is wrong! The answer is: B")
wrong = wrong + 1
time.sleep(1)
elif ans5 == 'a.':
print("5. is wrong! The answer is: B")
wrong = wrong + 1
time.sleep(1)
else:
print("QUESTION 5. UNKNOWN ERROR")
wrong = wrong + 1
time.sleep(1)
if ans6 == 'a':
print("6. is correct!")
correct = correct + 1
accuracy = accuracy + 10
time.sleep(1)
elif ans6 == 'A':
print("6. is correct!")
correct = correct + 1
accuracy = accuracy + 10
time.sleep(1)
elif ans6 == 'A.':
print("6. is correct!")
correct = correct + 1
accuracy = accuracy + 10
time.sleep(1)
elif ans6 == 'a.':
print("6. is correct!")
correct = correct + 1
accuracy = accuracy + 10
time.sleep(1)
elif ans6 == 'b':
print("6. is wrong! The answer is: A")
wrong = wrong + 1
time.sleep(1)
elif ans6 == 'B':
print("6. is wrong! The answer is: A")
wrong = wrong + 1
time.sleep(1)
elif ans6 == 'B.':
print("6. is wrong! The answer is: A")
wrong = wrong + 1
time.sleep(1)
elif ans6 == 'b.':
print("6. is wrong! The answer is: A")
wrong = wrong + 1
time.sleep(1)
elif ans6 == 'c':
print("6. is wrong! The answer is: A")
wrong = wrong + 1
time.sleep(1)
elif ans6 == 'C':
print("6. is wrong! The answer is: A")
wrong = wrong + 1
time.sleep(1)
elif ans6 == 'C.':
print("6. is wrong! The answer is: A")
wrong = wrong + 1
time.sleep(1)
elif ans6 == 'c.':
print("6. is wrong! The answer is: A")
wrong = wrong + 1
time.sleep(1)
else:
print("QUESTION 6. UNKNOWN ERROR")
wrong = wrong + 1
time.sleep(1)
if ans7 == 'a':
print("7. is correct!")
correct = correct + 1
accuracy = accuracy + 10
time.sleep(1)
elif ans7 == 'A':
print("7. is correct!")
correct = correct + 1
accuracy = accuracy + 10
time.sleep(1)
elif ans7 == 'A.':
print("7. is correct!")
correct = correct + 1
accuracy = accuracy + 10
time.sleep(1)
elif ans7 == 'a.':
print("7. is correct!")
correct = correct + 1
accuracy = accuracy + 10
time.sleep(1)
elif ans7 == 'b':
print("7. is wrong! The answer is: A")
wrong = wrong + 1
time.sleep(1)
elif ans7 == 'B':
print("7. is wrong! The answer is: A")
wrong = wrong + 1
time.sleep(1)
elif ans7 == 'B.':
print("7. is wrong! The answer is: A")
wrong = wrong + 1
time.sleep(1)
elif ans7 == 'b.':
print("7. is wrong! The answer is: A")
wrong = wrong + 1
time.sleep(1)
elif ans7 == 'c':
print("7. is wrong! The answer is: A")
wrong = wrong + 1
time.sleep(1)
elif ans7 == 'C':
print("7. is wrong! The answer is: A")
wrong = wrong + 1
time.sleep(1)
elif ans7 == 'C.':
print("7. is wrong! The answer is: A")
wrong = wrong + 1
time.sleep(1)
elif ans7 == 'c.':
print("7. is wrong! The answer is: A")
wrong = wrong + 1
time.sleep(1)
else:
print("QUESTION 7. UNKNOWN ERROR")
wrong = wrong + 1
time.sleep(1)
if ans8 == 'c':
print("8. is correct!")
correct = correct + 1
accuracy = accuracy + 10
time.sleep(1)
elif ans8 == 'C':
print("8. is correct!")
correct = correct + 1
accuracy = accuracy + 10
time.sleep(1)
elif ans8 == 'C.':
print("8. is correct!")
correct = correct + 1
accuracy = accuracy + 10
time.sleep(1)
elif ans8 == 'c.':
print("8. is correct!")
correct = correct + 1
accuracy = accuracy + 10
time.sleep(1)
elif ans8 == 'a':
print("8. is wrong! The answer is: C")
wrong = wrong + 1
time.sleep(1)
elif ans8 == 'A':
print("8. is wrong! The answer is: C")
wrong = wrong + 1
time.sleep(1)
elif ans8 == 'A.':
print("8. is wrong! The answer is: C")
wrong = wrong + 1
time.sleep(1)
elif ans8 == 'a.':
print("8. is wrong! The answer is: C")
wrong = wrong + 1
time.sleep(1)
elif ans8 == 'b':
print("8. is wrong! The answer is: C")
wrong = wrong + 1
time.sleep(1)
elif ans8 == 'B':
print("8. is wrong! The answer is: C")
wrong = wrong + 1
time.sleep(1)
elif ans8 == 'B.':
print("8. is wrong! The answer is: C")
wrong = wrong + 1
time.sleep(1)
elif ans8 == 'b.':
print("8. is wrong! The answer is: C")
wrong = wrong + 1
time.sleep(1)
else:
print("QUESTION 8. UNKNOWN ERROR")
wrong = wrong + 1
time.sleep(1)
if ans9 == 'a':
print("9. is correct!")
correct = correct + 1
accuracy = accuracy + 10
time.sleep(1)
elif ans9 == 'A':
print("9. is correct!")
correct = correct + 1
accuracy = accuracy + 10
time.sleep(1)
elif ans9 == 'A.':
print("9. is correct!")
correct = correct + 1
accuracy = accuracy + 10
time.sleep(1)
elif ans9 == 'a.':
print("9. is correct!")
correct = correct + 1
accuracy = accuracy + 10
time.sleep(1)
elif ans9 == 'b':
print("9. is wrong! The answer is: A")
wrong = wrong + 1
time.sleep(1)
elif ans9 == 'B':
print("9. is wrong! The answer is: A")
wrong = wrong + 1
time.sleep(1)
elif ans9 == 'B.':
print("9. is wrong! The answer is: A")
wrong = wrong + 1
time.sleep(1)
elif ans9 == 'b.':
print("9. is wrong! The answer is: A")
wrong = wrong + 1
time.sleep(1)
elif ans9 == 'c':
print("9. is wrong! The answer is: A")
wrong = wrong + 1
time.sleep(1)
elif ans9 == 'C':
print("9. is wrong! The answer is: A")
wrong = wrong + 1
time.sleep(1)
elif ans9 == 'C.':
print("9. is wrong! The answer is: A")
wrong = wrong + 1
time.sleep(1)
elif ans9 == 'c.':
print("9. is wrong! The answer is: A")
wrong = wrong + 1
time.sleep(1)
else:
print("QUESTION 9. UNKNOWN ERROR")
wrong = wrong + 1
if ans10 == 'c':
print("10. is correct!")
correct = correct + 1
accuracy = accuracy + 10
time.sleep(1)
elif ans10 == 'C':
print("10. is correct!")
correct = correct + 1
accuracy = accuracy + 10
time.sleep(1)
elif ans10 == 'C.':
print("10. is correct!")
correct = correct + 1
accuracy = accuracy + 10
time.sleep(1)
elif ans10 == 'c.':
print("10. is correct!")
correct = correct + 1
accuracy = accuracy + 10
time.sleep(1)
elif ans10 == 'a':
print("10. is wrong! The answer is: C")
wrong = wrong + 1
time.sleep(1)
elif ans10 == 'A':
print("10. is wrong! The answer is: C")
wrong = wrong + 1
time.sleep(1)
elif ans10 == 'A.':
print("10. is wrong! The answer is: C")
wrong = wrong + 1
time.sleep(1)
elif ans10 == 'a.':
print("10. is wrong! The answer is: C")
wrong = wrong + 1
time.sleep(1)
elif ans10 == 'b':
print("10 is wrong! The answer is: C")
wrong = wrong + 1
time.sleep(1)
elif ans10 == 'B':
print("10. is wrong! The answer is: C")
wrong = wrong + 1
time.sleep(1)
elif ans10 == 'B.':
print("10. is wrong! The answer is: C")
wrong = wrong + 1
time.sleep(1)
elif ans10 == 'b.':
print("10. is wrong! The answer is: C")
wrong = wrong + 1
time.sleep(1)
else:
print("QUESTION 10. UNKNOWN ERROR")
wrong = wrong + 1
time.sleep(1)
percent = "%"
message2 = "%s, you got a score of %s correct and %s wrong."
print(message2 % (name1, correct, wrong))
time.sleep(2.7)
message3 = "You get a score of %s out of 10!"
print(message3 % (correct))
time.sleep(2)
message6 = "%s, you have a %s%s accuracy rate!"
print(message6 % (name1, accuracy, percent))
time.sleep(1.5)
rerun1 = input("Do you want to do it again? (Y/N):")
if rerun1 == 'Y':
main()
elif rerun1 == 'y':
main()
elif rerun1 == 'N':
message4 = "Ok, goodbye, %s!"
print(message4 % (name1))
time.sleep(1)
sys.exit()
elif rerun1 == 'n':
message5 = "Ok, goodbye, %s!"
print(message5 % (name1))
time.sleep(1)
sys.exit()
else:
print("INVALID INPUT")
sys.exit()
我想让它做的是将问题随机化,然后在底部附近运行if语句。不过我不想让它太复杂,因为我一个月前才开始使用Python,所以请向我解释一下您将要做的每一部分都是什么。虽然不是这样,但这段代码只是一种糟糕的做法,可能是您无法找出使其随机处理N个问题的逻辑的原因
您的代码中有大量重复使用的代码部分,因此我的建议是构建一个问题类,然后生成问题,例如:
# Most basic Trivia
import time
import sys
import shuffle
# Define
CORRECT_ANSWER = 1
class Question:
def __init__(self,question, answers, correct_answer)::
self.question = question
self.answers = answers
self.correct = correct_answer
def ask(self, num_of_question):
print('Question #{}:\n{}.format'(num_of_question, self.question))
for ind, answer in enumerate(answers):
print('{}.{}'.format(ind + 1, answer))
time.sleep(1)
ans = input("What is your answer (1/2/3):")
if ans - 1 == str(self.correct):
return CORRECT_ANSWER
def main():
curr_score = 0
questions = list()
questions.append(Question('Which US state is closest to Russia?', ['Hawaii', 'Alaska', 'Oregon'], 1))
questions.append(Question(.....))
# Keep adding all questions...
# Then:
for num, question in enumerate(shuffle(questions)):
curr_score += question.ask(num + 1)
请注意:
对于num,枚举中的问题(shuffle(Questions)[:10]):
其他建议:
祝你好运 700行不是一个好主意。请将代码减少到演示问题所需的数量。请在[“c”、“c”、“c.”、“c”]中使用类似于:if ans10的语句,而不是使用具有相同代码的4个不同if语句来执行。如果你重写你的代码,你可以很容易地删除400多行代码。你好!你需要重写你的代码。当您有多个类似对象(例如问题字符串)时,请使用列表,并使用函数进行重复操作。一个用于过滤输入的函数、一个包含与正确答案链接的问题的数组以及一个用于检查问题是否有正确答案的函数可将此代码减少约500行