除了错误处理,Python不显示文本和我告诉它的错误,只是崩溃并显示奇怪的行?

2024-10-01 11:32:38 发布

您现在位置:Python中文网/ 问答频道 /正文

只是一些背景信息,这段代码有另一个python文件,它测试我的代码,看它是否正常工作,并正确处理所有情况,因为它是一个大学代码/网站

我正在尝试写入csv文件,如果测试它的代码找不到该文件,则显示此错误: “写入错误:错误”

我的代码和输出可以与空闲/讲师期望的输出一起显示在图像中

关于为什么我的代码会显示它所做的事情,以及如何使它与预期的输出相同,有什么想法吗

import csv

def rememberInfo():

    name = input("What is your name?")
    print()
    age = input("How old are you?")
    print()
    course = input("What course are you on?")
    print()
    location = input("Where do you live?")
    print()

    try:
        with open('employee_file.csv', mode='w') as employee_file:
            employee_writer = csv.writer(employee_file, delimiter=',')
            employee_writer.writerow([name, age, course, location])
            line = name + "," + age + "," + course + "," + location
            print(line)
        employee_file.close()

    except Exception as e:
        print("Error writing:", e)


if __name__ == "__main__":
    rememberInfo()

Code And Output vs Expected Output


Tags: 文件csv代码nameyouinputage错误