如何选择随机测验12次并以不同方式显示我的测验结果分数

时间:2015-03-15 19:13:31

标签: python arrays printing

经过数小时的研究,我很难编写这样的标准:

#select random question 12 times 
#Display highest to lowest score in (alphabetical order) 
#Display highest to lowest score 
#Display highest to lowest average

#However my quiz's coding is able to save the players name in a text file     (images will be provided) and score as well as the quiz works perfectly

#MY PYTHON QUIZ FILE AND RESULTS OF QUIZ IN TEXT FILE IN TERMS OF CLASS          IMAGE :http://gyazo.com/817bd04f8c28e66ee4c9e1f5142b01df

##THIS IS MY CODE : http://pastebin.com/bdHCcvxt

#Sorry i wasnt able to paste my code on as there was an error code
 on the site so please visit the link I provided. Thanks a lot.

1 个答案:

答案 0 :(得分:0)

您正在使用input()输入字符串,而是使用raw_input()

这是修改后的源代码:

name=raw_input ("what is your name?")
print ("Welcome", name, ", I am heisenberg and this is my quiz")       
begin = raw_input ("To begin enter 'heisenberg''")
if begin == ("heisenberg"):
    print ("Alright GOODLUCK!")
    score = 0

    class_no = ""
    while class_no not in ["1", "2", "3"]:
        class_no = raw_input("Please enter your class - 1, 2 or 3:")  # Asks the user for an input

print ("What is 5*10")
answer = raw_input ("Enter Answer: ")
if answer == "50":
    print ("Correct")
    score = score  + 1
else:
    print ("Incorrect")
    score =  score - 0

print ("What is 5*2")
answer = raw_input ("Enter Answer: ")
if answer == "10":
    print ("Correct")
    score = score  + 1
else:
    print ("Incorrect")
    score =  score - 0

print ("What is 5*3")
answer = raw_input ("Enter Answer: ")
if answer == "15":
    print ("Correct")
    score = score + 1
else:
    print ("Incorrect")
    score = score - 0

print ("What is 22+ 84")
answer = raw_input ("Enter Answer: ")
if answer == "106":
    print ("Correct")
    score = score + 1
else:
    print ("Incorrect")
    score = score - 0 

print ("What is 20/2")
answer = raw_input ("Enter Answer: ")
if answer == "10":
    print ("Correct")
    score = score + 1
else:
    print ("Incorrect")
    score = score  - 0

print ("What is 20/5")
answer = raw_input ("Enter Answer: ")
if answer == "4":
    print ("Correct")
    score = score + 1
else:
    print ("Incorrect")
    score = score - 0

print ("What is 1.1*4 ")
answer = raw_input ("Enter Answer: ")
if answer == "4.4":
    print ("Correct")
    score = score  + 1
else:
    print ("Incorrect")
    score = score - 1

print ("What is 7/7")
answer = raw_input ("Enter Answer: ")
if answer == "1":
    print ("Correct")
    score = score + 1
else:
    print ("Incorrect")
    score = score - 0

print ("What is 90-9")
answer = raw_input ("Enter Answer: ")


if answer == "81":
    print ("Correct")
    score = score + 1
else:
    print ("Incorrect")
    score = score - 0

print ("What is 41+51")
answer = raw_input ("Enter Answer: ")
if answer == "92":
    print ("Correct")
    score = score + 1
else:
    print ("Incorrect")
    score = score - 0

print ("What is 1 - 5")
answer = raw_input ("Enter Answer: ")
if answer == "-4":
    print ("Correct")
    score = score + 1
else:
    print ("Incorrect")
    score = score - 0

print ("What is 10/2")
answer = raw_input ("Enter Answer: ")
if answer == "5":
    print ("Correct")
    score = score + 1
else:
    print ("Incorrect")


print ("Thank you for playing my quiz", name,"you have scored,", score,"out of 12 points")

with open("class%s.txt" % class_no, "a") as my_class:
        my_class.write("{0}\n".format([name, score]))

这是输出:

what is your name?suraj
('Welcome', 'suraj', ', I am heisenberg and this is my quiz')

To begin enter 'heisenberg''heisenberg
Alright GOODLUCK!

Please enter your class - 1, 2 or 3:1
What is 5*10

Enter Answer: 50
Correct
What is 5*2

Enter Answer: 10
Correct
What is 5*3

Enter Answer: 15
Correct
What is 22+ 84

Enter Answer: 106
Correct
What is 20/2

Enter Answer: 10
Correct
What is 20/5

Enter Answer: 4
Correct
What is 1.1*4 

Enter Answer: 4.4
Correct
What is 7/7

Enter Answer: 1
Correct
What is 90-9

Enter Answer: 81
Correct
What is 41+51

Enter Answer: 92
Correct
What is 1 - 5

Enter Answer: -4
Correct
What is 10/2

Enter Answer: 5
Correct
('Thank you for playing my quiz', 'suraj', 'you have scored,', 12, 'out of 12 points')

注意:您可能希望使用字典或列表存储问题和答案,然后循环浏览它们并比较相应的答案,而不是手动输入每个问题和每个答案。