Python Forum
Using a For Loop to subtract numbers from an input function.
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Using a For Loop to subtract numbers from an input function.
#1
I am new to programming, and I'm beginning with Python with an online course; progress, so far, has been well, but there is this exercise that I have to do, where the digits will be subtracted from right to left, so, for example, when we input 12345 into the function, the result would be -5. Could anyone help me understand this problem so that I could write a script for it? I have tried to use a modified while loop to understand the problem before attempting to convert it into a for loop, but that also produced a problem for which I do not know the solution. Could anyone please help explain where I've gone wrong and how I can tackle this issue?


inp = int(input('Enter an integer: '))
revInp = str(inp)[::-1]
conInp = int(revInp)
abc = 0

while conInp > 0:
    abc -= conInp % 10
    conInp //= 10
print(abc)
When I wrote this script and wrote 12345, which would then be converted into 54321, the result was -15. I knew this was wrong, since it should output -5. What did I do wrong?
Reply
#2
Your first line is wrong. It does not give you a list of ints.
Reply
#3
You don't have an end case properly. You're subtracting every digit from zero.

You start with zero, then you subtract every digit from it.
Reply
#4
If you print the variables as they change in a while loop or for loop, it is easier to understand what is happening and find the problem, should things not go according to plan!

inp = int(input('Enter an integer: '))
revInp = str(inp)[::-1]
conInp = int(revInp)
abc = 0
 
while conInp > 0:
    abc -= conInp % 10
    conInp //= 10
    print(f'abc = {abc}, conInp = {conInp}')
Gives:

Output:
abc = -1, conInp = 5432 abc = -3, conInp = 543 abc = -6, conInp = 54 abc = -10, conInp = 5 abc = -15, conInp = 0
Is this what you want?

conInp = int(revInp)

while conInp > 5:
    conInp //= 10
    print(f'conInp = {conInp}')
Gives:

Output:
conInp = 5432 conInp = 543 conInp = 54 conInp = 5
Reply
#5
Quote:You don't have an end case properly. You're subtracting every digit from zero.

You start with zero, then you subtract every digit from it.

How could I improve upon it?
Reply
#6
(Sep-22-2025, 12:24 AM)Pedroski55 Wrote: If you print the variables as they change in a while loop or for loop, it is easier to understand what is happening and find the problem, should things not go according to plan!

inp = int(input('Enter an integer: '))
revInp = str(inp)[::-1]
conInp = int(revInp)
abc = 0
 
while conInp > 0:
    abc -= conInp % 10
    conInp //= 10
    print(f'abc = {abc}, conInp = {conInp}')
Gives:

Output:
abc = -1, conInp = 5432 abc = -3, conInp = 543 abc = -6, conInp = 54 abc = -10, conInp = 5 abc = -15, conInp = 0
Is this what you want?

conInp = int(revInp)

while conInp > 5:
    conInp //= 10
    print(f'conInp = {conInp}')
Gives:

Output:
conInp = 5432 conInp = 543 conInp = 54 conInp = 5

No, I apologise, but the exercise/homework I'm doing says that the number will subtract the digits entered, in the same way as entering 123 into the input would produce 6 if it were in addition. And also, it specifies that it will do the calculation from right to left, so 12345 will be 5 - 4 - 3- 2- 1, which is equal to -5, negative five. I tried to do this with a while loop, but the result is wrong there, for which I have no idea why.
Reply
#7
Aha! That's what you want, I didn't understand your goal

rev = '54321'
start = int(rev[0])
for i in range(1, len(rev)):
    start = start - int(rev[i])
    print(start)

num = '12345'
start = int(num[0])
for i in range(1, len(num)):
    start = start + int(num[i])
    print(start)
Reply
#8
(Sep-22-2025, 04:00 AM)Pedroski55 Wrote: Aha! That's what you want, I didn't understand your goal

rev = '54321'
start = int(rev[0])
for i in range(1, len(rev)):
    start = start - int(rev[i])
    print(start)

num = '12345'
start = int(num[0])
for i in range(1, len(num)):
    start = start + int(num[i])
    print(start)

Thank you so much. I apologise, once more, for my lack of skill.
Reply
#9
I think you're starting off wrong converting the input to an int. You can still compute the first digit minus the sum of the following digits, but starting by converting input to an int just makes it more complicated.
def goofy_sum(number):
    total = 0
    while number > 10:
        number, digit = divmod(number, 10)
        total -= digit
    return total + number

print(goofy_sum(int('54321')))
It is easier to treat the input as a string and process the digits one by one.
def goofy_sum(number_str):
    total = int(number_str[0])
    for digit in number_str[1:]:
        total -= int(digit)
    return total

print(goofy_sum('54321'))
Or without a loop.
def goofy_sum(number_str):
    return int(number_str[0]) - sum(map(int, number_str[1:]))

print(goofy_sum('54321'))
Reply
#10
Problems with this code:
inp = int(input('Enter an integer: '))
revInp = str(inp)[::-1]
conInp = int(revInp)
This problem is easier to solve if you leave the input as a string instead of converting to an int. You can solve the problem using divmod or % and // on the int number, but in that case you don't want to reverse then digits in the number. If you did, for some reason, wanted to reverse the digits in the number, that is more easily done like this:
Output:
revInp = int(input('Enter an integer: ')[::-1])
Converting to an int to a string to an int is confusing.

The reason you don't need to reverse the order of the digits is because the method you use to extract the digits is extracting them in reverse order.
54321 % 10 = 1
54321 // 10 = 5432
5432 % 10 = 2
5432 // 10 = 543
...
number % 10 gives you the last digit, so the order the digits are extracted in the loop is the reverse of the order the digits appear in the number.

This is too many times through the loop.
while conInp > 0:
If the entered number is 54321, the values for conInp will be 54321, 5432, 543, 54, 5, 0. When conInp gets to 5, you want to stop the loop and add the value of conInp to abc.

Your code with corrections.
inp = int(input("Enter an integer: "))
abc = 0
while inp >= 10:
    abc -= inp % 10
    inp //= 10
abc += inp
print(abc)
There's a nice function for doing % and // together.
inp = int(input("Enter an integer: "))
abc = 0
while inp >= 10:
    inp, last_digit = divmod(inp, 10)
    abc -= last_digit
abc += inp
print(abc)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Question Two arguments in input function Alfredd 4 810 Nov-09-2025, 12:56 AM
Last Post: Pedroski55
  Input function oldschool 1 1,255 Sep-14-2024, 01:02 PM
Last Post: deanhystad
  difference between forms of input a list to function akbarza 6 3,499 Feb-21-2024, 08:02 PM
Last Post: bterwijn
  subtract 2 datetime string jss 4 5,975 Oct-19-2023, 02:42 PM
Last Post: Larz60+
  WHILE LOOP NOT RETURNING USER INPUT AFTER ZerroDivisionError! HELP! ayodele_martins1 7 3,707 Oct-01-2023, 07:36 PM
Last Post: ayodele_martins1
Information How to take url in telegram bot user input and put it as an argument in a function? askfriends 0 3,131 Dec-25-2022, 03:00 PM
Last Post: askfriends
  Code won't break While loop or go back to the input? MrKnd94 2 2,805 Oct-26-2022, 10:10 AM
Last Post: Larz60+
  ValueError: Found input variables with inconsistent numbers of samples saoko 0 4,070 Jun-16-2022, 06:59 PM
Last Post: saoko
  Showing an empty chart, then input data via function kgall89 0 1,902 Jun-02-2022, 01:53 AM
Last Post: kgall89
  WHILE Loop - constant variables NOT working with user input boundaries C0D3R 4 3,454 Apr-05-2022, 06:18 AM
Last Post: C0D3R

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020