Is there a way to limit the number of digits in an integer with Python?

Write a program that prompts the user to enter a two-digit integer .
This was the question and I tried to work it like this unsuccessfully.

def main():
# Prompt user to enter integer
    numb1 = int(input('Enter a two digit integer '))
    
# Verify integer entered is correct before continuing
    if len(numb1)!= 2:
        print(f'Something is wrong with the first input. Try again')
        
# If number entered is correct get a second number
    elif len(numb1) == 2:
            numb2 = int(input('Enter another DIFFERENT two-digit integer '))

A two digit integer can’t be bigger than the biggest two digit integer, right?

1 Like

Yes that’s what I’m trying to do

What is the largest two digit integer?

The largest would be 99. However I tried doing it this way and it was not accepted.
if numb1 > 99 and numb1 < 10:

We’d need to see your full code. There are several ways that you might have made a bug here.

Well yeah, you are literally checking if a number is larger than 99 AND smaller than 10. Which last time I checked, is impossible.

1 Like
def main():
# Prompt user to enter integer
    numb1 = int(input('Enter a two digit integer '))
    
# Verify integer entered is correct before continuing
    if len(numb1)!= 2:
        print(f'Something is wrong with the first input. Try again')
        
# If number entered is correct get a second number
    elif len(numb1) == 2:
            numb2 = int(input('Enter another DIFFERENT two-digit integer '))
# Verify new number fits parameters
            if len(numb2)!= 2:
                print(f'Something is wrong with the second input. Try again')
# Sort and display results
# Use subtraction to see how greater a number is from the other
            else:
                if numb1 > numb2:
                    print(f'{numb1} is larger than {numb2} by {numb1 - numb2}')
                elif numb2 > numb1:
                    print(f'{numb2} is larger than {numb1} by {nuumb2 - numb1}')
main()

I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (’).

1 Like

when I do it this way It displays “Something is wrong with the first input. Try again” whether it is two digits or not.

An int does not have the len property in that way. You can’t treat the int like a string. I’m surprised that code runs without error.

Side note, Python is free; you don’t have to pay extra for longer variable names. Something like firstNumber is easier to read than numb1.

1 Like

since this was the prompt i thought doing it this way would work:
Write a program that prompts the user to enter a two-digit integer . If the user’s input is acceptable, prompt the user to enter another, DIFFERENT two-digit integer

def main():
# Prompt user to enter integer
    numb1 = int(input('Enter a two digit integer '))
    
# Verify integer entered is correct before continuing
    if numb1 > 99 and numb1 < 10:
        print(f'Something is wrong with the first input. Try again')
        
# If number entered is correct get a second number
    else:
        if numb1 < 100 and numb1 > 9:

            numb2 = int(input('Enter another DIFFERENT two-digit integer ')
# Verify new number fits parameters
            if numb2 = numb1 and numb2 >99 and numb2 < 10:
                print(f'Something is wrong with the second input. Try again')
# Sort and display results
# Use subtraction to see how greater a number is from the other
            else:
                if numb1 > numb2:
                    print(f'{numb1} is larger than {numb2} by {numb1 - numb2}')
                elif numb2 > numb1:
                    print(f'{numb2} is larger than {numb1} by {nuumb2 - numb1}')
main()

And this can’t work because a number can’t both be >99 and < 10 at the same time.

1 Like
if numb1 > 99 or numb1 < 10:

thats better

Yeah, though you still have syntax issues to chase down.

okay let me recheck everything, thanks for helping me realize that error

That’s an assignment, not a comparison.

That’s not “numb2”

You just checked that the number is not incorrect, now you are checking if it is correct?

Same issue as we already had.

Redundant check.