Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Indefinite loop ( I think )
#1
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.
Reply
#2
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
It is more important to do the right thing, than to do the thing right.(P.Drucker)
Better is the enemy of good. (Montesquieu) = French version for 'kiss'.
Reply
#3
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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Which GUI can have indefinite loop ? jst 20 1,777 Nov-16-2023, 10:23 PM
Last Post: jst
  Init an indefinite number of class MathisDELAGE 9 2,330 Feb-18-2022, 07:49 PM
Last Post: deanhystad
  indefinite loop Johnygo 3 2,149 Jul-03-2019, 12:53 PM
Last Post: Johnygo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020