Skip to content

Instantly share code, notes, and snippets.

@towkir
Last active October 28, 2016 16:35
Show Gist options
  • Save towkir/754d212e4f83dc00a0c1ee5f293f2113 to your computer and use it in GitHub Desktop.
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)
#!/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