timber101

J277Q5d extension

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