Python syntax errors - coin machine problem

197 views Asked by At

I know these are very basic questions but cannot figure out what I'm doing wrong. I'm just beginning to learn python and don't understand why I am getting a syntax error every time I try to subtract something.

When I try to run this:

```#def variables - input cash entered and item price in float parentheses
cash = float(400)
price = float(215)

#change calculation

def cash_owed(cash - price)```

I get a

SyntaxError: invalid syntax with the ^ pointing to the - sign.

I can't find any information about why using a subtraction sign would return a syntax error in this context. What am I doing wrong?

I am trying to create a coin machine program where the cash is entered in integers representing cents (i.e $5 = 500) returns the required change in the most convenient coin denominations. This is the rest of the code that I wrote, but I can't even get past that first syntax error.

cash = float(400)
price = float(215)

#change calculation

def cash_owed(cash - price)

c = cash_owed

#display amount recieved, cost of item, required change
print ("Amount recieved : " + str(cash)) \n
print ("Cost of item : " + str(float)) \n
print("Required change : " + str(c)) \n

#calculate toonies owed
def calculate_toonies(c//200)
round(calculate_toonies)

print("Toonies x " + str(calculate_toonies))

c = c%200

#calculate loonies owed
def calculate_loonies(c//100)
round(calculate_loonies)

print("Loonies x " + str(calculate_loonies))

c = c%100

#calculate quarters owed
def calculate_quarters(c//25)
round(calculate_quarters)

print("Quarters x " + str(calculate_quarters))

c = c%25

#calculate dimes owed
def calculate_dimes(c//10)
round(calculate_dimes)

print("Dimes x " + str(calculate_dimes))

c = c%10

#calculate nickles owed
def calculate_nickles(c//5)
round(calculate_nickles)

print("Nickles x " + str(calculate_nickles))

c = c%5```
2

There are 2 answers

0
frunkad On

Your function definition is wrong. The parameter cannot do an operation & should contain a colon.

Change

def cash_owed(cash - price)

To

def cash_owed(cash, price):
  new_price = cash - price
1
Aditya Gaur On

You have to put a colon after function You can try this:

def cash_owed(cash, price):
    return(cash - price)