不带任何d的rogue值相加时出现被零除的错误

2024-05-19 03:03:25 发布

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

嗨,如果有人不想添加任何数据,那么在我只输入一个“#”或“流氓”值时,无法修复发生的错误。我不知道如何修复它,我希望能像处理数据一样结束代码。在

#Gets Data Input
def getData():
    fullList = []
    inputText = checkInput("Enter the students first name, last name, first mark, and second mark (# to exit): ")
    while inputText != "#":
        nameList = []
        nameList2 = []
        nameList = inputText.split()
        nameList2.extend((nameList[0],nameList[1]))
        nameList2.append((float(nameList[2]) + float(nameList [3]))/2)
        fullList.append(nameList2)        
        inputText = checkInput("Enter the students first name, last name, first mark, and second mark (# to exit): ")
    print("\n")
    return fullList

#Calculates Group Average
def calc1(fullList):
    total = 0
    for x in fullList:
        total = total + x[2]
    groupAverage = total/(len(fullList))
    return(groupAverage)

#Finds Highest Average
def calc2(fullList):
    HighestAverage = 0
    nameHighAverage = ""
    for x in fullList:
        if x[2] > HighestAverage:
            HighestAverage = x[2]
            nameHighAverage = x[0] + " " + x[1]        
    return (HighestAverage, nameHighAverage)

#Returns Marks above average
def results1(groupAverage,r1FullList):
    r1FullList.sort()
    print("List of students with their final mark above the group average")
    print("--------------------------------------------------------------")
    print("{:<20} {:<12}".format("Name","Mark"))
    for x in r1FullList:
        if x[2] > groupAverage:
            name = x[0] + " " + x[1]
            print("{:<20} {:<12.2f}".format(name,x[2]))

def calc3(x):
        if x[2] >= 80:
            return 'A'
        elif x[2] >= 65:
            return 'B'
        elif x[2] >= 50:
            return 'C'
        elif x[2] < 50:
            return 'D'
        else:
            return 'ERROR'


def results2(fullList):
    print("List of Studens with their Final Marks and Grades")
    print("-------------------------------------------------")
    print("{:<20} {:<12} {:<12}".format("Name","Mark","Grade"))
    for x in fullList:
        grade = calc3(x)
        name = x[0] + " " + x[1]
        print("{:<20} {:<12.2f} {:<12}".format(name,x[2],grade))

#Checks for boundary and invalid data  
def checkInput(question):
    while True:
        textInput = input(question)
        if textInput == "#":
            return textInput            
        splitList = textInput.split()
        if len(splitList) !=4:
            print("Invalid Format, Please Try Again")
            continue
        try:
            a = float(splitList[2])
            a = float(splitList[3])
            if float(splitList[2]) < 0 or float(splitList[2]) > 100:
                print("Invalid Format, Please Try Again")
                continue
            if float(splitList[3]) < 0 or float(splitList[3]) > 100:
                print("Invalid Format, Please Try Again")
                continue
           return(textInput)
        except ValueError:
            print("Invalid Input, Please Try Again")
            continue






#Main Program
#Input Data
fullList = getData()
#Process Data
groupAverage = calc1(fullList)
HighestAverage, nameHighAverage = calc2(fullList)
#Display Results
print("The group average was %.2f" % groupAverage)
print("The student with the highest mark was: %s %0.2f" %(nameHighAverage,HighestAverage))
results1(groupAverage,fullList)
print("\n")
results2(fullList)

Tags: nameforreturnifdeftextinputfloatmark
1条回答
网友
1楼 · 发布于 2024-05-19 03:03:25

您的程序对我来说运行正常,除非您输入#作为第一个条目,在这种情况下,fullList是{},长度为0。因此,DivisionByZero在这一行:groupAverage = total/(len(fullList))。在

您可以修改代码以检查并退出:

import sys
fullList = getData()
if not fullList:
    print('No Data!')
    sys.exit()

相关问题 更多 >

    热门问题