Buying bunnies, eggs and elections with tax and a discount I have a small problem with the code below. Although it executes the task perfectly, It's supposed to be more "short" and compact according to the assignment feedback. I was wondering if someone can please help me change my code from its longer version to a more short version that completes the same task...

from math import *

ask = input("Would you like to buy bunnies, eggs, or elections?: ")
if ask == 'bunnies':
    bunnies = 30
    print("Bunnies will cost " + str(bunnies) + " dollars each.")
    quantity = int(input("How many would you like to buy?: "))
    cost1 = bunnies * quantity
    tax = (0.089 * cost1)
    final_cost = (cost1 + tax)
    if final_cost == final_cost &gt; 57.89:
        discount1 = (0.13 * final_cost)
        with_discount1 = (final_cost - discount1)
        print("Your total will be " + str(with_discount1) + " with a discount.")
    if final_cost == final_cost &lt; 57.89:
        print("Your total will be " + str(final_cost) + " without a discount")

if ask == 'eggs':
    eggs = 10
    print("Eggs will cost " + str(eggs) + " dollars each.")
    quantity = int(input("How many would you like to buy?: "))
    cost2 = eggs * quantity
    tax = (0.089 * cost2)
    final_cost2 = (cost2 + tax)
    if final_cost2 == final_cost2 &gt; 57.89:
        discount = (0.13 * final_cost2)
        with_discount = (final_cost2 - discount)
        print("Your total will be " + str(with_discount) + " with a discount for eggs.")
    if final_cost2 == final_cost2 &lt; 57.89:
        print("Your total will be " + str(final_cost2) + " without a discount for eggs")

if ask == 'elections':
    elections = 20
    print("Elections will cost " + str(elections) + " dollars.")
    quantity = int(input("How many are you looking to purchase?: "))
    cost3 = quantity * elections
    tax = (0.089 * cost3)
    final_cost3 = (cost3 + tax)
    if final_cost3 == final_cost3 &gt; 57.89:
        discount2 = (0.13 * final_cost3)
        withdiscount = (final_cost3 - discounts)
        print("Your total will be " + str(withdiscount) + " dollars with a discount for elections.")
    if final_cost3 == final_cost3 &lt; 57.89:
        print("Your total will be " + str(final_cost3) + " dollars without a discount for elections.")
Using from math import * is highly discouraged. Please always use either:


from math import sqrt, or
import math and use math.sqrt instead of sqrt.

You are not using math and so the import is not needed.
It is commonly recommended to use either f-strings or str.format to format your strings. This is as they make reading the format easier on more complex formats.

In your case you won't see this benefit too much but it would be a good habit to get now, rather than later.

print("Bunnies will cost {} dollars each.".format(bunnies))
print(f"Bunnies will cost {bunnies} dollars each.")

Please don't use unnecessary parentheses. This is as they add unneeded clutter to your code.
You can simplify the calculation for final_cost

$$
\begin{array}{r l}
c &amp;= bq\\
t &amp;= 0.089c\\
f &amp;= c + t\\
f &amp;= 0.089c + c\\
f &amp;= (0.089 + 1)c\\
f &amp;= 1.089bq
\end{array}
$$
The statement final_cost == final_cost &gt; 57.89 is confusing and only works due to Python splitting the code into two different conditionals connected with an and.

The statement final_cost == final_cost will always be true, and so by all metrics is just bad.
Your ifs are missing if the final cost is 57.89 exactly. I assume this is a mistake.

When you have two ifs like this when one is getting half the options and the other is getting the other half it is better to use an if and an else rather than two ifs.
You don't need to store discount1 in a variable, it's just adding lines with no visible benefit.
Whilst there's nothing inherently wrong with printing the same string with a slight modification twice, you may be inclined to change it so you only define the structure of the print once.


Overall this would get:

bunnies = 30
print(f"Bunnies will cost {bunnies} dollars each.")
quantity = int(input("How many would you like to buy?: "))
final_cost = 1.089 * bunnies * quantity
if final_cost &gt;= 57.89:
    with_discount = final_cost - 0.13 * final_cost
    print(f"Your total will be {with_discount} with a discount")
else:
    print(f"Your total will be {final_cost} without a discount")


From here we can see all the other options have almost exactly the same code. There are only three things that change:


The variable name bunnies.
The value of the variable bunnies
The name of the item you're buying.


From this we can see that a function would be good.

def price_to_buy(item, price):
    print(f"{item} will cost {price} dollars each.")
    quantity = int(input("How many would you like to buy?: "))
    final_cost = 1.089 * price * quantity
    if final_cost &gt;= 57.89:
        with_discount = final_cost - 0.13 * final_cost
        print(f"Your total will be {with_discount} with a discount")
    else:
        print(f"Your total will be {final_cost} without a discount")


ask = input("Would you like to buy bunnies, eggs, or elections?: ")
if ask == "bunnies":
    price_to_buy("Bunnies", 30)
if ask == "eggs":
    price_to_buy("Eggs", 10)
if ask == "elections":
    price_to_buy("Elections", 20)


Advanced changes

Whilst the above is probably what your instructor expects from you, there are more ways to improve the code and make it shorter.


You can store the name and value in a dictionary, allowing you to condense those ifs into two lines of code.

You may want to use a try and except here to get the code to function the same if you don't enter valid input.
You can use str.title() to make the inputted item's name display in title case.
You can use an if __name__ == "__main__": guard to prevent the code from running when imported, normally by accident.
You can use a turnery to apply the discount, this is basically just an if and else but on one line!

By also using tuple unpacking we can get the preposition (with/without) and the discount percentage in one line.
You can use a try and except to display a nice error message.
You can use foo -= ... rather than foo = foo - ....


def price_to_buy(item, price):
    print(f"{item} will cost {price} dollars each.")
    value = input("How many would you like to buy?: ")
    try:
        quantity = int(value)
    except ValueError:
        print(f"{value} is not an integer.")
        return
    cost = 1.089 * price * quantity
    discount, prep = (0.13, "with") if cost &gt;= 57.89 else (0, "without")
    cost -= discount * cost
    print(f"Your total will be {cost} {prep} a discount")


PRICES = {"bunnies": 30, "eggs": 10, "elections": 20}

if __name__ == "__main__":
    item = input("Would you like to buy bunnies, eggs, or elections?: ")
    price_to_buy(item.title(), PRICES[item])