Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/293.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_Calculator - Fatal编程技术网

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)