2014-11-11 3 views
0

그래서 여기에 내 상황이 있습니다. 필자는 파이썬 3.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) 

하지만이 엉망와 저와 오류를 제공 계산시

오류 -

if data[n1] < 0 or data[n1] > 0: 
    TypeError: unorderable types: str() < int() 

사람이 내가 뭐하는 거지 볼 수있다 여기서 뭐라구? 감사합니다.

+0

"검사 아래 #The 코드가 숫자 인 경우" 는이 문제를 해결하려면 정수에 문자열을 변환 할 INT()를 호출 그것을 제외하고. –

+0

정말요? 좋아, 그렇지 않다면 문제를 해결하는 방법에 대해 알고 싶습니까? –

답변

1

문자열과 정수를 비교할 때이 문제가 발생합니다. 파이썬이 추측하지 못하면 오류가 발생합니다.

int(input(...)) 

그래서, 수정 문이 있어야한다 :

if int(data[n1]) < 0 or int(data[n1]) > 0: 
+0

아니요,이 없습니다. 위의 루프를 읽으십시오. –

관련 문제