How to Write Conditional Statements in Python

Conditional statements are usually a core part of any script or program. What are the conditional statements in our life? It’s decision-making. For example: If she finished her vegetables, give her dessert.
If this is true, do that; otherwise, do something else or nothing at all. Using conditional statements allows us to check for multiple conditions.
How to Write Conditional Statements in Python Click To TweetLet’s Create a Simple Rental Car Cost Estimator Script!
The project is broken into three sections:
- Collect customer input
- Calculate the costs from the customer input
- Display the results to the customer
Tips: Variables are reserved memory locations to store values. In this project, I asked a user to input the rental period. When users input the rental period, I recorded the value into a variable called rentalPeriod.
Tips: Python has five standard data types such as numbers, string, list, tuple, and dictionary. For the calculation, the variables data type should be numerical.
Final Output:
Rental Code: D
Rental Period: 5
Starting Odometer: 1234
Ending Odometer: 2222
Miles Driven: 988
Amount Due: $422.00
Rental Car Script:
import sys
rentalCode = input("(B)udget, (D)aily, or (W)eekly rental?\n")
if rentalCode == "W":
rentalPeriod = int(input("Number of Weeks Rented:\n"))
else:
rentalPeriod = int(input("Number of Days Rented:\n"))
budgetCharge = 40.00
dailyCharge = 60.00
weeklyCharge = 190.00
if rentalCode == "B":
baseCharge = rentalPeriod * budgetCharge
elif rentalCode == "D":
baseCharge = rentalPeriod * dailyCharge
else:
baseCharge = rentalPeriod * weeklyCharge
odoStart = int(input("Starting Odometer Reading:\n"))
odoEnd = int(input("Ending Odometer Reading:\n"))
totalMiles = odoEnd - odoStart
if rentalCode == "B":
mileCharge = totalMiles * 0.25
if rentalCode == "D":
averageDayMiles = totalMiles/rentalPeriod
if averageDayMiles > 100:
extraMiles = averageDayMiles - 100
else:
extraMiles = 0
mileCharge = extraMiles * 0.25
if rentalCode == "W":
averageWeekMiles = totalMiles/rentalPeriod
if averageWeekMiles > 900:
mileCharge = 100 * rentalPeriod
else:
mileCharge = 0
amtDue = baseCharge + mileCharge
print("Rental Summary")
print("Rental Code:", rentalCode)
print("Rental Period:", rentalPeriod)
print("Starting Odometer:", odoStart)
print("Ending Odometer:", odoEnd)
print("Miles Driven:", totalMiles)
print("Amount Due: $", '%.2f' % amtDue)
Explanation line by line
Collect customer data and prompt the user to input the rental code. Store the returned value as the variable rentalCode. The data type is a string. The returned value will be one out of three: B for budget, D for daily, W for weekly.
rentalCode = input("(B)udget, (D)aily, or (W)eekly rental?\n")
Next, request the time period the car was rented. If the variable rentalCode is W, prompt the user to input how many weeks the car was rented. Store the value into a variable rentalPeriod. The data type is numerical. If the variable rental code is not W, prompt the user to input how many days the car was rented. Store the returned value into a variable rentalPeriod. The data type is numerical.
if rentalCode == "W":
rentalPeriod = int(input("Number of Weeks Rented:\n"))
else:
rentalPeriod = int(input("Number of Days Rented:\n"))
Store the numbers for calculation.
budgetCharge = 40.00
dailyCharge = 60.00
weeklyCharge = 190.00
If the variable rental code is B, run this calculation: rental period x budget charge. Store the returned value into a variable baseCharge. If the variable rental code is D, run this calculation: rental period x daily charge. Store the returned value into a variable baseCharge. If the variable rental code is not B and D, run this calculation: rental period x weekly charge. Store the returned value into a variable baseCharge.
if rentalCode == "B":
baseCharge = rentalPeriod * budgetCharge
elif rentalCode == "D":
baseCharge = rentalPeriod * dailyCharge
else:
baseCharge = rentalPeriod * weeklyCharge
Collect mileage information. Prompt the user to input the starting odometer reading and store the returned value into the variable odoStart. The data type is numerical.
odoStart = int(input("Starting Odometer Reading:\n"))
Prompt the user to input the ending odometer reading and store the returned value into the variable odoEnd. The data type is numerical.
odoEnd = int(input("Ending Odometer Reading:\n"))
Calculate total miles! Run this calculation: odo end – odo start. And, store the returned value into variable totalMiles.
totalMiles = odoEnd - odoStart
Calculate mile charge. If the variable rentalCode is B, run this calculation: total miles x 0.25. Store the returned value into a variable mileCharge.
if rentalCode == "B":
mileCharge = totalMiles * 0.25
If the variable rentalCode is D, run this calculation: total miles devided by rental period. Store the returned value into a variable averageDayMiles. If the variable averageDayMiles is 100 or less, run this calculation: average day miles – 100. Store the returned value into a variable extraMile. If the variable rentalCode is not D and the variable averageDayMiles is not 100 or less, define extra miles as 0. And then, run this calculation: extra miles x 0.25. Store the returned value into a variable mileCharge.
if rentalCode == "D":
averageDayMiles = totalMiles/rentalPeriod
if averageDayMiles > 100:
extraMiles = averageDayMiles - 100
else:
extraMiles = 0
mileCharge = extraMiles * 0.25
If the variable rentalCode is W, run this calculation: total miles devided by rental period. If the variable averageWeekMiles is 900 or less, run this calculation: 100 x rental period. If the variable rentalCode is not W and the variable averageWeekMiles is not 900 or less, define mile charge as 0.
if rentalCode == "W":
averageWeekMiles = totalMiles/rentalPeriod
if averageWeekMiles > 900:
mileCharge = 100 * rentalPeriod
else:
mileCharge = 0
Find out the total amount. Run this calculation: base charge + mile charge.
amtDue = baseCharge + mileCharge
Output rental summary, rental code, rental period, starting odometer, ending odometer, miles driven, and amount due.
print("Rental Summary")
print("Rental Code:", rentalCode)
print("Rental Period:", rentalPeriod)
print("Starting Odometer:", odoStart)
print("Ending Odometer:", odoEnd)
print("Miles Driven:", totalMiles)
print("Amount Due: $", '%.2f' % amtDue)
Wrapping Up
I hope you enjoy creating a simple rental car cost estimator as much as I enjoyed doing this! You can include these types of projects into your portfolio by creating your own site or simply uploaded to Dropbox and share it with your clients.