-1

I am learning python for the first time(complete beginner) and i have to make this program:

In a store the products have discounts depending on their code:

• The products with code 45612 have 10% discount

• The products with code 45613 have 12% discount

• The products with code 45614 have 18% discount

• The products with code 45615 have 20% discount

Write an algorithm that reads the code (int) and value (float) a product and calculate the price after discount.

If the code you have entered doesn't exist, it should print "The password is not right.

But my program doesn't work, what am i doing wrong?

This is my code:

kodikos = [45612, 45613, 45614, 45615]

password = input("Give code: ")

timi = float(input("Give price: "))



if password == 45612:

     timi = timi - 10
     print("The price after the discount is %f" %(timi))

if password == 45613:

     timi = timi - 12
     print("The price after the discount is %f" %(timi))

if password == 45614:

     timi = timi - 18
     print("The price after the discount is %f" %(timi))

if password == 45615:

     timi = timi - 20
     print("The price after the discount is %f" %(timi))


for n in kodikos:

     if(n != password):
         print("The password doesn't exist!")
         break
Bhargav Rao
  • 50,140
  • 28
  • 121
  • 140
  • 1
    `input()` gives you a string object, you are comparing an `int` object in your if statement. You can't compare apples to oranges – MooingRawr Nov 10 '16 at 19:14
  • 1
    I don't know. What _are_ you doing wrong? – Christian Dean Nov 10 '16 at 19:14
  • you should also check if your given `code` input is valid first for an if block statement, as in if code is valid then apply discount, else tell user not valid code. Also use `elif` to chain if blocks that are similar to each other – MooingRawr Nov 10 '16 at 19:16
  • It's ok i found the problem.. @MooingRawr found my mistake! Thanks a lot! Now everything works! :) – Kristian Menaj Nov 10 '16 at 19:17
  • @KristianMenaj Also i believe you don't want your for loop to do what you want done. try `if int(password) not in kodikos: print("password not found")` (replace the for loop with that statement) – MooingRawr Nov 10 '16 at 19:18

1 Answers1

0

You subtract a fixed amount but must subtract a relative amout (i.e. 10 per cent of the value). Try this code:

kodikos = [45612, 45613, 45614, 45615]

password = input("Give code: ")

timi = float(input("Give price: "))



if password == 45612:

     timi = timi - 0.1 * timi
     print("The price after the discount is %f" %(timi))

if password == 45613:

     timi = timi - 0.12 * timi
     print("The price after the discount is %f" %(timi))

if password == 45614:

     timi = timi - 0.18 * timi
     print("The price after the discount is %f" %(timi))

if password == 45615:

     timi = timi - 0.2 * timi
     print("The price after the discount is %f" %(timi))


for n in kodikos:

     if(n != password):
         print("The password doesn't exist!")
         break
Ukimiku
  • 608
  • 1
  • 6
  • 13