如果列表中没有第一个,如何打印报告

2024-06-06 17:37:51 发布

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

我试图显示除“姓名”之外的姓名,并试图获得列表中没有“年龄”的平均年龄,最后获得没有“成绩”的平均成绩

我有一个问题,我试图打印列表中没有第一行的名称,而没有弹出或从列表中删除它们

studentDB = [['Name', 'Age', 'Grade'], ['Con', 20, 90.2],
['Juan', 45, 70.2], ['Jed', 39, 100.0]]

while True:
    for i in range(len(studentDB)):
         print(i, studentDB[i])
            
    print("\n**********  Hackathon  ***********")
    CRUD = """1- Add
2- Delete
3- Edit
4- Print Report
----------------
Enter a number: """
    choice = float(input(CRUD))
    if (choice == 1):
        Name = input("Please Enter your Name: ")
        Age = int(input("Please Enter your Age: "))
        Grade = float(input("Please Enter your Grade: "))
        
        studentDB.append([Name, Age, Grade])

        print(studentDB)
    elif (choice == 2):
        Del = int(input("Please Enter the row you want to delete: "))
        studentDB.pop(Del)
    elif (choice == 3):
        Del1 = int(input("Please Enter the row you want to delete: "))
        Name1 = input("Please Enter your Name: ")
        Age1 = int(input("Please Enter your Age: "))
        Grade1 = float(input("Please Enter your Grade: "))
        
        studentDB.pop(Del1)
        studentDB.insert(Del1,[Name1, Age1, Grade1])
        print(studentDB)
    elif (choice == 4):
        #here is the problem
        for s in range(1, len(studentDB)): 
            print("")
        print(f"There are in the {s} student table. The people included in the list are.")
    else:
        print("Invalid input")

Here is the output that I wanted to do

这是联机编译器:https://www.programiz.com/python-programming/online-compiler/


Tags: thenamein列表inputageyourfloat
1条回答
网友
1楼 · 发布于 2024-06-06 17:37:51

有很多方法可以解决这个问题。一种方法是在计算年龄或年级的平均数时,使用范围内的起始指数和切片

 from statistics import mean
 while True:
    # Use the start index in range.
    for i in range(len(studentDB)):
         print(i, studentDB[i])
            
    print("\n**********  Hackathon  ***********")
    CRUD = """1- Add
2- Delete
3- Edit
4- Print Report
        
Enter a number: """
    choice = float(input(CRUD))
    if (choice == 1):
        Name = input("Please Enter your Name: ")
        Age = int(input("Please Enter your Age: "))
        Grade = float(input("Please Enter your Grade: "))
        
        studentDB.append([Name, Age, Grade])

        print(studentDB)
    elif (choice == 2):
        Del = int(input("Please Enter the row you want to delete: "))
        studentDB.pop(Del)
    elif (choice == 3):
        Del1 = int(input("Please Enter the row you want to delete: "))
        Name1 = input("Please Enter your Name: ")
        Age1 = int(input("Please Enter your Age: "))
        Grade1 = float(input("Please Enter your Grade: "))
        
        studentDB.pop(Del1)
        studentDB.insert(Del1,[Name1, Age1, Grade1])
        print(studentDB)
    elif (choice == 4):
        #here is the problem
        # Use slice to get the row from index 1 afterwards
        print(f"There are in the {len(studentDB) - 1} records in the student table. The people included in the list are.")
        print(",".join([student[0] for student in studentDB[1:]]))
        average_age = mean([row[1] for row in studentDB[1:]])
        average_grades = mean([row[2] for row in studentDB[1:]])
        # Print average age and grades here
    else:
        print("Invalid input")```

相关问题 更多 >