[Python] How to work with try and except in python?

Discussão em 'Python' iniciado por Stack, Outubro 5, 2024.

  1. Stack

    Stack Membro Participativo

    This program has to analyze student numbers and if they are correct write them to one list and if not write them to another list. They are correct if they have eight digits and contain only numbers.

    I however have no idea what to type when it comes to the try and except part.

    If anyone could help, I would greatly appreciate it!

    Valid_file = "ValidNumbers.txt"
    Invalid_file = "InvalidNumbers.txt"
    Data_file = "Data.txt"

    def analyse_students(Data_file):
    lenth = len(Data_file)
    total = 0
    try:

    except:

    return 0

    def read(Data_file):
    count = 0
    student_list = []
    try:
    open(Data_file)
    except:
    print("Couldn't append file", Valid_file)
    return count, student_list

    def write(student, status):
    if status:
    try:
    open(Data_file)
    except:
    print("Couldn't append file", Invalid_file)

    count, student_list = read(Data_file)

    print("Number of lines read", count)

    for student in student_list:

    print("See output files")

    Continue reading...

Compartilhe esta Página