Angel  Valenzuela
Angel Valenzuela

Reputation: 377

improvements on code

Enter a number with only two digits: 10

enter another integer: 1

1 is the reverse of: 10

My Code

from pcinput import getInteger


minimum_len = 2
number1 = getInteger("enter a number with only two digits: ")
input_length = int(len(str(number1)))

if input_length < minimum_len: 
    print("needs to be 2 digits" )
    exit(1)
elif not isinstance(number1, int):
    print("needs to be a int")

else: 
     number2 = input("enter another integer: ")

def reverse_int(number1):
    return int(str(number1)[::-1])

if number1 == number2:
    print(str(number1) + " is the reverse of "+ (str(number2 )))
else:
    print(str (number2) + " is not the reverse of " + (str(number1)))

Upvotes: 1

Views: 78

Answers (3)

SpacedMonkey
SpacedMonkey

Reputation: 2783

Instead of turning the number to a string and finding the length of that, you could check the value of the number. 10 is the smallest two digit number and 99 is the largest.

You can similarly avoid strings when reversing the digits:

def reverse(number):
    tens = number // 10
    ones = number % 10
    return 10 * ones + tens

Then all you need to do is reverse one of the numbers:

if number2 == reverse(number1):
    print("{0} is the reverse of {1}".format(number2, number1))

Upvotes: 1

ottomd
ottomd

Reputation: 437

From what I understand if you reverse 10 you will get 01. And in programming 01 is not a number, so the number just equates to 1. If I were you I would also verify the length of the number.

Upvotes: 1

mic4ael
mic4ael

Reputation: 8320

You don't call the reverse_int function anywhere.

- if number1 == number2:
+ if number1 == reverse_int(number2):

Upvotes: 2

Related Questions