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 trialEnes artun
Courses Plus Student 2,364 PointsProblem 2 with list comprehension. How can I do it?
Hi. I am trying to do solve problem 2 with list comprehension but I am failing. I want to turn the list into integers because I am getting 'unsupported operand type(s) for -: 'str' and 'int' error on line print('Total is: ', sum(numbers)). How can I fix it?
def total_and_average():
numbers = []
while True:
for int(ask) for ask in numbers:
ask = input('Please type a number. Type q to exit. ')
numbers.append(ask)
if ask == 'q':
print('Exitting. Here are the list, sum and avarage of the numbers:')
print(numbers)
print('Total is : ', sum(numbers))
average = sum(numbers)/len(numbers)
print(round(average))
elif ask.isalpha() == True:
print('Please type a number.')
break
1 Answer
Steven Parker
231,198 PointsI see a few issues:
- you don't need the "for" loop, you're already in a "while" loop
- append to the numbers list only after checking for "q" or a letter (with an "else:" clause)
- convert the string you get from "input" into a number as you add it (
numbers.append(int(ask))
) - there seems to be a mix of 2 and 4 spaces for indentation - pick one and use it consistently