Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/285.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python语法错误-投币机问题_Python_Coin Change - Fatal编程技术网

Python语法错误-投币机问题

Python语法错误-投币机问题,python,coin-change,Python,Coin Change,我知道这些都是非常基本的问题,但我无法找出我做错了什么。我刚刚开始学习python,不明白为什么每次尝试减法时都会出现语法错误 当我尝试运行此命令时: ```#def variables - input cash entered and item price in float parentheses cash = float(400) price = float(215) #change calculation def cash_owed(cash - price)``` 我得到一份工作

我知道这些都是非常基本的问题,但我无法找出我做错了什么。我刚刚开始学习python,不明白为什么每次尝试减法时都会出现语法错误

当我尝试运行此命令时:

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

#change calculation

def cash_owed(cash - price)```
我得到一份工作

SyntaxError:无效语法,^指向-符号

我找不到任何关于为什么在这种情况下使用减法符号会返回语法错误的信息。我做错了什么?

我正在尝试创建一个硬币机程序,在该程序中,现金以整数形式输入,表示美分(即$5=500),返回最方便的硬币面额所需的变化。这是我编写的其余代码,但我甚至无法通过第一个语法错误

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```

你的函数定义是错误的。参数无法执行操作&应包含冒号

改变

def cash_owed(cash - price)


你必须在函数后面加一个冒号 您可以尝试以下方法:

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

欢迎来到堆栈溢出!虽然这段代码可以解决这个问题,但如何以及为什么解决这个问题将真正有助于提高您的帖子质量,并可能导致更多的投票。请记住,你是在将来回答读者的问题,而不仅仅是现在提问的人。请在回答中添加解释,并说明适用的限制和假设。
def cash_owed(cash, price):
    return(cash - price)