4
\$\begingroup\$

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):
    answer = ['Yes', 'yes']
    print("The first number is %d." % number1,
          "The second number is %d." % number2)
    print("The sum is %d." % (number1 + number2))
    if input("Am I right? \nANSWER : ") in answer:
        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?

\$\endgroup\$
2
  • 1
    \$\begingroup\$ '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? \$\endgroup\$
    – Georgy
    Dec 11, 2017 at 16:32
  • \$\begingroup\$ 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. \$\endgroup\$
    – Keanu Tan
    Dec 11, 2017 at 17:43

2 Answers 2

3
\$\begingroup\$

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?

\$\endgroup\$
6
  • \$\begingroup\$ 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. \$\endgroup\$
    – Daniel
    Dec 11, 2017 at 11:22
  • \$\begingroup\$ @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 ? :) \$\endgroup\$
    – NoobEditor
    Dec 11, 2017 at 11:25
  • 2
    \$\begingroup\$ 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> \$\endgroup\$
    – Daniel
    Dec 11, 2017 at 11:29
  • \$\begingroup\$ +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! :) \$\endgroup\$
    – NoobEditor
    Dec 12, 2017 at 4:12
  • \$\begingroup\$ That is not true, print works fine with non-ASCII characters. \$\endgroup\$
    – Daniel
    Dec 12, 2017 at 21:18
1
\$\begingroup\$

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)
\$\endgroup\$
3
  • 1
    \$\begingroup\$ 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. \$\endgroup\$
    – Daniel
    Dec 11, 2017 at 11:25
  • \$\begingroup\$ @Coal_ may I ask why str.format() is superior to '%' formatting? I haven't coded long enough to learn why myself. \$\endgroup\$
    – Keanu Tan
    Dec 11, 2017 at 16:20
  • \$\begingroup\$ 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. \$\endgroup\$
    – Daniel
    Dec 11, 2017 at 18:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.