Trivia游戏随机化

时间:2019-02-18 13:29:04

标签: python

我想将问题随机化,但是当我设置一个列表并添加随机选择功能时,它只是使它陷入了反复打印相同问题的无限循环中。

我试图创建一个列表,并告诉它选择10个随机选择,然后重复一次。因此,我删除了重复代码,然后向您展示我现在拥有的内容,但是其中不再包含列表和random.choice。

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,所以请向我解释一下您要做什么的每个部分。

1 个答案:

答案 0 :(得分:1)

尽管它不是codereview,但这只是不好的做法,这可能是您无法弄清楚将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)

请注意:

  1. 问题类代表一个问题,并已实现,因此可以在主体中声明,您也可以从文本文件或数据库中读取问题,但这似乎已经足够
  2. 我使用shuffle来随机整理问题列表,您可以使用列表切片来限制问题的数量,例如:for num, question in enumerate(shuffle(questions)[:10]):

其他阅读建议:

希望有帮助,

祝你好运!