15,848,407 members
See more:
I'm checking to see if code below can be optimized by avoiding the repetition of the same 'else' statement in multiple nested if/else.

What I have tried:

PythonLearning/B3: if-else, nested at c54aa0a5b1b4084bb09fde7b2cb99285821d668e · manbirs/PythonLearning · GitHub[^]

```"""You are working at the dealsership of Alpha Motors. A customer walked in and needed help to decide as which car type to purchase (Electric vs. Gasoline). You have created a program that will assist in the decision"""

print("Welcome to Alpha Motors!")
#Input as if they are interested in Electric Car? 'Y' or 'N'
Interested_in_Electric_Car = input("Are you interested in buying Electric Car?")
if Interested_in_Electric_Car == "Y":
print('Good.. few more questions')
#Do they have space for Dedicated Wall Charger?
Place_for_wall_charger = input("Is there a place for dedicated wall charger?")
if Place_for_wall_charger == 'Y':
EV_Sale_Price = 25000
Sales_Tax = 10
EV_Sale_Price_with_Sales_Tax = Sales_Tax / 100 * EV_Sale_Price + EV_Sale_Price
print(f"Good, you can buy an EV.Sales price for Electric Car is \${EV_Sale_Price} and sales tax is {Sales_Tax}%,so the total sales price for car including tax is \${EV_Sale_Price_with_Sales_Tax}")
Need_warranty = input("Need extended warranty on car?")
if Need_warranty == "Y":
EV_Sale_Price_with_Sales_Tax_and_Warranty =  3000 + EV_Sale_Price_with_Sales_Tax
print(f"Sale price with warranty is {EV_Sale_Price_with_Sales_Tax_and_Warranty}")
else:
print (f"Sale price without warranty is {EV_Sale_Price_with_Sales_Tax}")
else:
print("One last question")
#Is there supercharging avaialablity in their area?
Supercharging_availability = input("Is there supercharging in your area?")
if Supercharging_availability == "Y":
print("Good, you can buy EV")
EV_Sale_Price = 25000
Sales_Tax = 10
EV_Sale_Price_with_Sales_Tax = Sales_Tax / 100 * EV_Sale_Price + EV_Sale_Price
print(f"Good, you can buy an EV.Sales price for Electric Car is \${EV_Sale_Price} and sales tax is {Sales_Tax}%,so the total sales price for car including tax is \${EV_Sale_Price_with_Sales_Tax}")
Need_warranty = input("Need extended warranty on car?")
if Need_warranty == "Y":
EV_Sale_Price_with_Sales_Tax_and_Warranty =  3000 + EV_Sale_Price_with_Sales_Tax
print(f"Sale price with warranty is {EV_Sale_Price_with_Sales_Tax_and_Warranty}")
else:
print (f"Sale price without warranty is {EV_Sale_Price_with_Sales_Tax}")
else:
print("EV is not an option")

else:
#If they don't like an EV, then go with Gas car
print('Then, Go with Gas Car')
#Do they care about the gas bill
Care_about_gas_bill = input("Do you care about gas bill?")
if Care_about_gas_bill == "N":
#if no, sports car is good recommendation
SportsCar_Sale_Price = 75000
Sales_Tax = 10
SportsCar_Sales_Price_with_Sales_Tax = Sales_Tax / 100 * SportsCar_Sale_Price + SportsCar_Sale_Price
print(
f"Sales price for Sports Car is \${SportsCar_Sale_Price} and sales tax is {Sales_Tax}%,so the total sales price for car including tax is \${SportsCar_Sales_Price_with_Sales_Tax}")
Need_warranty = input("Need extended warranty on car?")
if Need_warranty == "Y":
SportsCar_Sales_Price_with_Sales_Tax_and_Warranty = 3000 + SportsCar_Sales_Price_with_Sales_Tax
print(f"Sales price with warranty is {SportsCar_Sales_Price_with_Sales_Tax_and_Warranty}")
else:
print(f"Sales price without warranty is {SportsCar_Sales_Price_with_Sales_Tax}")
else:
Hybrid_Sale_Price = 50000
Sales_Tax = 10
Hybrid_Sale_Price_with_Sales_Tax = Sales_Tax / 100 * Hybrid_Sale_Price + Hybrid_Sale_Price
print(
f"Sales price for Hybrid Car is \${Hybrid_Sale_Price} and sales tax is {Sales_Tax}%,so the total sales price for car including tax is \${Hybrid_Sale_Price_with_Sales_Tax}")
Need_warranty = input("Need extended warranty on car?")
if Need_warranty == "Y":
Hybrid_Sale_Price_with_Sales_Tax_and_Warranty = 3000 + Hybrid_Sale_Price_with_Sales_Tax
print(f"Sales price with warranty is {Hybrid_Sale_Price_with_Sales_Tax_and_Warranty}")
else:
print(f"Then, total is {Hybrid_Sale_Price_with_Sales_Tax}")```
Posted
Updated 25-May-22 3:49am

## Solution 1

You should create functions for the common code. For example each car will have a sale price and a tax level, so those values only need to be set once for any type. Each car offers extended warranty so that code can go into a common function. The main part of the program should offer the different types of car and then call a function to handle the specific details, and any other common parts of the code. So the structure should be something like:
Python
```def electric():
# this function should return the basic price and tax rate

def hybrid():
# the same as above
return 5000, 10 # base cost and tax rate

def get_warranty():
answer = input("Do you need extended warranty?")
if answer == "Y" or answer == "y":
extended = 3000
else
extended = 0

return extended

# start of main program
base_cost = 0
tax_rate = 0
warranty = 0

# print a list of the car types as a menu and input the user's choice
# call the relevant function to set the price and tax rate
base_cost, tax_rate = hybrid()

# call the warranty function
warranty = get_warranty()

# add price tax and warranty values together and display the final total.

total_cost = base_cost + ...```

v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Top Experts
Last 24hrsThis month
 Richard Deeming 135 Richard MacCutchan 60 Graeme_Grant 50 Dave Kreskowiak 35 Maxim Kartavenkov 20
 OriginalGriff 2,233 Richard Deeming 1,010 Dave Kreskowiak 676 Graeme_Grant 639 Richard MacCutchan 570

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900