Python 3.4 Python:一个类型错误

Python 3.4 Python:一个类型错误,python-3.4,Python 3.4,这就是我的情况。我一直在尝试用Python3.4制作一个高级计算器,您可以在其中键入类似的内容1+1',然后它会给你“2”的答案。现在我将解释我的计算器应该如何工作。首先输入一个数学等式,然后根据空格计算输入的单词数。它这样做是为了知道未来的循环需要多长时间。然后它会将您输入的所有内容拆分。它将其拆分为str和int,但它们仍然在同一个变量中,并且它们仍然是有序的。我遇到的问题是,它实际上是用来计算的 这是我所有的代码- # This is the part were they ente

这就是我的情况。我一直在尝试用Python3.4制作一个高级计算器,您可以在其中键入类似的内容1+1',然后它会给你“2”的答案。现在我将解释我的计算器应该如何工作。首先输入一个数学等式,然后根据空格计算输入的单词数。它这样做是为了知道未来的循环需要多长时间。然后它会将您输入的所有内容拆分。它将其拆分为str和int,但它们仍然在同一个变量中,并且它们仍然是有序的。我遇到的问题是,它实际上是用来计算的

这是我所有的代码-

    # This is the part were they enter the maths equation
    print("-------------------------")
    print("Enter the maths equation")
    user_input = input("Equation: ")
    # This is were it counts all of the words
    data_count = user_input.split(" ")
    count = data_count.__len__()
    # Here is were is splits it into str's and int's
    n1 = 0
    data = []
    if n1 <= count:
        for x in user_input.split():
            try:
                data.append(int(x))
            except ValueError:
                data.append(x)
            n1 += 1
    # And this is were it actually calculates everything
    number1 = 0
    number2 = 0
    n1 = 0
    x = 0
    answer = 0
    while n1 <= count:
        #The code below checks if it is a number
        if data[n1] < 0 or data[n1] > 0:
            if x == 0:
                number1 = data[n1]
            elif x == 1:
                number2 = data[n1]
        elif data[n1] is "+":
            if x == 0:
                answer += number1
            elif x == 1:
                answer += number2
        n1 += 1
        x += 1
        if x > 1:
            x = 0
    print("Answer =", answer)
#这是他们输入数学方程式的部分
打印(“---------------------------”)
打印(“输入数学公式”)
用户输入=输入(“公式:”)
#这是一个很重要的问题
数据计数=用户输入。拆分(“”)
计数=数据计数
#这是将其拆分为str和int
n1=0
数据=[]
如果n1 1:
x=0
打印(“Answer=”,Answer)
但是在计算过程中,它把我搞砸了,给我带来了错误

错误-

    if data[n1] < 0 or data[n1] > 0:
    TypeError: unorderable types: str() < int()
如果数据[n1]<0或数据[n1]>0:
TypeError:无序类型:str()
有人能看出我做错了什么吗?
谢谢

当您比较字符串和整数时,会出现这个问题。 Python不会猜测,它会抛出一个错误。 要解决此问题,只需调用int()将字符串转换为整数:

int(input(...))
因此,更正后的声明应为:

if int(data[n1]) < 0 or int(data[n1]) > 0:
如果int(数据[n1])<0或int(数据[n1])>0:

“#下面的代码检查它是否是一个数字”,但它不是?好的,如果没有,那么你知道如何修复它吗?不,不是这个。阅读上面的循环。