I'm new to programming and have a Python-question!
What I want to do is:
Let the user type in a number (for ex 4512)
Sort this number, starting with the biggest digit (5421)
Sort the same number but starting with the smallest digit (1245)
Subtract the two numbers (5421-1245)
Print out the result
Here is what I have tried:
print("type in a number")
number = (input())
start_small = "".join(sorted(number))
start_big = "".join(sorted(number, reverse=True))
subtraction = ((start_big)-(start_small))
print(subtraction)
I'm getting the error
TypeError: unsupported operand type(s) for -: 'str' and 'str'
You forgot to convert the numbers to integers before doing arithmetic with them. Change the line where you do the subtraction to
subtraction = int(start_big) - int(start_small)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With