-2

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```
Heikki
  • 2,214
  • 19
  • 34
Lily
  • 1
  • 1

2 Answers2

1

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
frunkad
  • 2,433
  • 1
  • 23
  • 35
0

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

def cash_owed(cash, price):
    return(cash - price)
Aditya Gaur
  • 1
  • 1
  • 1
  • Welcome to Stack Overflow! While this code may solve the question, [including an explanation](//meta.stackexchange.com/q/114762) of how and why this solves the problem would really help to improve the quality of your post, and probably result in more up-votes. Remember that you are answering the question for readers in the future, not just the person asking now. Please [edit] your answer to add explanations and give an indication of what limitations and assumptions apply. – rizerphe Dec 02 '20 at 07:09