我的音乐测验就此停止

时间:2015-11-13 23:06:02

标签: python

它没有出现错误,它只是没有完成它的while循环。从列表中询问一个问题,如果用户输入了答案键的相应答案,则得分为

#Music Quiz
import random

print ("Welcome To the Maths Quiz")
name = input("Please enter your name : ")
neigh = input("Please enter your area : ")
#Questions and Answers
randomQuestions = { 1 :"What is Stormzy's new single?",
                    2 :"What is Lethal Bizzles's new single?",
                    3 :"Who sang Umberella?",
                    4 :"Who sang The Hills?",
                    5 :"Who featured with MNEK to sing Never Foget You?",
                    6 :"Who is married to Kim Kardashian?",
                    7 :"What is Kanye's first childs name?",
                    8 :"What date did Wiz Khalifa and A$AP Rocky perform at the o2?"}
#Defines the questions to the answers
qAns = { "What is Stormzy's new single?" : "WickedSkengMan4",
                    "What is Lethal Bizzles's new single?" : "Dude",
                    "Who sung Umberella?" : "Rihanna",
                    "Who sung The Hills?" : "The Weekend",
                    "Who featured with MNEK to sing Never Foget You?" : "Zara Larson",
                    "Who is married to Kim Kardashian?" : "Kanye West",
                    "What is Kanye's first childs name?" : "North West",
                    "What date did Wiz Khalifa and A$AP Rocky perform at the o2?" : "17/10/15"}
#askedqs is where the aske questions are stored so they cannot be `reasked`
askedqs = {}
#While statement  makes sure it can only happen 5 times
score = 0
x = 0
#While x is less than 5 means thatonly 5 times can it be looped before it         doesnt qualify and the code moves on
 while x < 5:
 #Choses the random question from the array by selecting a number out of the amount of questions
    rand1 = random.randint(1,8)
#If the randm number is in the list that stores the asked questions, it is redifined
    if rand1 in askedqs:
        randomNum = random.randint(1,8)
        while rand1 notin askedqs:
            rand1 = random.randint(1,8)
        print(randomQuestions[rand1])
        cQuestion = randomQuestions[rand1]
    userAnswer = input("")  
    if userAnswer.lower() == qAns[rand1].lower():
            print("Well done! \n")
            score +=1
    else:
         print("Wrong answer! ", qAns[cQuestion].lower(), "\n")
         askedQs[rand1] = cQuestion
         rand1 = random.randint(1,8)
    x = x + 1
 print ("Hello World")

还有任何其他错误的帮助

2 个答案:

答案 0 :(得分:0)

我几乎改变了一切;)

#Music Quiz
import random

print("Welcome To the Maths Quiz")

name = input("Please enter your name : ")
neigh = input("Please enter your area : ")

# list of Questions and Answers
data = [
    ("What is Stormzy's new single?", "WickedSkengMan4"),
    ("What is Lethal Bizzles's new single?", "Dude"),
    ("Who sung Umberella?", "Rihanna"),
    ("Who sung The Hills?", "The Weekend"),
    ("Who featured with MNEK to sing Never Foget You?", "Zara Larson"),
    ("Who is married to Kim Kardashian?", "Kanye West"),
    ("What is Kanye's first childs name?", "North West"),
    ("What date did Wiz Khalifa and A$AP Rocky perform at the o2?", "17/10/15")
]

# number of asked question
asked_number = []

score = 0

# repeat 5 times
for _ in range(5):

    rand = random.randint(0, len(data)-1)

    while rand in asked_number:
         rand = random.randint(0, len(data)-1)

    # get question and asnwer
    question, answer = data[rand]
    answer = answer.lower()

    print(question)
    user_answer = input("").lower()

    if user_answer == answer:
        print("Well done! \n")
        score +=1
    else:
        print("Wrong answer! ", answer, "\n")
        asked_number.append(rand)

答案 1 :(得分:0)

我尝试在让它正常工作的同时尽可能少地进行更改。我还试图避免硬编码问题和冗余的数量,例如将问题定义两次,因为它可能导致以后的错误:

#Music Quiz
import random

print ("Welcome To the Maths Quiz")
name = input("Please enter your name : ")
neigh = input("Please enter your area : ")

#Questions and Answers
qAns = [ ("What is Stormzy's new single?", "WickedSkengMan4"),
        ("What is Lethal Bizzles's new single?", "Dude"),
        ("Who sung Umberella?", "Rihanna"),
        ("Who sung The Hills?", "The Weekend"),
        ("Who featured with MNEK to sing Never Foget You?", "Zara Larson"),
        ("Who is married to Kim Kardashian?", "Kanye West"),
        ("What is Kanye's first childs name?", "North West"),
        ("What date did Wiz Khalifa and A$AP Rocky perform at the o2? (DD/MM/YY)", "17/10/15") ]

#askedqs is where the aske questions are stored so they cannot be `reasked`
askedqs = {}
score = 0

#only loop 5 times
for x in range(5):
    while True:
        #Choses the random question from the array by selecting a number out of the amount of questions
        rand = random.randint(0, len(qAns)-1)
        if rand not in askedqs:
            askedqs[rand] = True
            break
    question, answer = qAns[rand]
    print(question)
    userAnswer = input("")  
    if userAnswer.lower() == answer.lower():
        print("Well done! \n")
        score +=1
    else:
         print("Wrong answer! ", answer, "\n")

print("Quiz complete,", name, "of", neigh + ",", "your score was", score)

我还修复了我认为是错误的内容,因为如果回答错误,您只会向“提问”列表添加问题。这可能是缩进错误,在Python中,空格非常重要。

我还认为您可能希望最后打印更好的内容,并使用您要求用户提供的名称和邻域。