Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ### new version with ability to add new code number and discount
- ### discount applied if voucher code is valid
- discount =[["PVFC7", 10],["CPU5", 5],["BGF2", 10],["Colin", 100]]
- """
- 0 1
- 0. PVFC1 10
- 1. CPU5 5
- 2. BGF2 10
- 3. Colin 100
- """
- # pass a price and code when calling the function
- def checkdiscount(price, code):
- #newprice = price
- for x in range(len(discount)): # loops down whole array
- if discount[x][0] == code: # checks if the code matches
- price = price - discount[x][1] # if it does, update
- # newprice by taking away the value at [x][1]
- return price # pass out the newprice
- ## call the function
- revised_price = (checkdiscount(500,"Colin"))
- print("the revised price is >>", revised_price)
- # asks for item price and a code
- # uses check discount from previous qn - call the function
- # repeats until 0 is entered as a price , accumulating the total price
- # outputs total cost of all items
- # 6 marks
- tot_price = 0 # declares variable acalled tot_price and set to 0
- price_input = 1 # ensure the while loop runs at least once
- while price_input != 0:
- price_input = int(input("Please enter the price 0 to quit, 99999 to add new code >> ")) ##
- if price_input == 99999:
- new_code = input("Please enter new code to add >> ")
- new_discount = int(input("Please enter new discount amount >> "))
- new_list_item = [new_code, new_discount]
- discount.append(new_list_item)
- continue
- if price_input != 0:
- code_input = input("Please enter the code >> ")
- new_price = checkdiscount(price_input, code_input)
- tot_price = new_price + tot_price
- print("test",price_input, code_input, new_price, tot_price)
- print("Total cost = ", tot_price)
Add Comment
Please, Sign In to add comment