TypeError:write()只接受一个参数(2个给定)python

ggazkfy8  于 2023-11-20  发布在  Python
关注(0)|答案(3)|浏览(258)
elif choice == "A":
    def add_task(count):
        with open ( 'user.txt' ) as fin:
            usernames = [ i.split ( ',' ) [ 0 ] for i in fin.readlines ( ) if len ( i ) > 3 ]
            task = input ( "Please enter the username of the person the task is assigned to.\n" )
        while task not in usernames:
            task = input ( "Username not registered. Please enter a valid username.\n" )
        else:
            task_title = input ( "Please enter the title of the task.\n" )
            task_description = input ( "Please enter the task description.\n" )
            task_due = input ( "Please input the due date of the task. (yyyy-mm-dd)\n" )
            date = datetime.date.today ( )
            task_completed = False
            if task_completed == False:
                task_completed = "No"
            else:
                task_completed = ("Yes")
            with open ( 'tasks.txt' , 'a+' ) as task1:
                count = count + 1
                task1.write ( "\n User assigned to task: " , str (
                    count ) + "\n" + task + "\nTask Title :" + "\n" + task_title + "\n" + "Task Description:\n" + task_description + "\n" + "Task Due Date:\n" + str (
                    task_due ) + "\n" + "Date Assigned:\n" + str (
                    date ) + "\n" + "Task Completed:\n" + task_completed + "\n" )
                file.close ( )
                print ( "The new assigned task has been saved" )
    count = 0
    add_task ( count )

字符串

mnemlml8

mnemlml81#

取代

task1.write ( "\n User assigned to task: " , str (
                count ) + "\n" + task + "\nTask Title :" + "\n" + task_title + "\n" + "Task Description:\n" + task_description + "\n" + "Task Due Date:\n" + str (
                task_due ) + "\n" + "Date Assigned:\n" + str (
                date ) + "\n" + "Task Completed:\n" + task_completed + "\n" )

字符串

task1.write ( "\n User assigned to task: " + str (
                count ) + "\n" + task + "\nTask Title :" + "\n" + task_title + "\n" + "Task Description:\n" + task_description + "\n" + "Task Due Date:\n" + str (
                task_due ) + "\n" + "Date Assigned:\n" + str (
                date ) + "\n" + "Task Completed:\n" + task_completed + "\n" )


前两个字符串用逗号连接,而所有其他字符串用+号连接。

tpxzln5u

tpxzln5u2#

你需要修改task1.write行:
它应该是:

task1.write ( "\n User assigned to task: " + str (count) + "\n" + task + "\nTask Title :" + "\n" + task_title + "\n" + "Task Description:\n" + task_description + "\n" + "Task Due Date:\n" + str (task_due) + "\n" + "Date Assigned:\n" + str (date ) + "\n" + "Task Completed:\n" + task_completed + "\n" )

字符串

xqk2d5yq

xqk2d5yq3#

你可以把,改为+:

task1.write(
    f"\nUser assigned to task: {count}" +
    f"\nTask:\n{task}" +
    f"Task Title:\n{task_title}" +
    f"Task Description:\n{task_description}" +
    f"Task Due Date:\n{task_due}" +
    f"Date Assigned:\n{date}" +
    f"Task Completed:\n{task_completed}\n"
)

字符串

相关问题