Home API Notes

College Board Requirements

</p>

</p>

</table> </p> </div> </div> </div>
def calculate_avg_mpg(trip_mileage, fuel_used):
    # Calculate the total mileage and fuel used
    total_mileage = sum(trip_mileage)
    total_fuel_used = sum(fuel_used)
    
    # Calculate the average MPG by dividing total mileage by total fuel used
    return total_mileage / total_fuel_used


def main():
    # Initialize a variable to track whether the user wants to continue
    continue_calculation = True

    # Keep looping until the user indicates that they don't want to continue
    while continue_calculation:
        # Prompt the user to enter the number of entries for their trip
        num_entries = int(input("Enter the number of entries for your trip: "))

        # Initialize empty lists for trip mileage and fuel used
        trip_mileage = []
        fuel_used = []

        # Iterate through the number of entries and prompt the user to enter the distance and fuel used for each entry
        for i in range(num_entries):
            trip_mileage.append(float(input("Enter the distance in miles for entry #{}: ".format(i+1))))
            fuel_used.append(float(input("Enter the fuel amount used in gallons for entry #{}: ".format(i+1))))
        
        # Call calculate_avg_mpg() to calculate the average MPG and store the result in a variable
        average_mpg = calculate_avg_mpg(trip_mileage, fuel_used)
        
        # Display the result to the user
        print("The average MPG for your trip is: {:.2f}".format(average_mpg))

        # Ask the user if they want to calculate another trip's average MPG
        continue_response = input("Do you want to calculate another trip's average MPG? (y/n): ")
        if continue_response.lower() != 'y':
            continue_calculation = False


# Call the main function
main()
The average MPG for your trip is: 1.00

Reflection/Plans

Highlights

Overall, I think this program does a good job of calculating the average MPG for a trip based on the distance and fuel used. One thing that stands out to me is the use of separate lists for the distance and fuel used, which allows for easy iteration through each entry of the trip and accurate calculation of the total mileage and fuel used. I also like the use of procedural abstraction with the calculate_avg_mpg() function, which makes the main function easier to read and understand.

Improvements

One thing that could be improved in this program is the user input process. Right now, the program simply prompts the user for input without any validation or error handling. I can add some error handling to ensure that the user is entering valid input, such as checking that the distance and fuel used are both positive numbers. Additionally, I can also provide more detailed output to the user, such as displaying the individual mileage and fuel used for each entry in addition to the average MPG. Lastly, I still need to test my code more using different test cases - to ensure that it properly works.

Overall

Overall, I think this program is a good starting point for calculating the average MPG for a trip, but there are certainly areas for improvement and additional features that could be added to make the program more user-friendly and versatile.

import requests

url = "https://corona-virus-world-and-india-data.p.rapidapi.com/api"

headers = {
	"X-RapidAPI-Key": "d3a3e94748msh74bb629320d5734p160ceajsn7f28f4859ea2",
	"X-RapidAPI-Host": "corona-virus-world-and-india-data.p.rapidapi.com"
}

response = requests.request("GET", url, headers=headers)

print("Country Totals")
countries = response.json().get('countries_stat')
for country in countries:  # countries is a list
    if country["country_name"] == "Niue":  # this filters for USA
        for key, value in country.items():  # this finds key, value pairs in country
            print(key, value)
Country Totals
country_name Niue
cases 8
deaths 0
region 
total_recovered 7
new_deaths 0
new_cases 0
serious_critical 0
active_cases 1
total_cases_per_1m_population 4,860
deaths_per_1m_population 0
total_tests 0
tests_per_1m_population 0
</div>
Program Purpose and Function The purpose of this program is to calculate the average miles per gallon (MPG) for a trip based on user input. The program prompts the user to enter the distance and fuel used for each entry in their trip, calculates the total mileage and fuel used for the trip, and then calculates the average MPG by dividing the total mileage by the total fuel used. Finally, the program displays the average MPG to the user.
Data Abstraction This code utilizes data abstraction by encapsulating the logic for calculating the average MPG in a separate function, calculate_avg_mpg(). This function takes two input parameters, trip_mileage and fuel_used, which are both lists of floating-point values representing the distance and fuel used for each entry in the trip. The function then returns the average MPG as a floating-point value.
Managing Complexity This code manages complexity by breaking down the problem into smaller, more manageable parts. The program uses loops to iterate through the user input and store it in lists, which are then passed to the calculate_avg_mpg() function for processing. By separating the input collection and processing logic into different functions, the code becomes more modular and easier to understand.
Procedural Abstraction This code utilizes procedural abstraction by separating the user input collection and processing logic into different functions. The main() function is responsible for prompting the user to enter the number of entries and the distance and fuel used for each entry. The calculate_avg_mpg() function is responsible for calculating the average MPG based on the input values passed to it.
Algorithm Implementation The algorithm implemented by this code is straightforward. The program prompts the user to enter the number of entries for their trip, initializes empty lists for trip mileage and fuel used, iterates through the number of entries and prompts the user to enter the distance and fuel used for each entry, calculates the total mileage and fuel used for the trip, calculates the average MPG by dividing the total mileage by the total fuel used, and finally displays the average MPG to the user.
Testing To test this code, a user can run the program and enter sample input values for the distance and fuel used for each entry. The output of the program can be verified manually by calculating the average MPG for the entered values and comparing it to the output displayed by the program. Additionally, a user can test the program with different input values to ensure that it handles various scenarios correctly.