timber101

J277Q5d

Dec 26th, 2021 (edited)
1,085
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.54 KB | None
  1. ### discount applied if voucher code is valid
  2.  
  3. discount =[["PVFC7", 10],["CPU5", 5],["BGF2", 10],["Colin", 100]]
  4.  
  5. """
  6.    0        1
  7. 0.  PVFC1   10
  8. 1.  CPU5     5
  9. 2.  BGF2    10
  10. 3.  Colin  100
  11. """
  12.  
  13. # pass a price and code when calling the function
  14. def checkdiscount(price, code):
  15.     #newprice = price
  16.     for x in range(len(discount)): # loops down whole array
  17.         if discount[x][0] == code: # checks if the code matches
  18.             price = price - discount[x][1] # if it does, update
  19.             # newprice by taking away the value at [x][1]
  20.     return price # pass out the newprice
  21.  
  22.  
  23. ## call the function
  24. revised_price = (checkdiscount(500,"Colin"))
  25.  
  26. print("the revised price is >>", revised_price)
  27.  
  28. # asks for item price and a code
  29. # uses check discount from previous qn - call the function
  30. # repeats until 0 is entered as a price , accumulating the total price
  31. # outputs total cost of all items
  32. # 6 marks
  33.  
  34. tot_price = 0 # declares variable acalled tot_price and set to 0
  35. price_input = 1 # ensure the while loop runs at least once
  36.  
  37. while price_input != 0:
  38.     price_input = int(input("Please enter the price >> ")) ##
  39.     if price_input != 0:
  40.         code_input = input("Please enter the code >> ")
  41.         new_price = checkdiscount(price_input, code_input)
  42.         tot_price = new_price + tot_price
  43.         print("test",price_input, code_input, new_price, tot_price)
  44.  
  45. print("Total cost = ", tot_price)
  46.  
  47. ##  extension if enter 9999  prompts to add a new code and value
  48. ##  my answer here  https://pastebin.com/H88UJizL
  49.  
RAW Paste Data Copied