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 7496667
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T18:43:35+00:00 2026-05-29T18:43:35+00:00

I am trying to create a simple program that reads a string of 4

  • 0

I am trying to create a simple program that reads a string of 4 digits, makes sure it is indeed 4 digits, makes sure there are no non-digits, then separates the first two digits from the last two and adds them together. I can make it all work but I still got this error:

ValueError: invalid literal for int() with base 10:

This only happens when I try a string such as ‘456f’.

What can I change to fix this?

Code:

    s = input('please type a 4-digit integer \n')
valid = True
for c in s:
    if len(s)!= 4:
        valid = False
    if not c.isdigit():
        print (c, 'is not a valid input')
number = int(s)
firstOne = number // 100
secondOne = number % 100
sum = firstOne + secondOne
x = '/'
if valid == True:
    print('your integer is ' + str(number), x, 'first two digits are ' + str(firstOne), x, 'second two digits are ' + str(secondOne), x, 'sum of two new numbers is ' + str(sum))
else:
    print(len(s), 'is an invalid amount of digits')
  • 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-05-29T18:43:38+00:00Added an answer on May 29, 2026 at 6:43 pm

    Let’s focus on this code:

    for c in s:
        if len(s)!= 4:
            valid = False
        if not c.isdigit():
            print (c, 'is not a valid input')
    number = int(s)
    

    The first thing to say is that the len() check should be moved outside the character loop.

    if len(s)!= 4:
        valid = False
    for c in s:
        ...
    

    The next comment to make is that whilst you are detecting non-digits, you continue executing code as if nothing is wrong. You presumably intend to set valid to False.

    if not c.isdigit():
        print (c, 'is not a valid input')
        valid = False
    

    Now, the main part of the problem. You need to skip the conversion to int when invalid input is detected.

    if valid:
        number = int(s)
        ...
    

    If you want to continue with such an approach your code would look like this:

    valid = True
    s = input('please type a 4-digit integer \n')
    if len(s)!= 4:
        valid = False
        print(len(s), 'is an invalid amount of digits')
    
    if valid:
        for c in s:
            if not c.isdigit():
                valid = False
                print (c, 'is not a valid input')
    
    if valid:
        number = int(s)
        firstOne = number // 100
        secondOne = number % 100
        sum = firstOne + secondOne
        x = '/'
        print('your integer is ' + str(number), x, 'first two digits are ' + str(firstOne), x, 'second two digits are ' + str(secondOne), x, 'sum of two new numbers is ' + str(sum))
    

    Having said all of that, I’d probably reorganise the code quite a bit to deal with the errors as soon as they are detected. Code is much easier to understand if you can organise your error handling that way.

    s = input('please type a 4-digit integer \n')
    if len(s)!= 4:
        sys.exit(str(len(s)) + ' is an invalid amount of digits')
    for c in s:
        if not c.isdigit():
            sys.exit(c + ' is not a valid input')
    number = int(s)
    firstOne = number // 100
    secondOne = number % 100
    sum = firstOne + secondOne
    x = '/'
    print('your integer is ' + str(number), x, 'first two digits are ' + str(firstOne), x, 'second two digits are ' + str(secondOne), x, 'sum of two new numbers is ' + str(sum))
    

    Now, that’s a start in the right direction, but you can continue in this vein making the code better and better. Sven’s answer gives you an excellent illustration of where such a process would ultimately lead.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to create a simple phonebook program that reads data from a
I am trying to create a program that will do some simple calculations, but
I'm trying to create a client-server program that send and receive a string (its
I'm trying to create a program that reads in a .txt file with multiple
I was trying to create simple program that would perform some trivial operation with
I am trying to create a program that reads multiple choice answers from a
I'm trying to create a simple program or function that will take the users
I'm trying to create a simple program that does the following: A service (NewsService)
i'm trying to create a simple program that will upload a few files for
I have been trying to create a simple program with Python which uses OpenCV

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.