I need to create a program where the user inputs height - program asks for if the user wants to input more heights? if yes you program loops back to the start - if not, drops out and gives the average of heights and asks the user if they want to continue or terminate the program??? I'm still new to the programming world, any help would be great
Software Engineering programming do while loop
- 0 followers
- 0 badges
- Thread Starter
Last edited by smorgan594; 13-10-2016 at 21:43.
- 13-10-2016 21:35
- 0 followers
- 2 badges
- 14-10-2016 14:27
I am wondering what programming language you would like to do this in?
As an example we can simply put this into Python code:
We could go about stating this programs objectives:
1. Allow a user to enter in their desired heights and save these temporarily,
2. If the user was to stop entry, all the saved heights would be used to calculate an average.
NOTE: The indentation in the code below is incorrect, I am unsure how to properly indent code on posts, hopefully this can help.
heights =  # An empty list; this is where can temporarily store our heights the user enters in.
# This procedure will allow you to calculate the mean of the heights.
sum_heights = 0 # Store the sum of the heights.
# This is a for loop to iterate through the items in the heights list and return the mean of the list.
for height in heights:
sum += height # Add the value of height to the sum.
mean = sum_heights / len(heights) # Divide the sum by the length of the heights, giving us our mean.
return mean # Return the mean to use.
while True: # This will start a continuous loop in Python.
entered_height = float(input('Enter the height you require: ' # Get the entry of the user as a string, then convert it into # a floating point decimal.
heights.append(entered_height) # This will add the height, as a decimal, to the list.
more_heights = input('Do you want to input more heights (Y/N)?'
if more_heights is 'Y':
continue # starts the loop again.
elif more_heights is 'N':
average_height = calculate_average(heights) # Calculate an average using our function and then store it. print('Average height from the data you entered:', average_height)
terminate = input('Terminate program? (Y/N)')
if terminate is 'Y':
break # Stops the while loop and as a result the program terminates due to no more code.
elif terminate is 'N':
continue # Continues with the main while loop.Last edited by Goel; 14-10-2016 at 14:56.