'I'm creating a code that allows the user to enter only 5 readings of swallow speeds, then print the minimum, maximum and average speed in MPH and KPH

print("Swallow Speed Analysis: Version 1.0")
print("")

reading1 = (input)
reading2 = (input)
reading3 = (input)
reading4 = (input)
reading5 = (input)

def a_list():
    a_list = [reading1, reading2, reading3, reading4, reading5]
a_list()

while True:
    reading1 = (input("Enter First Reading: "))
    print("Reading Saved.")
    reading2 = (input("Enter Second Reading: "))
    print("Reading Saved.")
    reading3 = (input("Enter Third Reading: "))
    print("Reading Saved.")
    reading4 = (input("Enter Fourth Reading: "))
    print("Reading Saved.")
    reading5 = (input("Enter Fifth Reading: "))
    print("Reading Saved.")
    print("")
    print("Results Summary")
    print("")
    print("5 Readings Analysed")
    print("")
    break

    if "U" in a_list:
        a_list.append(float(a_list[1:]))
        print("Reading Saved.")

    elif "E" in a_list:
        a_list.append(float(a_list[1:]))
        print("Reading Saved.")
        for number in a_list:
            reading_sum = float(number / 1.61)
            a_list.append(float(reading_sum))
        if a_list == "":
        print('No readings entered. Nothing to do.')
        break

a_list = (reading1, reading2, reading3, reading4, reading5)
max_value = max(a_list)
min_value = min(a_list)
avg_value = 0 if len(a_list) == 0 else sum(a_list)/len(a_list)

I have this so far but I don't know how to find the minimum, maximum and average, print the min max and avg in MPH and have the users entered speeds converted correctly to KPH from MPH. I've tried to add some code to find the min, max, and avg but it didn't convert the readings from MPH to KPH correctly, it would only convert one reading and print it for all 3. I've added the specification I'm following below.

The Problem

A science project is seeking to determine the average flight speeds of British and European swallows. To this end, tracking equipment has been set up, to track birds in both the UK and in Europe. The equipment sends its measurements over the Internet as a simple data stream.

The project now needs to use the data to determine various interesting statistics about swallow speed.

Unfortunately, post-Brexit the European equipment measures speed in kilometres per hour while the British equipment uses miles per hour. Data from the European equipment looks like this:

E23.56

The E denotes that this is a European reading (so in KPH), and the following numbers are the measured speed. The data from the British equipment is identical except that the first letter is a U:

U15.8

A program is required that will process a stream of this data, and then display the number of readings processed, along with the maximum, minimum, and average (mean) speeds. The final output is required in both KPH and MPH (see the examples below).

Write a program that reads any number of data items from standard input (i.e. the keyboard), and then displays the statistics needed. The output ends when the user just presses Enter at the input prompt.

Since the data stream is generated by software that has been rigorously tested, it is safe to assume that the data is always in one of the two expected formats.

Note: 1 MPH is roughly 1.61 KPH. Google a more exact factor to avoud rounding errors in your results.

Examples

The following illustrate what should happen when the program executes in a variety of scenarios.

Here, there is no data, so the program just exits gracefully.

Swallow Speed Analysis: Version 1.0

Enter the Next Reading:
No readings entered. Nothing to do.

Now there is one reading, in MPH, which is reported as the max, min, and mean. (We should also check that 20 MPH is indeed just over 32 KPH.)

Swallow Speed Analysis: Version 1.0

Enter the Next Reading: U20 Reading saved. Enter the Next Reading:

Results Summary

1 Reading Analysed.

Max Speed: 20.0 MPH, 32.2 KPH. Min Speed: 20.0 MPH, 32.2 KPH. Avg Speed: 20.0 MPH, 32.2 KPH.

Now, if the data is 20 MPH and the equivalent in KPH, we should see the same results:

Swallow Speed Analysis: Version 1.0

Enter the Next Reading: U20 Reading saved. Enter the Next Reading: E32.2 Reading saved. Enter the Next Reading:

Results Summary

2 Readings Analysed.

Max Speed: 20.0 MPH, 32.2 KPH. Min Speed: 20.0 MPH, 32.2 KPH. Avg Speed: 20.0 MPH, 32.2 KPH.

Note: If you try similar experiments, you may see rounding errors (so one of the above values might be slightly different). This can probably be fixed by using a more accurate conversion factor, but there is no need to worry about small differences.

Finally, a stream of five values produces expected output:

Swallow Speed Analysis: Version 1.0

Enter the Next Reading: U20 Reading saved. Enter the Next Reading: U25 Reading saved. Enter the Next Reading: E30 Reading saved. Enter the Next Reading: E32.5 Reading saved. Enter the Next Reading: U28.3 Reading saved. Enter the Next Reading:

Results Summary

5 Readings Analysed.

Max Speed: 28.3 MPH, 45.5 KPH. Min Speed: 18.6 MPH, 30.0 KPH. Avg Speed: 22.4 MPH, 36.1 KPH.



Solution 1:[1]

I believe I have understood your problem and arrived at a solution.

The following code receives inputs until the user inputs nothing. It quits if the user enters nothing to begin with. It also separates the prefix from the number:

readings=[]
i = 1
reading=input(f"Enter reading {i}: ")
if reading:
    while reading:
        readings.append((reading[0], float(reading[1:])))
        print("Reading saved.")
        i += 1
        reading=input(f"Enter reading {i}: ")
else:
    input("No readings entered. Nothing to do.")
    quit()

print(f"\nResult summary\n\n{i-1} readings analysed")

Next, the numbers are converted to both units and put in a (kmh, mph) tuple:

readings = [
    (r * 1.609344, r) if unit == "U"
    else (r, r * 0.6213712)
    for unit, r in readings
]

This function formats the data for printing. It rounds the numbers and adjusts each side of the .:

def my_format(readings, digits=2, sep=" | "):
    return [
        "{1} km/h{0}{2} Mph".format(
            sep,
            *[
                integer.rjust(2, " ") + "." + decimal.ljust(digits, "0")
                for integer, decimal in [
                    str(round(float(number), digits)).split(".")
                    for number in reading
                ]
            ]
        )
        for reading in readings
    ]

I'm no ornithologist, so if swallows have a top speed greater than 100 km/h you need to change .rjust(2, " ") to .rjust(3, " ").

To print every number, simply use my_format on the readings and join the result:

print("\n".join(my_format(readings)))

Python's min and max can sort the (kmh, mph) tuples, but for sum you need to to only use one of them, in this case kmh. I added Median in case you need that aswell:

avg = sum(kmh for kmh, mph in readings) / len(readings)
print("\nMinimum: {}\nMaximum: {}\nAverage: {}\nMedian : {}".format(*my_format(
    [
        min(readings),
        max(readings),
        (avg, avg * 0.6213712)
        sorted(readings)[int(len(readings) / 2)]
    ]
)))

I hope this solution is suitable for your needs.

Your desired result is detailed, but the text could use some formatting to make the outputs stand out.

I was also confused by this function:

def a_list():
    a_list = [reading1, reading2, reading3, reading4, reading5]
a_list()

It does nothing, since it returns nothing and calls no other functions. If you were trying to make a list, then this is enough:

a_list = [reading1, reading2, reading3, reading4, reading5]

The two breaks in your code either do nothing or make half the code do nothing.

You might have noticed I use km/h instead of KPH. KPH doesn't make sense in SI, as meters is the root and kilo is just a prefix that is also used for other units. Some of them can be measured by the hour, ie. kilowatt hours: kW/h, 1000 Watt per hour. Capital K and H represent Kelvin and Henry, heat and inductance, so KpH would be Kelvin picoHenry.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 8yq53wft