-1

So basically I want to calculate the average of (x) test marks. So far this is what I have:

for i in range(3):
    testmark = int(input("Enter one test mark: "))
print("Calculating...")
average = (testmark + testmark + testmark) / (i + 1)
print("Your average is", average, "percent.")

However, I want it so that the average variable add ALL the inputs together. Right now, I made it so that it only calculates 3. I want it something like:

for i in range(7):
    testmark = int(input("Enter one test mark: "))
print("Calculating...")
average = (**[sum of all test marks]**) / (i + 1)
print("Your average is", average, "percent.")
Minn
  • 5,688
  • 2
  • 15
  • 42
  • 1
    Welcome to StackOverflow. [On topic](https://stackoverflow.com/help/on-topic), [how to ask](https://stackoverflow.com/help/how-to-ask), and ... [the perfect question](https://codeblog.jonskeet.uk/2010/08/29/writing-the-perfect-question/) apply here. StackOverflow is a knowledge base for *specific* programming problems -- not a design, coding, research, or tutorial resource. – Prune Feb 13 '20 at 22:42
  • 4
    When asking about homework (1) **Be aware of your school policy**: asking here for help may constitute cheating. (2) Specify that the question is homework. (3) **Make a good faith attempt** to solve the problem yourself first (include your code in your question). (4) **Ask about a specific problem** with your existing implementation; see [Minimal, complete, verifiable example](https://stackoverflow.com/help/minimal-reproducible-example). Also, [here](https://meta.stackoverflow.com/questions/334822/how-do-i-ask-and-answer-homework-questions) is guidance on asking homework questions. – Prune Feb 13 '20 at 22:42
  • 1
    If you search in your browser for "Python loop sum average", you'll find references that can explain this much better than we can manage here. – Prune Feb 13 '20 at 22:42
  • `testmark` can keep only one value - you would have to `append()` it to list and later sum all elements from list - `sum(list)` – furas Feb 13 '20 at 22:45

2 Answers2

2

That's a good start. However, You can do it in a better way like this:

iters = int(input("How many numbers do you have?\n"))
sum = 0
for number in range(iters):
    sum += int(input("Give a number: "))
print(f"The average of those inputs is {sum/iters}")  # I'm assuming you have Python >= 3.6 
Filip
  • 898
  • 1
  • 8
  • 24
1

Lets start by taking a look at the issues you encountered and how you can address them;

for i in range(3):
    testmark = int(input("Enter one test mark: "))
print("Calculating...")
average = (testmark + testmark + testmark) / (i + 1)
print("Your average is", average, "percent.")
  1. You override testmark every iteration, meaning you only ever store ONE value
  2. You attempt to call i outside of the for loop, which will fail if i is not defined prior

We can adjust you code to be more resilient and provide the end-user more ability to modify the functions, such as how may iterations we test and the testmarks we would like to calculate.

test_marks = []
tests = int(input('How many tests are there? '))

#How many tests are there? 5

for i in range(tests):
    test_marks.append(int(input("Enter one test mark: ")))

#Enter one test mark: 5
#Enter one test mark: 10
#Enter one test mark: 56
#Enter one test mark: 99
#Enter one test mark: 1

print(f"The average test mark is: {sum(test_marks) / len(test_marks)}")

#The average test mark is: 46.5
  1. Declare an empty list to store marks in as test_marks
  2. Prompt the user to define the total tests being entered
  3. Iterate over the total tests and prompt the user for a mark
  4. Calculate and print the average (This is Python 3.6+ Syntax!)

This is a good opportunity to dive a little deeper to bolster your understanding of some core workings on Python, i have included a few links for your learning!

Iterating over lists in Python Appending & extending lists Python 3.6 f-strings

PacketLoss
  • 5,561
  • 1
  • 9
  • 27