Home > Mobile >  How can I fix this code so that inputs that start and end with 0 will show the zeros
How can I fix this code so that inputs that start and end with 0 will show the zeros

Time:11-10

My code runs fine, for example, if i input 123, i will get 321. However there is a problem when I try the input 01230, it would output 321. I can't seem to figure it out.

edit: it has to be done using integers as data type.

while True:
    reverse=0
    num=str(input("Enter an integer of at least 2 digits or -1 to quit: "))
    if num == str(-1):
        break
    elif len(num)< 2 or len(num)>11:
        print("error")
    else:
        num=int(num)
        while(num>0):
            lastDigit=num
            reverse=(reverse*10) lastDigit
            num=num//10
        print(reverse)

I have tried using if statements to check for the zeros but i felt like that was too inefficient and couldn't figure out a better way.

CodePudding user response:

You could just keep your variables as strings. Following is a snippet of code that tweaks your current program.

while True:
    reverse=0
    num=str(input("Enter an integer of at least 2 digits or -1 to quit: "))
    if num == str(-1):
        break
    elif len(num)< 2 or len(num)>11:
        print("error")
    else:
        reverse = num[::-1]
        print(reverse)

Testing this results in the following terminal output.

@Dev:~/Python_Programs/Palindrome$ python3 Reverse.py 
Enter an integer of at least 2 digits or -1 to quit: 01230
03210
Enter an integer of at least 2 digits or -1 to quit: -1

Give that a try and see if it meets the spirit of your project.

CodePudding user response:

First of all, you don't need to use str() function to convert the value you get from user, because input() function always returns string as return type. See https://docs.python.org/3/library/functions.html#input for more information.

This part will try to convert string num into integer but it will ignore the leading zeros because 0123 does not make any sense as an integer if you think about it.

num=int(num) 

This part also won't work for the numbers that are multiplies of 10, you can either use a condition to check if that is the case and use that information at the end or you can use some other method like adding 1 to number at the beginnig and deal with it at the end (ie. 5670 -> 5671 -> 1765 -> 0765 but again leading zeros does not make any sense if you don't work with strings)

lastDigit=num 
reverse=(reverse*10) lastDigit
num=num//10 

What you can do is, you can count the 0s at the beginning and at the end, use the same logic you already did while ignoring them and simply add when you finish reversing the number. For counting the 0s at the beginning, you should either use string format or input user again until he/she inputs a number that has no leading zeros and for the 0s at the end, use modulo operator.

You can take a look at Display number with leading zeros, this might help. You may also want to add some error handling since you directly convert user input into integer.

  • Related