Please i need help for this

Basically my challenge is to :Ask the user to enter 6 numbers which are then stored in an array. The user should then be able to choose to see either the total or the average of these numbers. Print out the answer.

score = []
for i in range (6):
   number=input("Enter number")
   score.append(number)
option = input("Would you like the total or average? ")
total =score[0]+
if option == "total":
   print(total)
elif option == "average":
   print(int(total)/6)
else:
   print("Invalid option")

This is my code, I dont know what to put after total=score[0]+
Someone please help

I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (’).

I’m no Python guy, but this would seem to work. You have an issue that the values are stored in the array as strings, but that is easy to solve.

Just looking at what you have so far, there’s a few things that kind of stick out to me:

  1. User input in Python always gets stored as a string. Adding string elements concatenates (joins) them rather than summing them, so if you want the total or the average you need to convert from string to int or float.

  2. score[0] refers to the first position in your score array. Even if your list contained integers or floats, your total would just be the first element added over and over again. But here it won’t even do that because total is outside of your loop. If you don’t want to sum it up as you iterate since you used the iteration to build an array, Python has a built in sum function where you can just say total = sum(score) and it’ll add them all for you. Pretty sure they need to be ints, though.

If you want to keep going from where you’re at, then your line for total should look like this:

total = score[0] + score[1] + score[2] + score[3] + score[4] + score[5]

but they still need to be converted into ints and this type of thing is usually handled inside of loop.

  1. Be careful with division. Python 3 is pretty friendly, but when using an expression like int(total/6), what’s going to happen is that it’s going to round down to the nearest whole number. So if your average is like 6.9999999, the int will round it down to 6. If your challenge doesn’t care about about the decimal places, total/6 will work just fine. If is does care and wants a properly rounded number, you can use round((total/6, 2) for two decimal places. Change the second number as desired.
1 Like

When I write in: sum(score)
It does not work

As kevinSmith and Quentin wrote: Python stores inputs as string - meaning text.
The sum() function expects numbers, I guess integer as default. And adding a string to an int is not defined.

Right, one simple solution would be to convert it to an int (whatever) when doing your append.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.