educative.io

Educative

My code - reason why I got an incorrect code

Hello,
for this exercise I wrote this code:
price=250
if price>=300:
price - (price30/100)
elif (price>=200 and price<300):
print(price - (price
20/100))
elif (price>=100 and price<200):
price - (price10/100)
elif (price>=0 and price<100):
price - (price
5/100)
else:
price

and I got the message that this produces incorrect output.
when I put this code into pycharm, my code is correct.
what is the reason?

Hi Jaksa,
This is Samreen Fatima from Educative.

In order to correct your code, consider the following:

  1. Add the multiplication operator between the price and the following number, i.e. price * 20 instead of price20.
  2. Store the discounted price in the price variable as mentioned in the lesson:

“The price variable has already been created. You can use it in your code and assign it a new value based on the discount.

This helps us to evaluate your newly calculated price.

The correct version of your code is as follows:

price=250
if price>=300:
   price = price - (price*30/100)
elif (price>=200 and price<300):
    price = price - (price*20/100)
elif (price>=100 and price<200):
    price = price - (price*10/100)
elif (price>=0 and price<100):
    price = price - (price*5/100)
else:
    price

Best Regards,
Samreen Fatima | Developer Advocate
Educative

1 Like

Thanks a lot Fatima, it was very useful.

I am also facing a similar problem but it is working fine on my computer
…please check through my codes( ps: there is * between the percentage fraction and price but it is not showing

price = 150
if price>=300:
print (‘Price’, end=’ = ‘)
print(price-(30/100price))
elif price>=200:
print(‘Price’, end=’ = ')
print(price-(20/100
price))
elif price>=100:
print(‘Price’, end=’ = ‘)
print(price-(10/100price))
elif price<100:
print(‘Price’, end=’ = ')
print( price-(5/100
price))
elif price<0:
print(‘Price’, end=’ = ')
print(price)