Join TSR now and get answers to all your questions about uniSign up now

Software Engineering programming do while loop Watch

    • 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.
 
 
 
Poll
Is GoT overrated?

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.