[英]python basics - I am trying to write a function which takes a string(a month) as an input and returns the amount of days in the month
[英]How do I write a function in python that takes as input 2 numbers, and 1 string
我尝试这样做,但我的代码不起作用。 当我调用operate(4,5,add)
时,我有一个回溯,上面写着"name 'add' is not defined"
。 请你帮助我好吗?
def operate(x,y,z):
if (z == "add"):
op = x+y
print("%d + %d = %d" %(x, y, op))
elif (z == "multiply"):
op = x*y
print("%d * %d = %d" %(x, y, op))
add
不是字符串。 'add'
和"add"
是字符串。 您需要使用operate(4, 5, 'add')
或operate(4, 5, "add")
调用您的 function。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.