Software Engineering programming do while loop

Announcements Posted on
Four things that unis think matter more than league tables 08-12-2016
    • Thread Starter
    Offline

    0
    ReputationRep:
    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
    Offline

    2
    ReputationRep:
    Hi smorgan594,

    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.
    def calculate_average(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.
 
 
 
Write a reply… Reply
Submit reply

Register

Thanks for posting! You just need to create an account in order to submit the post
  1. this can't be left blank
    that username has been taken, please choose another Forgotten your password?
  2. this can't be left blank
    this email is already registered. Forgotten your password?
  3. this can't be left blank

    6 characters or longer with both numbers and letters is safer

  4. this can't be left empty
    your full birthday is required
  1. Oops, you need to agree to our Ts&Cs to register
  2. Slide to join now Processing…

Updated: October 14, 2016
TSR Support Team

We have a brilliant team of more than 60 Support Team members looking after discussions on The Student Room, helping to make it a fun, safe and useful place to hang out.

Poll
Do you think you'll achieve your predicted A Level grades?

The Student Room, Get Revising and Marked by Teachers are trading names of The Student Room Group Ltd.

Register Number: 04666380 (England and Wales), VAT No. 806 8067 22 Registered Office: International House, Queens Road, Brighton, BN1 3XE

Quick reply
Reputation gems: You get these gems as you gain rep from other members for making good contributions and giving helpful advice.