首页 > 解决方案 > Python - 不会为员工开启或关闭时钟,也不会写入 csv 文件

问题描述

我的代码首先从文件中读取以确保用户名和密码正确。之后,它会要求用户输入他们是否想要开启或关闭时钟。用户下班后,应该将小时数保存到 CSV 文件中,但这并没有发生。我不能为此任务使用“导入 csv”。另外,这段代码是否适合时钟的开启和关闭,或者我应该改变什么?谢谢

role = input("Choose role [E]mployee / [P]ayroll clerk [Default]:")

if role == "E" or role == "e":

    print("Please enter your details to log in:")
    print()
    user_name1 = input("Please enter your username: ")
    pass_word1 = input("Please enter your password: ")

    file = open("user_pass.csv", "r")

    for row in file:
        field = row.split(",")
        user_name = field[0]
        pass_word = field[1]
        user_id = field[2]
        # lastchar = len(pass_word) - 1
        # pass_word = pass_word[0:lastchar]

        if user_name1 == user_name and pass_word1 == pass_word:
            print("Hello", user_name)

            clock = input("Clock [I]n / Clock [O]ut:")

            if clock == "I" or clock == "i":
                date = input("Enter date: ")
                time = input("Enter time: ")
                print("Thanks for Clocking On.")

            if clock == "O" or clock == "o":

                date = input("Enter date: ")
                time = input("Hours worked: ")

                employee_data = open("CafeTestData.csv", "a+")
                data = employee_data.readlines()
                for line in data:
                    if line.split(",")[0] == user_id:
                        newline = line.split(",")

                        if date == "Monday":
                            newline[7] = time

                        if date == "Tuesday":
                            newline[8] = time

                        if date == "Wednesday":
                            newline[9] = time

                        if date == "Thursday":
                            newline[10] = time

                        if date == "Friday":
                            newline[11] = time

                        if date == "Saturday":
                            newline[12] = time

                        if date == "Sunday":
                            newline[13] = time

                        data.remove(line)
                        replacement = ""
                        for item in newline:
                            replacement = replacement + item + ","

                        replacement = replacement[0:len(replacement) - 1]
                        replacement += "\n"
                        data.append(replacement)
                        employee_data.writelines(data)
                        employee_data.close()

            else:
                print("Incorrect. Please try again.")

标签: pythoncsvtime

解决方案


推荐阅读