Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8683749
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T22:07:33+00:00 2026-06-12T22:07:33+00:00

My original python program used string interpolation to take a number a user enters

  • 0

My original python program used string interpolation to take a number a user enters between 0-9999 like 2928, and summed its individual digits (2+9+2+8=21), and then split the number until eventually it became a single digit number (in this case 3), which was displayed on the screen. The code looks like this:

Now I am needing to figure out a way to do the same without using string interpolation(converting the integers to strings and then splitting the strings, reconverting them to integers, and summing them). I am fairly new to python and therefore can use very simple commands (most complex being the while loop) can anyone help me out/ throw me some ideas?

*ps, I guess some ambiguity exists as to what yearint- year is. “year” is a command that i didnt write above but its code makes the user enter a number between 0-9999. I changed its variable name to “yearint” because I am new to python and want to make things descriptive so I can remember them when I look back. SO basically year/yearint are the input the user inputs.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T22:07:34+00:00Added an answer on June 12, 2026 at 10:07 pm

    I am typing this on a mobile phone, so I will use ” y ” instead of ” yearString “.

    if len(y) == 4:
        x = int(y[3]) + int([2]) + int(y[1]) + int([0])
        y = str(x)
    
    if len(y) == 3:
        x = int([2]) + int(y[1]) + int([0])
        y = str(x)
    
    if len(y) == 2:
        x = int(y[1]) + int([0])
        y = str(x) 
    
    answer = int(y) 
    

    See how, if the sum has multiple digits, a later if statement will handle it. Now let’s write similar code using pure math:

    x = int(yearString) 
    
    d3 = x // 1000
    x %= 1000
    
    d2 = x // 100
    x %= 100
    
    d1 = x // 10
    d0 = x % 10
    
    answer = d3 + d2 + d1 + d0
    

    EDIT: Now that I have thought about it, I think I see the best way for you to do this. I will not write the full code for this.

    If you use the modulus operator by 10 you pull out the bottom digit. Then if you use integer division by 10, you remove the bottom digit. Do this in a while loop and keep doing it until you have all the digits out; the number will be zero after the last divide by 10. Make a function that does this, and call it from a while loop until the sum is less than 10 (is a single digit).

    EDIT: Okay, I guess I might as well write the full code:

    y = int(yearString)
    x = 0  # we will accumulate the sum in x
    while y != 0:
        x += y % 10  # add one digit
        y //= 10  # discard one digit
        if y == 0 and x > 10:
            # We have pulled out all the digits from y, but x has
            # multiple digits.  Start over so we can sum the digits
            # from x.
            y = x
            x = 0
    
    answer = x
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

original column is like: 0.45::rafas::4.0::0.0::0.9 0.35::rasaf::4.0::110.0::1.0 and i would like to break the string
I'm trying to create a word ladder program in python. I'd like to generate
A Python program I'm writing is to read a set number of lines from
[community edit: original title was python conditionals, OP is asking what is wrong with
I need to migrate some C# code to Python. The original code makes use
original string is a,d,k I want to remove all , and make it to
I am trying to port a python program to c#. Here is the line
I'd like to prevent multiple instances of the same long-running python command-line script from
The original question was: Is there a way to declare macros in Python as
I am trying to write a python program using Python 3 I have to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.