CoCalc Public Filessubway fare.ipynbOpen with one click!
Authors: Julia Burnside, Johann Thiel
Views : 51
Compute Environment: Ubuntu 20.04 (Default)

Subway Fare

Two initial variables must be declared: one for a single fare price (farefare) and another for the weekly fare price (weekFareweekFare).

For the purpose of this exercise, the constraints that 1<n<301 < n < 30 are specified. A while loop is used to check that the user value entered is greater than 1 and less than 30. If not, the user is prompted to enter a valid amount.

Once the user enters a valid number of subway rides taken per week, the function minsubcostmin_sub_cost computes the product of the individual fare cost and the number of rides taken with a local value (purchaseAmount=farenpurchaseAmount = fare ⋅ n.

Using a boolean data type, the function compares the computed user amount with the price of a weekly fare. The Boolean value returns truetrue if the purchased amount of subway rides is less than the price of a weekly fare and falsefalse otherwise.

In either case, the computation of their weekly expenditure is declared for the user to view.

If the Boolean variable returns truetrue, the program prints a message telling the user that individual fares are more suited to their requirements.

If the Boolean variable returns falsefalse, the program prints the message explaining to the user that weekly fares are more monetarily beneficial for their requirements. If this is the case, the program computes the price the user pays per week on subway rides then subtracting the weekly fare amount from that value (farenweekFarefare ⋅ n - weekFare), shows the user how much they will save by purchasing a weekly fare.

In [1]:
fare=2.75 weekFare=33 def min_sub_cost(n): purchaseAmount=n*fare return(bool(purchaseAmount<weekFare)) print("How many subway rides do you take over a 7 day period?") n=int(input()) while n<1 or n>30: print("Please enter a number between 1 and 30. How many subway rides do you take over a 7 day period?") n=int(input()) min_sub_cost(n) print("you spend $", n*fare, "per week on subway fares.") if(min_sub_cost(n)): print("You should use individual rides.") else: print("You should buy a weekly subway fare and save $", (n*fare)-weekFare)
How many subway rides do you take over a 7 day period?

This is more sophisticated that what I intended. Well presented. In the future, put the Jupyter notebook in the folder that contains the project prompt. 10/10

In [ ]: