[Python] Keep getting error code - TypeError: unsupported operand type(s) for -: 'int' and...

Discussão em 'Python' iniciado por Stack, Fevereiro 8, 2025.

  1. Stack

    Stack Membro Participativo

    Im trying to make a program that always produces the sum of two three-digit numbers, derived from the difference of two other three-digit numbers, as 1089, provided that the originally chosen number’s first and third digits differ by two or more. I will clarify that i am very new to this and only know the very basics. This is what i have so far.

    num1 = input("Choose a three digit number whose first and third digits vary by at least two numbers.")

    num2 = num1[2] + num1[1] + num1[0]

    if int(num1) < int(num2):

    difference = int(num2) - (num1)
    else:

    difference = int(num1) - (num2)

    newNum1 = str(difference)

    newNum2 = newNum1[2] + newNum1[1] + newNum1[0]

    ten89 = int(newNum1) + int(newNum2)

    print ("If you chose your three-digit number correctly your should have gotten 1089 and you got," ,ten89)


    I keep getting the error code -

    TypeError: unsupported operand type(s) for -: 'int' and 'str' - for the line - difference = int(num2) - (num1).

    I tried changing the int in that line to str, as well as others. If anyone could tell me where I'm going wrong I'd really appreciate it. Thanks!

    Continue reading...

Compartilhe esta Página