Hi All,
I have been trying to help my son with his holiday homework.
We have got this far but keep failing to get var2 asked var1 times.
Here is how far we have got.
Var1 = int(input("How many weights do you want to enter?"))
Var2 = int(input("Enter a weight"))
Average = sum(Var2) / len(var1)
for (var2), num (var1)
print average
We want "Enter a weight" asked var1 times and then all the responses averaged.
I appreciate that this requires multiple elements but seem to be making no progress. Any assistance appreciated as we need to understand this function to move on to further questions.
There seem to be a number of syntax issues here, to mention a few:
1. please use [python] tags, so indentation becomes visible
2.if you need var2 , var1 times it should be inside the loop
3. The for loop needs proper syntax + indentation
4. if this is python 3 : print(...) needs brackets
5. to calculate average, either a function or an addition is needed
Paul
Don't write code in a language you don't understand. Instead write the code in a language you do understand and then translate. I start by writing my code in English, translate to pseudo code, and then eventually to my target code.
Pass 1:
Compute the average of a bunch of numbers
Pass 1 is too vague. How many numbers? Where do the numbers come from?
Pass 2:
Enter a number which is a count of numbers to follow
Enter the numbers
Print the average of the entered numbers
Pass 2 is much better. Maybe this could be converted to pseudocode
Pseudocode 1:
count = input('How many numbers to average?')
sum = 0
for count times
number = input('Enter number')
sum = sum + number
print sum / count
At this point I look at the pseudo code and thing "Why am I asking for the count ahead of time? Why not keep inputting numbers and compute the average when the input is not a number?
Pseudocode 2:
count = 0
sum = 0
print("Enter numbers to average. Press Enter with no number to end input"
repeat
number = input("Enter Number: ")
if number == ""
print("Average =", sum/count)
count = count + 1
sum = sum + number
until number == ""
I think that looks pretty good so I start translating to Python. I'm bummed to learn that Python doesn't have repeat until, so I'll have to change that to a while loop. Maybe a while True combined with a break.
What I am trying to convey is that writing programs consists of multiple steps. First you need to describe what you want the program to do. The description should have enough detail to describe the problem completely. This is the requirements. Always take some time to write a good requirements statement. After requirements come design. Design can be pictures or some sort of structured text, or just a really good description of the steps needed to solve the problem. The next state is coding where you translate the design into a programming language. The final stage is testing where you verify that the program works as described by the design and fulfills all the requirements.
A few more things.
Do not use variable names like Var1. The variable name should describe what the variable is. It is much easier to understand
average = sum / count
than
Var3 = Var2 / Var1
Learn how for loops and while loops work, especially for loops. You will use them a lot in Python.