# Calculate the sum of 2 answers

This is the code for a calculator function. It takes the first and second number, combines them and asks for an input whether the calculation was correct. If the input is "Yes", a statement is printed. If the input is "No", a different statement is printed.

def calculator(number1,number2):
print("The first number is %d." % number1,
"The second number is %d." % number2)
print("The sum is %d." % (number1 + number2))
print("Knew it!")
else:
print("Oh, well. Blame my programmer!")


Is there anything I can do to optimize the code and make it shorter and / or simpler?

• 'If the input is "No", a different statement is printed.' I don't see any mentioning of 'No' in your code. What will happen if I write something except Yes or No? Dec 11, 2017 at 16:32
• Yeah, I've made a mistake. I didn't account for that and simply put wired a response to "Yes" and wired another to everything else. Apologies for that mistake. Dec 11, 2017 at 17:43

Well, this program can be reduced a lot in pythonic way in terms of number for lines for sure!!

def calculator(number1,number2):
try:
print("The first number is {}\nThe second number is {}".format(number1, number2))
print("The sum is {}".format(number1 + number2))
print("Knew it!" if str(input("Am I right? \nANSWER : ")).lower() == "yes" else "Oh, well. Blame my programmer!")
except Exception as e:
print(e)


You have not taken care of handling input types, what if 2 different data types are passed?

• I'm hesitant to dislike this answer, but I disagree 'golfing' is the best solution here. Further, input is always a str object so there's no reason to cast it and catching exceptions here is useless. Dec 11, 2017 at 11:22
• @Coal_ : so there's no reason to cast it - what if user just hits enter without any value? || catching exceptions here is useless - calculator(10, None) would work or you should handle it for sum in 2nd print condition ? :) Dec 11, 2017 at 11:25
• My bad about the exceptions, but I'd only wrap print("The sum is ...") in the statement, and catch TypeError instead of Exception. Pressing return on input just yields an empty string. <edit> You should be printing str(e), not e. </edit> Dec 11, 2017 at 11:29
• +1 to comment but wrapping print("The sum is ...")  with try-except is only good under assumption if input is passed in ASCII. I am assuming you are under assumption that user would always give good input....for non ascii, since encoding is not used, it would break in prints.....just think from bad input perspective and this try except would make sense! :) Dec 12, 2017 at 4:12
• That is not true, print works fine with non-ASCII characters. Dec 12, 2017 at 21:18

You can use .format instead of % to format strings. I also reworded the prompt slightly.

def calculator(number1,number2):
print("Number1 is {} and number2 is {}\n".format(number1,number2) +
"The sum is {}".format(number1+number2))
if input("Is this correct (yes or no): ").strip().lower().startswith("y"):
print("Knew it!")
else:
print("Oh, well. Blame my programmer!")
calculator(5,5)

• str.format is superior to '%' formatting in almost all ways (StackOverflow question). Also, the way OP handles input and print are common to all versions of Python 3, not just 3.6. Dec 11, 2017 at 11:25
• @Coal_ may I ask why str.format() is superior to '%' formatting? I haven't coded long enough to learn why myself. Dec 11, 2017 at 16:20
• str.format automatically finds good string representations for most types without the hassle of using tuples. I suggest you read PEP 3101, especially the rationale will be of interest to you. Dec 11, 2017 at 18:16