0

Write a python program to ask a user to input a 6-digit integer and check if the input is a palindrome or not. If the user fails to enter an integer or if the integer is less than 6-digit, the user must be asked to input it again. (NOTE: The code should be written without using TRY and EXCEPT and the user should not be allowed to take more than 3 attempts.)

My take on this is:

for n in range(3):
    while True:
        i = input("Please enter a six digit integer:")
        if i.isnumeric():
            if len(i)==6:
                i_integer = int(i)
                print("Your number is:",i_integer,"and the data type is:",type(i_integer))
        break
    else:
        print("Enter value is not numeric") 

With this code, if I enter a six-digit number, then I have to enter it for 3 times, instead of one time. Below is the output.

Please enter a six digit integer:123456
Your number is: 123456 and the data type is: <class 'int'>
Please enter a six digit integer:123456
Your number is: 123456 and the data type is: <class 'int'>
Please enter a six digit integer:123456
Your number is: 123456 and the data type is: <class 'int'>

Is there any better way to do this without using TRY and EXCEPT?

Atif
  • 85
  • 7
  • Does this answer your question? [Asking the user for input until they give a valid response](https://stackoverflow.com/questions/23294658/asking-the-user-for-input-until-they-give-a-valid-response) – G. Anderson Sep 16 '22 at 23:48
  • It's not clear why you included `for n in range(3):` in your code if you don't want the user to enter three numbers, can you clarify that? – G. Anderson Sep 16 '22 at 23:49
  • Because the user is not allowed to take more than 3 attempts. – Atif Sep 16 '22 at 23:55
  • But surely the user is allowed to use *less* than 3 attempts, how does your `for` loop handle that situation? – Mark Ransom Sep 17 '22 at 02:39

2 Answers2

0

Instead of a "for" loop, you could just use a counter and "while" loop like the following code snippet.

tries = 0

while True:
    i = input("Please enter a six digit integer:")
    if i.isnumeric() and len(i) == 6:
        i_integer = int(i)
        print("Your number is:",i_integer,"and the data type is:",type(i_integer))
        break
    else:
        tries += 1
        if tries >= 3:
            break
            
if tries >= 3:
    print("Sorry - too many tries to enter a six digit integer")
    quit()
        
# Palindrome testing would start here
print("Going to see if this is a palindrome")

Testing this out, the following output was displayed on my terminal.

@Una:~/Python_Programs/Palindrome$ python3 Numeric.py 
Please enter a six digit integer:12345
Please enter a six digit integer:123456
Your number is: 123456 and the data type is: <class 'int'>
Going to see if this is a palindrome

I will leave it to you to build the test to see whether or not the entered number is a palindrome.

Give that a try.

NoDakker
  • 3,390
  • 1
  • 10
  • 11
  • Thanks your code worked well. For palindrome add this after the first print statement: ``` print("Going to see if this is a palindrome") reverse = i[::-1] if i == reverse: print('Yes! Number entered is a Palindrome') else: print("No! Number entered is not Palindrome") ``` – Atif Sep 17 '22 at 06:59
0
tries = 0

while True:
    i = input("Please enter a six digit integer:")
    if i.isnumeric() and len(i) == 6:
        i_integer = int(i)
        print("Your number is:",i_integer,"and the data type is:",type(i_integer))

        # Palindrome testing would start here
        print("Going to see if this is a palindrome")

        n=int(i)
        temp=n
        rev=0
        while(n>0):
            dig=n%10
            rev=rev*10+dig
            n=n//10
        if(temp==rev):
            print("The number is a palindrome!")
        else:
            print("The number isn't a palindrome!")        
        break

    else:
        tries += 1
        if tries >= 3:
            break

if tries >= 3:
    print("Sorry - too many tries to enter a six digit integer")
Diaa
  • 1
  • 1
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Sep 22 '22 at 00:53