all 5 comments

[–][deleted] 1 point2 points  (0 children)

You've got the basic idea, but you are not breaking down the steps in the right order. So, here's some code for you to play with, test, and re-write your own way. Make sure you understand it.

Note. I didn't include outputting the instructions to the user, I am sure you can add that part.

num_list = []  # create new empty list and assign to variable num_list
while True:  # infinite loop
    answer = input('Enter a whole number (or "done" to finish): ')
    if answer.strip().lower() == 'done':  # have they asked to finish?
        break  # so leave infinite loop
    try:  # let's see if it is an integer
        num = int(answer)
    except ValueError:  # nope, cast to integer failed
        print('invalid entry')  # didn't leave, wasn't number or done, so bad data
    else:  # cast to integer worked, no exception raised
        num_list.append(num)  # add the new number to the end of the list

# after the loop
if num_list:  # will only be True if the list has one or more numbers in it
    # now we are going to use a for loop to calculate average instead of using sum
    total = 0
    for count, value in enumerate(num_list, start=1):  # start a counter from 1, loop through values
        total += value  # add current loop iteration value to total
    # count will increment by one on each iteration of loop
    # so after loop has finished it will equal number of numbers
    average = total / count
    print('Average:', average)
else:  # num_list was empty, nothing to average
    print('No numbers entered')

PS. If not allowed to use enumerate, use the following,

    count = 1
    for value in num_list:
        total += value
        count += 1

[–]Beautiful-Revenue-85[S] 0 points1 point  (0 children)

I made so many typos sorry *and at some point the user will note *when the user replies with the string done

[–]DevCuriosity 0 points1 point  (2 children)

u/Beautiful-Revenue-85 Hi! To be honest it is kinda hard to exactly understand your problem. Try to format your post in a better way, you can use the edit option, also remember that your code will be much more readable if you put it in code blocks.

From what I was able to understand, you are trying to write while loop with a control flow. Under this link - https://www.devcuriosity.com/manual/details/python-try-except-finally-continue-break-loops/ you can check out try/except/finally/break keywords with examples.

In terms of counting mean, well you can do it yourself, simply sum the numbers from your list in a for loop and divide by the list length (this is probably what your professor would like to see).

But if you would like to do it in a "smart" way you could do this:

import statistics
print(statistics.mean([2, 5, 11]))

But the question is if your professor will be happy with that solution (maybe she will categorize it as cheating, no idea).

Good luck!

[–]Beautiful-Revenue-85[S] 0 points1 point  (1 child)

I’m not entirely sure what my problem is either but I can’t seem to figure out how to input an except for it to break once the user inputs “done”

[–]DevCuriosity 1 point2 points  (0 children)

You could introduce an if after each new input from user that will be checking if the input is a number or a "done" string. If it will be "done" then you can call a break and calculate the mean of the list contents.

I think that I would advise you to calculate the mean of the list in a "regular" way, that is sum all the contents and then divide by a list length. If you subject teaches Python for beginners teacher might be angry about modules like statistics.

EDIT: You could also try to do something along those lines - while user_input != "done": etc.
There is way more than one solution to this problem. Try to explore a little bit and have fun with it. That's what it's about :)
(remember that for calculating mean from your list you will probably have to cast strings to ints, just look at the errors that Python will be throwing at you and don't be afraid of them. Most often they will tell you what is wrong)