如何在每次循环后重置te得分变量,以便它们不加起来?

时间:2016-02-29 22:18:16

标签: python

所以我已经运行了这个代码3次,因为有3个循环,但是在每个循环之后,得分变量似乎从前一个循环中添加自己,例如第一个循环(第一个用户)得分为2,然后是第二个用户获得4,最后一个用户获得6.当我想单独输出所有3个用户分数时,代码只会增加分数。因此,这会导致错误的分数。修复得分变量以重置每个循环的任何解决方案,但仍然附加到'schooldata [x] ['得分']'(x是用户2得分的用户编号将是'schooldata [1] ['得分' ]),它输出3个单独的分数,特定于每个用户。

score = 0
schooldata = []
for x in range (0,3):
    quiz = dict()
    print ("Enter your name")
    quiz['name'] = input()
    print ("what class")
    quiz['class_code'] = input()

    print("1. 9+10=")
    answer = input()
    answer = int(answer)

    if answer == 19:
        print("correct")
        score = score + 1
    else:
        print("wrong") 
    print("2. 16+40=")
    answer = input()
    answer = int(answer)
    if answer == 56:
        print("correct")
        score = score + 1
    else:
        print("wrong")

    print("3. 5+21=")
    answer = input()
    answer = int(answer) 
    if answer == 26:
        print("correct")
        score = score + 1
    else:
        print("wrong")

    print("4. 5-6=")
    answer = input()
    answer = int(answer)
    if answer == -1:
        print("correct")
        score = score + 1
    else:
        print("wrong")

    print("5. 21-9=")
    answer = input()
    answer = int(answer)

    if answer == 12:
        print("correct")
        score = score + 1
    else:
        print("wrong")


    print("6. 12-11=")
    answer = input()
    answer = int(answer)

    if answer == 1:
        print("correct")
        score = score + 1
    else:
        print("wrong")



    print("7. 5*6=")
    answer = input()
    answer = int(answer)

    if answer == 30:
        print("correct")
        score = score + 1
    else:
        print("wrong")



    print("8. 1*8=")
    answer = input()
    answer = int(answer)

    if answer == 8:
        print("correct")

        score = score + 1
    else:
        print("wrong")


    print("9. 4*6=")
    answer = input()
    answer = int(answer)

    if answer == 24:
        print("correct")
        score = score + 1

    else:
        print("wrong")



    print("10. 9*10=")
    answer = input()
    answer = int(answer)

    if answer == 90:
        print("correct")
        score = score + 1
    else:
        print("wrong")
    quiz['score'] = score
    schooldata.append(quiz)

user1=("name - ", schooldata[0]['name'],", user score - ", schooldata[0]['score'],", class number - ", schooldata[0]['class_code'])
print ("if you want student 1's results and information, please type 'user[0][0]'")
user = input()
if user == "user1":
    print ("name - ", schooldata[0]['name'],", user score - ", schooldata[0]['score'],", class number - ", schooldata[0]['class_code'])

0 个答案:

没有答案