Python-如果有更多输入返回错误(怎么办)
初学者程序员,我正在做一个平方根计算的函数, 以下是我的功能: 输入是一个元组,我希望它是这样的Python-如果有更多输入返回错误(怎么办),python,calculator,Python,Calculator,初学者程序员,我正在做一个平方根计算的函数, 以下是我的功能: 输入是一个元组,我希望它是这样的 (“sqrt”,编号) 零件任何东西我应该设置什么功能??我尝试得很糟糕 if input[1] != "" 当然,我在尖叫时遇到了一个巨大的错误…使用内置的len()函数确保长度不大于2: def calculator(input): if len(input) > 2: print "Please only one input" break
(“sqrt”,编号)
零件任何东西
我应该设置什么功能??我尝试得很糟糕
if input[1] != ""
当然,我在尖叫时遇到了一个巨大的错误…使用内置的len()
函数确保长度不大于2:
def calculator(input):
if len(input) > 2:
print "Please only one input"
break
else:
#Do the rest of your code
if input[0] == "sqrt":
if input[1]<0 : # Error for negative input of number
return "No square root for negative numbers"
else:
ans = (input[1])**(0.5)
return ans
def计算器(输入):
如果len(输入)>2:
打印“请仅输入一项”
打破
其他:
#完成代码的其余部分
如果输入[0]=“sqrt”:
如果输入[1]请尝试此代码
def calculator(*args):
input=args[0]
num1 = args[1]
numberOfArguments = len(args)
print input
print num1
print len(args)
if numberOfArguments > 2 :
print "Please only one input"
else:
if input == "sqrt":
print "we will perform sqrt now"
if num1 < 0 : # Error for negative input of number
return "No square root for negative numbers"
else:
ans = (num1)**(0.5)
print "square root result is ",ans
return ans
用1个输入调用
希望这有帮助:)
def calculator(input):
if len(input) > 2:
print "Please only one input"
break
else:
#Do the rest of your code
if input[0] == "sqrt":
if input[1]<0 : # Error for negative input of number
return "No square root for negative numbers"
else:
ans = (input[1])**(0.5)
return ans
def calculator(*args):
input=args[0]
num1 = args[1]
numberOfArguments = len(args)
print input
print num1
print len(args)
if numberOfArguments > 2 :
print "Please only one input"
else:
if input == "sqrt":
print "we will perform sqrt now"
if num1 < 0 : # Error for negative input of number
return "No square root for negative numbers"
else:
ans = (num1)**(0.5)
print "square root result is ",ans
return ans
calculator("sqrt",25,2)
calculator("sqrt",25)