Last active
October 28, 2016 16:35
-
-
Save towkir/754d212e4f83dc00a0c1ee5f293f2113 to your computer and use it in GitHub Desktop.
Takes two numbers as input (abc and xyz) and returns the sum of their reversed form (cba + zyx)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
print("This is a calculator that takes two numbers of same lenght as input, reverses them and returns the sum") | |
print("Press Ctrl+C to exit the calculator") | |
try: | |
while True: | |
print("Enter a number:") | |
first = input() | |
for a in first: | |
try: | |
int(a) | |
except ValueError: | |
print "Please enter only without any other character" | |
break | |
first = [int(i) for i in str(first)] | |
print("Enter anohter number of same length:") | |
second = input() | |
for a in second: | |
try: | |
int(a) | |
except ValueError: | |
print("Please enter only without any other character") | |
break | |
second = [int(i) for i in str(second)] | |
if len(first) == len(second): | |
first.reverse() #reversed the list of numbers | |
first = [str(i) for i in first] #converted all numbers to string | |
first = "".join(first) # joined the reversed string | |
first = int(first) # turned into number for calculation | |
second.reverse() | |
second = [str(i) for i in second] | |
second = "".join(second) | |
second = int(second) | |
result = first + second | |
print("\nHere is your reversed calculation:\n") | |
print("\t\t" + str(first)) | |
print("\t\t" + str(second)) | |
print("---------------------------") | |
print("\t\t" + str(result)) | |
else: | |
print("Please input both the number with same length") | |
except KeyboardInterrupt: | |
print("You have exited the 'reversed calculator'") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment