'''lecture_20_while_miles.py Use a while loop to get mileage from trips. Loop sentinel is a negative milage (invalid entry) Goal: Compute the total number of miles across all trips and average trip miles UPDATE 1: Add break statement if sentinel is negative UPDATE 2: Introduce increment operator Oliver W. Layton CS 151: Computational Thinking: Visual Media Spring 2021 ''' # Declare variables for number of trips that are entered and total miles entered totalMiles = 0 numTrips = 0 # Get user input print('Enter a negative number to stop entering trip mileage.') currMiles = int(input('Please enter the number of miles you traveled in a trip: ')) # Get data while the user entry is valid (>0), first updating the mileage total and number of trips while currMiles >= 1: # currMiles > 0 also works, but currMiles >= 1 is ok since we're assuming an int # numTrips = numTrips + 1 # totalMiles = totalMiles + currMiles # alternative to the 2 lines above numTrips += 1 totalMiles += currMiles currMiles = int(input('Please enter the number of miles you traveled in a trip: ')) # Compute the average number of miles across all trips avgMiles = totalMiles / numTrips # Print number of total miles and total numebr of trips print('You traveled', totalMiles, 'miles over', numTrips, 'trips') # Print number of average number of miles print('Your average number of miles per trip was', avgMiles)