【问题标题】:Type Error, Need a Y/N user confirmation. Python类型错误,需要 Y/N 用户确认。 Python
【发布时间】:2022-11-29 08:07:42
【问题描述】:

我正在开发一个将删除个人记录的功能,但在此之前,它将显示:

Are you sure you want to delete record with Last Name: Apple, First Name: Amy ? Enter Y or N

我完成了大部分功能。我对这个是或否部分有困难。到目前为止,我拥有的删除功能代码如下

def delete_student():
    global student_info
    global database

    print("--- Delete Student ---")
    roll = input("Enter a Last Name: ")
    student_found = False
    updated_data = []
    with open(database, "r", encoding="utf-8") as f:
        reader = csv.reader(f)
        counter = 0
        for row in reader:
            if len(row) > 0:
                if roll != row[2]:
                    updated_data.append(row)
                    counter += 1
                else:
                    student_found = True

    if student_found is True:

        if input("Are you sure you want to delete record", roll,  "(y/n) ") != "y":
            exit()
        with open(database, "w", encoding="utf-8") as f:
            writer = csv.writer(f)
            writer.writerows(updated_data)
        print("Student ", roll, "deleted successfully")
    else:
        print("Record not found")

    input("Press any key to continue")

这给了我一个类型错误,我需要在删除记录之前显示此人的姓名作为确认。是/否输入。

类型错误:

Traceback (most recent call last):
  File "/Users/jake./PycharmProjects/Munyak_Jacob_FinalProject/FileRecords.py", line 58, in <module>
    delete_student()
  File "/Users/jake./PycharmProjects/Munyak_Jacob_FinalProject/deleteRecord.py", line 27, in delete_student
    if input("Are you sure you want to delete record", roll,  "(y/n) ") != "y":
TypeError: input expected at most 1 argument, got 3

Process finished with exit code 1

【问题讨论】:

    标签: python types typeerror


    【解决方案1】:

    input 将单个字符串作为参数,但您提供了三个。构造单个字符串:

    if input(f"Are you sure you want to delete record {roll} (y/n)? ") != "y":
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-06-24
      • 2018-01-11
      • 1970-01-01
      • 2021-10-24
      • 2021-11-07
      • 2023-04-02
      • 2013-12-19
      • 2017-09-16
      相关资源
      最近更新 更多