Air Miles Calculator
Air Miles Calculator
Write a Python function that calculates the miles gained by frequent travelers when flying with a specific European airline. The program should prompt the user to enter the number of flights he/she has booked during the last year.
If the number of flights is less than 5, the program should ask the user to enter destinations. You should provide four options:
– National
– Europe
– Americas
– Asia
The program should continue requesting the user to enter destinations until they enter “exit” or five destinations have been input.
The miles corresponding to the different destinations are as follows:
– National: 100 miles
– Europe: 200 miles
– Americas: 500 miles
– Asia: 650 miles
If the number of flights booked during the last year is greater than 5, the program should add 400 miles per flight.
Finally, the program should display the total number of miles gained by the user.
It is a Python conditionals exercise to practice how to use conditional statements in real-world problems. If you wish to keep practising Python, visit ou other real projects.
Steps
- Define a function
calculate_milesto calculate the miles gained by the user. - Prompt the user to enter the number of flights booked during the last year.
- Based on the number of flights, it either asks for destinations and calculates miles or directly calculates miles.
- Use a list to store the destinations and a “while” loop to keep requesting the user to enter destinations until “exit” is entered or 5 destinations have been input.
- Use conditions to calculate miles for different destinations.
- Finally, print the total number of miles gained by the user.
Expected ouput:
Enter the number of flights booked during the last year: 3 Enter destination (National, Europe, Americas, Asia) or 'exit' to stop: National Enter destination (National, Europe, Americas, Asia) or 'exit' to stop: Americas Enter destination (National, Europe, Americas, Asia) or 'exit' to stop: Asia Enter destination (National, Europe, Americas, Asia) or 'exit' to stop: exit Total miles gained: 1250
Solution
# Define the function to calculate miles
def calculate_miles():
# Ask the user to enter the number of flights booked during the last year
num_flights = int(input("Enter the number of flights booked during the last year: "))
total_miles = 0
# If the number of flights is less than 5
if num_flights < 5:
destinations = []
while len(destinations) < 5:
destination = input("Enter destination (National, Europe, Americas, Asia) or 'exit' to stop: ")
if destination.lower() == "exit":
break
if destination.lower() in ["national", "europe", "americas", "asia"]:
destinations.append(destination.lower())
else:
print("Invalid destination. Choose from National, Europe, Americas, Asia.")
# Calculate miles based on destinations
for dest in destinations:
if dest == "national":
total_miles += 100
elif dest == "europe":
total_miles += 200
elif dest == "americas":
total_miles += 500
elif dest == "asia":
total_miles += 650
else: # If the number of flights booked during last year is greater than 5
total_miles = num_flights * 400
# Print the total number of miles gained by the user
print(f"Total miles gained: {total_miles}")
# Call the function to calculate miles
calculate_miles()
