Python数组循环不循环/验证程序问题

2024-06-16 10:18:43 发布

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

我不确定我到底做了什么,但在测试我的代码时,它要么立即崩溃,要么陷入循环。如果第一个输入是一个值错误(字符串),下一个输入是一个数字,那么只要保持模式,它就会循环。但若第一个用户条目是int,那个么程序就会崩溃。如果您有任何帮助,我们将不胜感激

def main():
    courseArray = []
    keepGoing = "y"
    while keepGoing == "y":
        courseArray = getValidateCourseScore()
        total = getTotal(courseArray)
        average = total/len(courseArray)
        print('The lowest score is: ', min(courseArray))
        print('The highest score is: ', max(courseArray))
        print('the average is: ', average)
        keepGoing = validateRunAgain(input(input("Do you want to run this program again? (Y/n)")))


def getValidateCourseScore():
    courseArray = []
    counter = 1
    while counter < 6:
        try:
            courseArray.append(int(input("Enter the number of points received for course: ")))
            valScore(courseArray)
        except ValueError:
            print("Please enter a valid score between 0-100")
            courseArray.append(int(input("Enter a number between 1 and 100: ")))
    counter += 1
    return courseArray


def valScore(courseArray):
    score = int(courseArray)
    if score < 0:
        print("Please enter a valid score between 0-100")
        courseArray.append(int(input("Enter a number between 1 and 100: ")))
    elif score > 100:
        print("Please enter a valid score between 0-100")
        courseArray.append(int(input("Enter a number between 1 and 100: ")))
    else:
        return courseArray

def validateRunAgain(userInput):
    if userInput == "n" or userInput == "N":
        print("Thanks for using my program")
        return "n"
    elif userInput == "y" or userInput == "Y":
        return "y"
    else:
        print("Please enter y or n")
        validateRunAgain(input("Do you want to run this program again? (Y/n)"))
        return getValidateCourseScore()

def getTotal(valueList):
    total = 0
    for num in valueList:
        total += num
    return total

main()

Tags: numberinputreturndefbetweeninttotalscore
1条回答
网友
1楼 · 发布于 2024-06-16 10:18:43

用户的输入太多了,所以我减少了输入并进行了更改。
以下是我更改的代码部分:
这里valScore()我假设验证了输入分数,所以我也给出了要验证的元素的索引。如果无效,我们将其从数组中删除并引发ValueError,因为它会引发错误,我们的counter不会更新

keepGoing = validateRunAgain()

def getValidateCourseScore():
    courseArray = []
    counter = 1
    while counter < 6:
        try:
            courseArray.append(int(input("Enter the number of points received for course: ")))
            valScore(courseArray, counter - 1)
            counter += 1
        except ValueError:
            print("Please enter a valid score between 0-100")
            continue
    return courseArray

def valScore(courseArray, counter):
    score = courseArray[counter]
    if score < 0 or score > 100:
        courseArray.pop()
        raise ValueError

def validateRunAgain():
    while True:
        userInput = input("Do you want to run this program again? (Y/n)")
        if userInput == 'y' or userInput == 'Y':
            return 'y'
        elif userInput == 'n' or userInput == 'N':
            print('thank you for using my program')
            return 'n'
        else:
             print('Enter Y or N')

相关问题 更多 >