Py学习  »  Python

如何在python中添加评分系统

Big Jaheed Sanjeev • 6 年前 • 524 次点击  

当你得到一个正确的答案时,我怎样才能得到这个代码来打分呢?例如,我会输入正确的东西,但我不知道如何让它认识到它的正确性并加3分。我该怎么做?

def main():
    print ("WELCOME TO THE MENU")
    print ("===================")
    print ("Please choose an option")
    print ("1. Login")
    print ("2. Add")
    print ("3. Quiz")
    print ("===================")

    option = int(input("Option: "))

    if option == 1:
        login()
    elif option == 2:
        register()
    elif option == 3:
        quiz()
    else:
        print ("Incorrect option")
        main()

def login():
    print ("==== Login ====")
    choice=input("Username:")
    choice=input("Password:")


def register():
    print ("==== Add ====")
    choice=input("Username:")
    choice=input("Password:")
    print("Waiting for approval...")
def quiz():
    print ("==== Quiz ====")
    import random
    list1 = []
    list2 = []

    # reads in list of data
    with open("new.txt", "r") as f:
        for line in f.readlines():
            l1, l2 = line.strip().split(',')
            list1.append(l1)
            list2.append(l2)

    # randomly selects a value
    rand = random.randint(0,3)
    print ("Data:" + list1[rand])

    # finds only first letters from words
    words = list2[rand].split()
    letters = [word[0] for word in words]
    print ("".join(letters))

    # writeFile.py
    # allow user to input data and write out to file

    name = input("What is your name: ")
    colour = input("What is your favourite colour? ")

    file = open("list.txt", "a")
    file.write(name+',')
    file.write(colour+'\n')
    file.close()

    file = open('list.txt', 'a')
    info = input('Enter info: ')
    file.write(info+'\n')
    file.close()


    main()

请帮我纠正这一点,我很感谢任何帮助,因为它可以解决我的问题。请注意

Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/38190
文章 [ 1 ]  |  最新文章 6 年前
DanDeg
Reply   •   1 楼
DanDeg    7 年前

正如错误消息所说,您的列表索引超出范围。这意味着当你从 new.txt ,少于三项。