Question

In: Computer Science

def vend():     a = {'key':'0','item': 'chips', 'price': 1.50, 'stock': 6}     b = {'key':'1','item': 'cereal',...

def vend():

    a = {'key':'0','item': 'chips', 'price': 1.50, 'stock': 6}
    b = {'key':'1','item': 'cereal', 'price': 1.25, 'stock': 10}
    c = {'key':'2','item': 'pop', 'price': 1.75, 'stock': 12}
    d = {'key':'3','item': 'apple', 'price': 2.50, 'stock': 6}
    e = {'key':'4','item': 'orange', 'price': 1.95, 'stock': 10}
    items = [a, b, c, d, e]
    credit = 0 # cash in machine


# show items, prices
def show(items):
    while True:
            s = input("> ")
            if s == "items":
                print(items)
            for item in items:
                if item.get('stock') == 0:
                    items.remove(item)
                    print("Out of stock")
                for item in items:
                    print ('{0} {1} ${2} ({3} available)'.format(item.get('key'), item.get('item'), item.get('price'), item.get('stock')))

    vend = True
    # have user choose item
    while vend == True:
        show(items)
        print('credit: $0.00')
        selected = input('>')
        for key in items:
            if selected == key.get('key'):
                selected = key
                print("MSG: Insufficient Credit")
                print('credit: $0.00')
                price = selected.get('price')
                while credit < price:
                 
                    currency={"penny":0.01,"nickel":0.05,"dime":0.1,"quarter":0.25}
                    cred=input('>')
                    credit=credit+currency[cred]
                    print("CREDIT: ${0}".format(credit))

                print('vend:' + selected.get('item'))
                selected['stock'] -= 1
                credit -= price
                print('return:' + str(credit))
                print('credit: $0.00')
                break
            else:
                continue
vend()

can you fix the def show items

what i want it to do is wait for the user to input and when the user types in items then it will print the items

and could you add a way for the user to restock items

Solutions

Expert Solution

items = []
credit = 0

def vend():
    a = {'key':'0','item': 'chips', 'price': 1.50, 'stock': 6}
    b = {'key':'1','item': 'cereal', 'price': 1.25, 'stock': 10}
    c = {'key':'2','item': 'pop', 'price': 1.75, 'stock': 12}
    d = {'key':'3','item': 'apple', 'price': 2.50, 'stock': 6}
    e = {'key':'4','item': 'orange', 'price': 1.95, 'stock': 10}
    items.append(a)
    items.append(b)
    items.append(c)
    items.append(d)
    items.append(e)
    credit = 0 # cash in machine

# show items, prices
def show(items):    
    print("Available items in vending machine: ")
    for item in items:
        if item.get('stock') == 0:
            items.remove(item)
        else:
            print('{0} {1} ${2} ({3} available)'.format(item.get('key'), item.get('item'), item.get('price'), item.get('stock')))

if __name__ == "__main__":
    vend()
    buy = True
    # have user choose item
    while buy == True:
        s = input("> Enter items to buy from vending machine or add to add an item: ")
        if s == "items":
            show(items)
            selected = input('>Select an item number(0/1/..) to purchase: ')
            for item in items:
                if selected == item.get('key'):
                    selected_item = item                
                    price = selected_item.get('price')
                    while credit < price:
                        print("MSG: Insufficient Credit")
                        print('CREDIT: $%.2f' % credit)
                        print("Please Enter a 'penny', 'nickel', 'dime' or a 'quarter'")
                        currency={"penny":0.01,"nickel":0.05,"dime":0.1,"quarter":0.25}
                        cred=input('>')
                        credit=credit+currency[cred]
                        print("CREDIT: $%.2f" % credit)

                    print('VENDING OUT:' + selected_item.get('item'))
                    selected_item['stock'] -= 1
                    credit -= price
                    print('return: $%.2f' % credit)
                    credit = 0
                    print('credit: $%.2f' % credit)
                    break
            choice = input("Do you want to purchase more items? (Y/N) : ")
            if choice == "N":
                buy = False
        elif s == "add":
            show(items)
            choice = input("Enter existing to increase stock of an existing item, or add to add a new item: ")
            if choice == "existing":
                key = input("Enter item's key: ")
                amount = int(input("Enter number of items to add: "))
                for item in items:
                    if item.get('key') == key:
                        item['stock'] = item['stock'] + amount
                        break
            elif choice == "add":
                key = input("Enter key of item: ")
                name = input("Enter item's name: ")
                amount = int(input("Enter stock: "))                
                price = float(input("Enter price of the item: "))
                items.append({
                    'key':key,
                    'item':name,
                    'price':price,
                    'stock':amount
                })
            print("New contents of vending machine")
            show(items)


- User is asked if he wants to buy some item or add items to machine
- If he enters items
    - He is shown the list of available items
    - He is asked to enter key of some item he wants to buy
    - then he is prompted to add penny/wuarter/nickel/dime
    - after the purchase is successful, he is asked if he wants to purchase some more items
    - if he enters Y, process repeats, else process restarts from starting
- If he enters add
    - He is prompted to add a new item or increase stock of existing item

See sample run below


Related Solutions

def vend():     a = {'key': '0', 'item': 'choc', 'price': 1.50, 'stock': (2)}     b = {'key': '1',...
def vend():     a = {'key': '0', 'item': 'choc', 'price': 1.50, 'stock': (2)}     b = {'key': '1', 'item': 'pop', 'price': 1.75, 'stock': 1}     c = {'key': '2', 'item': 'chips', 'price': 2.00, 'stock': 3}     d = {'key': '3', 'item': 'gum', 'price': 0.50, 'stock': 1}     e = {'key': '4', 'item': 'mints', 'price': 0.75, 'stock': 3}     items = [a, b, c, d, e]          def show(items):         cim = 0         for item in items:             if item.get('stock') == 0:                 items.remove(item)         for item in items:             print(item.get('key'), item.get('item'),item.get('price'),...
def hiCount(iterable): rtnVal = 0 most = '' for item in iterable: num = iterable.count(item) if...
def hiCount(iterable): rtnVal = 0 most = '' for item in iterable: num = iterable.count(item) if num > rtnVal: most = item rtnVal = num return tuple(most, rtnVal) lyric = "you don't own me" print(hiCount(lyric)) does all the for then the return Python
1) The price of DEF Corp. stock is $50 per share and the call option on...
1) The price of DEF Corp. stock is $50 per share and the call option on the stock has a price of $10 and an exercise price of $45, with a time to maturity of one year. Assume the risk-free rate is 6%. (5 pts.) a. What is the price of a put option on the same stock with the same exercise price and maturity?   b. If the volatility of the stock is 20% during the year, use the two-state...
Question 1-6 are based on the following series of futures price (F(0), F(1),... F(6)): Day 0:...
Question 1-6 are based on the following series of futures price (F(0), F(1),... F(6)): Day 0: F(0)=$212 Day 1: F(1)=$211 Day 2: F(2)=$214 Day 3: F(3)=$209 Day 4: F(4)=$210 Day 5: F(5)=$202 Day 6: F(6)=$200 Suppose you are going to long 20 contracts. The initial margin=$10 per contract, and the maintenance margin is $2. 1) from the set of information: how much do you need to deposit in the trading account at Day 0? 2) Using the same set of...
6) The valid range of sample correlation coefficient is A) 0 ≤ ? ≤ 1 B)...
6) The valid range of sample correlation coefficient is A) 0 ≤ ? ≤ 1 B) −1 ≤ ? ≤ 1 C) 0 < ? < 1 D) −1 ≤ ? ≤ 1 7) The method used to find the estimate of the parameters in the classic regression model is called A) Classic Least Squares B) Ordinary Least Squares C) Generalized Least Squares D) Weighted Least Squares 8) Which of the following statement is true? A) RSS = TSS +...
1. a. You are creating an index for the four following stocks: Stock Price, t=0 Price,...
1. a. You are creating an index for the four following stocks: Stock Price, t=0 Price, t=1 Shares (million) ABC 122 107 100 DEF 46 50 500 GHI 21 26 1,200 JKL 26 30 450 What is the one day return for the index if it is price-weighted? Present your answer as the percent change from t=0 to t=1 to the nearest two decimals in this format, 1.23% b. Stock Price, t=0 Price, t=1 Shares (million) ABC 122 107 100...
def annoying_factorial(n): if n == 0 or n == 1: return 1 if n == 2:...
def annoying_factorial(n): if n == 0 or n == 1: return 1 if n == 2: return 2 if n == 3: return 6 if n == 4: return 4 * annoying_factorial(3) if n == 5: return 5 * annoying_factorial(4) if n == 6: return 6 * annoying_factorial(5) else: return n * annoying_factorial(n-1) def annoying_fibonacci(n): if n==0: return 0 if n==1: return 1 if n==2: return 1 if n==3: return 2 if n==4: return annoying_fibonacci(4-1)+annoying_fibonacci(4-2) if n==5: return annoying_fibonacci(5-1)+annoying_fibonacci(5-2) if...
a) Describe the key feature of a zero-coupon bond. (1 mark) b) “The price of a...
a) Describe the key feature of a zero-coupon bond. (1 mark) b) “The price of a zero coupon bond should be equal to its face value.” True or false? Explain. c) “The yield to maturity of a discount bond is greater than its coupon rate.” True or false? Explain. d) You just purchased a 10-year semi-annual coupon bond with a par value of $1,000 and a coupon rate of 8%. The nominal yield to maturity is 7% per annum. Calculate...
1. Fill the blanks. 1. The price of cereal and the demand for milk have a...
1. Fill the blanks. 1. The price of cereal and the demand for milk have a ...(1)... relationship. 2. Income tax and the demand for air travel have a ...(2).... relationship. Select one: a. 1. Positive & 2. Positive b. 1. Positive & 2. Negative c. 1. Negative & 2. Positive d. 1. Negative & 2. Negative e. These variables have a neutral relation, which means that they do not affect each other 2. What would happen if the price...
Sanford common stock is expected to pay $1.50 in dividends next​ year, and the market price...
Sanford common stock is expected to pay $1.50 in dividends next​ year, and the market price is projected to be $52.35 per share by​year-end. If investors require a rate of return of 13 percent, what is the current value of the​ stock?
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT