【发布时间】:2017-11-30 18:40:43
【问题描述】:
这是练习:
单参数是带 * 或 / 操作符的操作符
默认运算符是“*”(乘)
返回乘法或除法的结果
如果运算符不是“*”或“/”,则返回“无效运算符”
这里的说明有很多让我困惑的地方,我知道我还没有解决它,但这是我最接近解决这个问题的方法:
def multiply(operator):
op = (x)
return op
ops = input ("what would you like the operator to be? ")
x = input()
y = input()
if ops == "*":
return int (x) * int (y)
elif ops == "/":
return int (x) / int (y)
else:
return "invalid operator"
print (multiply())
我不能将`return'保留在函数之外,那么我是否将所有条件都保留在函数中?
【问题讨论】:
-
你应该从学习基本的 Python 语法开始,参见例如sopython.com/wiki/What_tutorial_should_I_read%3F
-
您应该修复python代码的格式,以便保留缩进,否则它是不可读的(特别是因为缩进很重要,它会改变代码的行为,在python中!)。您可以在代码块周围使用 Ctrl+K 对其进行缩进,使其成为代码,请参阅stackoverflow.com/editing-help#code
标签: python python-3.x calculator