ValueError:数学域错误。无负数根

时间:2019-04-10 10:47:11

标签: python

我的python总是给我这个错误。

没有负数的根,因为里面的所有位都是平方的..help!

    elif coordNumber == "4":
        fourInputXOne = int(input ("Please enter the x value of the first co-ordinate "))
        fourInputYOne = int(input ("Please enter the y value of the first co-ordinate "))
        print ("")
        fourInputXTwo = int(input ("Please enter the x value of the second co-ordinate "))
        fourInputYTwo = int(input ("Please enter the y value of the second co-ordinate "))
        print ("")
        fourInputXThree = int(input ("Please enter the x value of the third co-ordinate "))
        fourInputYThree = int(input ("Please enter the y value of the third co-ordinate "))
        print ("")
        fourInputXFour = int(input ("Please enter the x value of the fourth co-ordinate "))
        fourInputYFour = int(input ("Please enter the y value of the fourth co-ordinate "))
        print ("")

        print ("Here are the co-ordinates you have entered:")
        print ("1: (",fourInputXOne,",",fourInputYOne,")")
        print ("2: (",fourInputXTwo,",",fourInputYTwo,")")
        print ("3: (",fourInputXThree,",",fourInputYThree,")")
        print ("4: (",fourInputXFour,",",fourInputYFour,")")

        sideOneLength = math.sqrt((fourInputXTwo-fourInputXOne)^2 + (fourInputYTwo-fourInputYOne)^2 )
        sideTwoLength = math.sqrt((fourInputXThree-fourInputXTwo)^2 + (fourInputYThree-fourInputYTwo)^2 )
        sideThreeLength = math.sqrt((fourInputXFour-fourInputXThree)^2 + (fourInputYFour-fourInputYThree)^2 )
        sideFourLength = math.sqrt((fourInputXOne-fourInputXFour)^2 + (fourInputYOne-fourInputYFour)^2 )

sidelengths位出错。

2 个答案:

答案 0 :(得分:1)

在python中,您使用a**b平方而不是a^b

插入符号是二进制XOR运算符

elif coordNumber == "4":
    fourInputXOne = int(input ("Please enter the x value of the first co-ordinate "))
    fourInputYOne = int(input ("Please enter the y value of the first co-ordinate "))
    print ("")
    fourInputXTwo = int(input ("Please enter the x value of the second co-ordinate "))
    fourInputYTwo = int(input ("Please enter the y value of the second co-ordinate "))
    print ("")
    fourInputXThree = int(input ("Please enter the x value of the third co-ordinate "))
    fourInputYThree = int(input ("Please enter the y value of the third co-ordinate "))
    print ("")
    fourInputXFour = int(input ("Please enter the x value of the fourth co-ordinate "))
    fourInputYFour = int(input ("Please enter the y value of the fourth co-ordinate "))
    print ("")

    print ("Here are the co-ordinates you have entered:")
    print ("1: (",fourInputXOne,",",fourInputYOne,")")
    print ("2: (",fourInputXTwo,",",fourInputYTwo,")")
    print ("3: (",fourInputXThree,",",fourInputYThree,")")
    print ("4: (",fourInputXFour,",",fourInputYFour,")")

    sideOneLength = math.sqrt((fourInputXTwo-fourInputXOne)**2 + (fourInputYTwo-fourInputYOne)**2 )
    sideTwoLength = math.sqrt((fourInputXThree-fourInputXTwo)**2 + (fourInputYThree-fourInputYTwo)**2 )
    sideThreeLength = math.sqrt((fourInputXFour-fourInputXThree)**2 + (fourInputYFour-fourInputYThree)**2 )
    sideFourLength = math.sqrt((fourInputXOne-fourInputXFour)**2 + (fourInputYOne-fourInputYFour)**2 )

答案 1 :(得分:0)

为什么在逻辑上对您的值进行XOR?

打印有问题的号码:

print( (fourInputXTwo-fourInputXOne)^2 )   # you can see its negative

或进行实验:

print(6^2)       # prints 4  - because 0b110^0b010 == 0b100
print(6**2)      # prints 36 - because 6**2 = 6*6 = 36

将会向您显示您做错了什么。

有关完整文档,请参见binary bitwise operations