Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python

I'm stuck on problem #2. With every number I enter, it prints every line after 'continue' (EG: You entered: etc.)

Give me a number, or 'q' to quit: 5 You entered: [5.0] The total is: 5.0 The average is: 5.0 Give me a number, or 'q' to quit: 5 You entered: [5.0, 5.0] The total is: 10.0 The average is: 5.0 Give me a number, or 'q' to quit: 5 You entered: [5.0, 5.0, 5.0] The total is: 15.0 The average is: 5.0 Give me a number, or 'q' to quit: 5 You entered: [5.0, 5.0, 5.0, 5.0] The total is: 20.0 The average is: 5.0 Give me a number, or 'q' to quit: q

Michael Hulet
Michael Hulet
47,913 Points

Looks like something didn't get linked here properly. Can you post a link to the challenge you're working on, and the code you have now?

def total_and_average():
    numbers = []
    while True:
        num = input("Give me a number, or 'q' to quit: ").lower()
        if num == 'q':
            break
        try:
                numbers.append(float(num))
        except ValueError:
                continue # if anything is entered that is not a number and not a q, then ask again...
    print("You entered: ", numbers)
    print("The total is: ", sum(numbers))
    print("The average is: ", sum(numbers)/len(numbers))
total_and_average()

1 Answer

Michael Hulet , thank you for your quick response. I figured it out. My editor was not showing me TABS and SPACES in my code and I had them mixed up.