Click here to Skip to main content
15,885,914 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Create a function called func1(acc, price, quantity, decision),acc should be a dictionary that holds two values: balance and stock_quantity. Set an initial account value in a main function. Price: current price ofstock, quantity is the number of stocks you want to like to buy or sell, decision is a boolean ;buy or sell (True = buy, False = sell). This function does the sell and buy of stocks,
updating account, it should print nice formatted information about what
transaction occured, what the current balance is and quantity of stock left.

-if the transaction you are trying to make is impossible (buying more stock than the value of your balance or selling more than you have), it should adjust the quantity. For instance, if you own 50 stocks and the input wants you to sell 60, you should only sell 50 shares.

When I use this code it keeps saying that acc is not defined

What I have tried:

Python
def func1(account,price,quantity,decision):
    stock_quantity = -1
    balance = -1
    account ={}
    account.update({'balance': balance})
    account.update({'stock_quantity': stock_quantity})
    print(account)
    print('Enter price')
    price=float(input())
    print('Enter quantity')
    quantity=int(input())
    print ('Enter True to buy and False to sell ')
    decision=bool (input())
    if decision==True:
        if (balance>= fprice):
            balance=balance-fprice
            stock_quantity= stock_quant+quantity
        else:
            t=balance//price
            balance=balance-(t*price)
            stock_quantity=stock_quantity+t
    else:
        if (quant>=quantity): 
            balance=balance+fprice
            stock_quant=stock_quant-quantity
        else:
            selling=stock_quantity*price
            balance=balance+ selling
Posted
Updated 24-Apr-20 16:23pm
v2

1 solution

The problem is that this single Python code has several naming problems. And the problem you are showing:
Quote:
When I use this code it keeps saying that acc is not defined
... is likely not to be raised from this piece of code. If it is, then just change the acc to account; since you have passed and created an "account" variable (again!)

Secondly, there are several other problems with the code. You can see, in the first line under decision == True: you have:
Python
if (balance>= fprice):
But you never defined fprice variable anywhere. On and on with so many other variables, like stock_quant, quant, etc.

Although Python is flexible with the naming, dynamic (or duck) typing, you at least need to provide the information to the interpreter to "know" that a variable was intended. Once you will create the variables, initialize with anything (since Python is not forcing a type) your "defined / undefined" problems will go away. This will introduce other problems like a type mismatch, but that can be easily tackled.

Please see these links to learn more on this:
typing — Support for type hints — Python 3.8.2 documentation[^]
python - input() error - NameError: name '...' is not defined - Stack Overflow[^] (Note that this answer discusses a case where your input values are shown as not defined in the Python code, read this if your "acc" comes from your input)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900