Search code examples
pythonarraysvalidationuser-input

Python array loop not looping/validator issues


I'm not exactly sure what I did, but when testing my code it either crashes immediately or gets stuck in a loop. If the first input is a value error (string) and the next is a number it loops as long as the pattern is kept. But if first user entry is int then program crashes. Please any help would be appreciated.

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()

Solution

  • There are too many inputs from the user, so I have cut down on them and changed it.
    Here are the sections of your code which I have changed :
    Here valScore() I presume validates the input score so, I also gave the index of element to be validated. If it is not valid we remove it from the array and raise ValueError, since it raises error our counter is not updated.

    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')