writing a basic math program to help me understand python math calculation.
if I write
x = 15 + 30 + 45
print(x)
I get
90
if I write
x = 90 / 3
print(x)
I get
30.0
but if I write
def avg3():
print("This program will calculate the average of 3 scores")
scores = eval(input("enter 3 scores: "))
average = scores[0] + scores[1] + scores[2] / 3
avg = str(average)
print("The average of the input scores is " + avg + ".")
avg3()
and input
15, 30, 45
what is returned is
The average of the input scores is 60.0.
of course I’m expecting 30. What’s going on here?
You need parentheses in your average, like this:
Otherwise, you are just dividing
scores[2]by3.