Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

For this task, you are to write code that calculates the height difference between the shortest and the tallest person. Instructions Your program should ask

For this task, you are to write code that calculates the height difference between the shortest and the tallest person.

Instructions

Your program should ask the user to input a number of people. The program should then repeatedly ask the user to input a person's height until the specified number of people is reached (using a for loop with a range). Finally, the program should output the height difference between the shortest and the tallest persons

You must write the code which identify the shortest and tallest by yourself, updating variables as each new value is input by the user. You are not to place input values in a data structure or use predefined functions that perform aggregation. For example, a solution which places all of the values in a list and sorts the list can not achieve full marks.

Hint: try starting with maximum aggregation (refer to the lecture materials) and work from there.

Hint: the aggregation will require two summary variables.

If the user inputs a number less than 2 for the number of people, the program should output a friendly message informing the user that at least 2 people are required.

A function has already been defined for you which asks the user for the height of a person. You must use this function as part of your solution.

Requirements

To achieve full marks for this task, you must follow the instructions above when writing your solution. Additionally, your solution must adhere to the following requirements:

You must choose appropriate data types for user input.

Your solution must not crash if the user inputs zero or one for the number of person.

Your solution must be implemented using a for loop on a range.

Your solution must not place user input values in a data structure (e.g. set, list, tuple, or dict) or use any predefined aggregation functions (e.g. max, sort).

You must use the ask_for_height function to obtain the height of each person.

Example Runs

Run 1

Number of people: 4 Number of people: 4 Enter the height of a person (cm): 174 Enter the height of a person (cm): 153 Enter the height of a person (cm): 86 Enter the height of a person (cm): 189 Height difference between the shortest and the tallest: 103 cm

Run 2

Number of people: 1 At least 2 people are required.

Run 3

Number of people: 2 Enter the height of a person (cm): 190 Enter the height of a person (cm): 165 Height difference between the shortest and the tallest: 25 cm

Your code should execute as closely as possible to the example runs above. To check for correctness, ensure that your program gives the same outputs as in the examples, as well as trying it with other inputs.

def ask_for_height():

height = int(input('Enter the height of a person (cm): '))

return height

please write the rest of the code

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

How To Build A Million Dollar Database

Authors: Michelle Bergquist

1st Edition

0615246842, 978-0615246840

More Books

Students also viewed these Databases questions