# 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?

Redundant check.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.