How do i write a python code so that my list of numbers can be updated within an elif cause?
I start off with an empty an empty list then from there i have to ask the user how many new scores they want to add to the list then add those numbers to the list.Then i have to go back to the menu system and ask the user to press 3 if they want the average of those number in the list. here my code below the problem im having is that the scores are being updated in the elif clause but once i get out the clause the list goes back to being empty. please help!

while 1:
    print("0 - clear out scores")
    print("1 - input more scores")
    print("2 - display all scores")
    print("3 - get average scores")
    option = int(input("\n Please seclet an opition: "))
    scores =[]

    def clearScores():
            scores = []

    def addScores():
            x=1
            while x < 2 : 
                numOfScores = int(input("How many new scores would you like to add :"))
                if numOfScores < 0:
                    print("Please enter a postivite integer for the number of scores you would like to add.")
                else:
                    index = 0
                    while index < numOfScores:
                        new_num = eval(input("Enter score: "))
                        scores.append(new_num)
                        index += 1    
                    print(scores)
                    x += 2
            return scores
    if option == 0:
            clearScores()
            print("The scores has been cleared")

    elif option == 1:
            scores.append(addScores())

    elif option == 2:
            printScores()

    elif option == 3:
            averageScores()
    else:
        print("goodbye")

replay = input("\nDo You Want To Continue? Y/N: ")          
replay = replay.lower()             

if (replay == "yes") or (replay == "y"):            
    pass                
else:               
    print("\nGoodbye!") 

Recommended Answers

All 3 Replies

Line 7 should go before the for loop. When it is executed, scores becomes a new empty list. Also line 10 should be scores[:] = [], otherwise it does nothing.

It is better to write all the function definitions out of the for loop (before the loop). A function definition usually needs to be executed only once.

And generally, the input is separate from the rest of the program.

def get_input():
    option = 99
    while option not in [0, 1, 2, 3, 4]:
        print("0 - clear out scores")
        print("1 - input more scores")
        print("2 - display all scores")
        print("3 - get average scores")
        print("4 - end program")
        option = int(input("\n Please select an opition: "))

    return option

scores =[]
option=99
while option != 4:
    option = get_input():

    ## rest of program
    if option == 0:
        clearScores()

Also, you should pass the variable to the function and return it. Lists are mutable so this is not necessary with lists, but for readability it is generally done.

def clear_scores(scores):
    scores = []
    return scores

## called as
scores = clear_scores(scores)

## and the same for
scores = add_scores(scores)
## etc.

I agree with wooeee's suggestions. However you should not use clear_scores function but do

# clear the old scores
scores[:] = []

or

#make new empty list for scores
scores = []
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.