python - How to subtract the smallest from the largest number until 0 is left - Stack Overflow
Beginner input subtraction help
Python: How to sort a number in two ways and then subtract the numbers - Stack Overflow
python - Subtract Unless Negative Then Return 0 - Stack Overflow
Hey guys, so i'm trying to write a script where you input your age which then is subtracted from a set number (In this case it's 40)
So basically what i'd like to do is write like this
# Question
print('How old are you?:')
age = input()
x = 40
# Result
print('Hello' + ' ' + name + ' ' + 'you retire in' + ' ' + x - age + ' ' + 'years.')
This is not possible since "x" is a set number and "age" is a string which wont subtract.
I've been trying this out with float but haven't got any further than that.
Any help would be appreciated!
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)
Try this
number = input('Please enter a number')
number = sorted(number, reverse=True)
number = ''.join(number)
print(int(number) - int(number[::-1]))
number[::-1] reverses the string, it's a feature of python called slicing, generally the syntax of a slice is
[start:stop:step] so leaving the first two arguments empty and filling -1 as the last, tells us to step through the list by negative 1, which starts from the last element, to the second to the last element whose index is -2 till it gets to the end of the string
iterables can also be sliced so this technique will work on tuples and lists
There are several answers on this question that explain more about slicing Explain Python's slice notation