Python Forum
Python Calculator Application
Thread Rating:
  • 2 Vote(s) - 3 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Python Calculator Application
#2
(Jul-21-2017, 04:23 PM)MemeStealer Wrote: Is there anything that I could improve on in terms of simplicity or better ways of getting the job done?
Yes Cool
The effort is good,but there are really big problem is your design of this.
I will not at all try to fix your code,but give a example with design in focus.
It's scary that you call the main() function 14 times Dodgy

Some point i name the function menu() not main(),
a lot people use main() but name no sense because it can be everything.
menu() is just called 1 time,after calculate always fall back into menu() again.
There can make new calculation or Quit out.
from operator import add, sub, mul

def calc():
   op = input("Enter operator +-*: ")
   n1 = int(input('Fist number: '))
   n2 = int(input('Second number: '))
   operators = {'+': add(n1, n2), '-': sub(n1, n2), '*': mul(n1, n2)}
   if op in operators:
       print('{} {} {} = {}'.format(n1, op, n2, operators[op]))
   input('Press enter to return menu\n')

def menu():
   while True:
       print('(1) Calculate 2 numbers')
       print('(Q) Quit')
       choice = input('Enter your choice: ').lower()
       if choice == '1':
           calc()
       elif choice == 'q':
           return False
       else:
           print('Not a correct choice: <{}>,try again'.format(choice))

if __name__ == '__main__':
   menu()
A run:
Output:
C:\1_py\div λ python calc.py (1) Calculate 2 numbers (Q) Quit Enter your choice: aaaa Not a correct choice: <aaaa>,try again (1) Calculate 2 numbers (Q) Quit Enter your choice: 1 Enter operator +-*: * Fist number: 6 Second number: 7 6 * 7 = 42 Press enter to return menu (1) Calculate 2 numbers (Q) Quit Enter your choice: q C:\1_py\div λ
Reply


Messages In This Thread
Python Calculator Application - by MemeStealer - Jul-21-2017, 04:23 PM
RE: Python Calculator Application - by snippsat - Jul-21-2017, 06:03 PM
RE: Python Calculator Application - by MemeStealer - Jul-21-2017, 07:08 PM
RE: Python Calculator Application - by MemeStealer - Jul-21-2017, 07:12 PM
RE: Python Calculator Application - by nilamo - Jul-21-2017, 06:36 PM
RE: Python Calculator Application - by nilamo - Jul-21-2017, 07:08 PM
RE: Python Calculator Application - by MemeStealer - Jul-21-2017, 08:42 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Need some help trying to get my Python mortgage amortization calculator to work prope IamSirAskAlot 4 17,269 Feb-12-2024, 10:53 PM
Last Post: BerniceBerger
  Python running only in application Mawixy 2 1,781 Apr-19-2022, 11:38 AM
Last Post: Mawixy
  New to python, trying to make a basic calculator AthertonH 2 1,835 Apr-14-2022, 03:33 PM
Last Post: AthertonH
  How To Create A "Birthday Calculator" in Python? unigueco9 3 6,461 Oct-11-2021, 08:03 PM
Last Post: SamHobbs
  How to send data from a python application to an external application aditya_rajiv 1 2,981 Jul-26-2021, 06:00 AM
Last Post: ndc85430
  python calculator only using decomposing functions kirt6405 1 2,374 Jun-19-2021, 12:52 AM
Last Post: bowlofred
  python application and credentials safety concern aster 4 4,552 Mar-06-2021, 06:51 PM
Last Post: snippsat
  Keep Application running after Python script ends PEGylated_User 0 2,633 Nov-12-2020, 03:27 PM
Last Post: PEGylated_User
  Installing Python Application pplgf 3 3,373 Apr-27-2020, 10:51 PM
Last Post: Larz60+
  loop in pyautogui (python automation GUI application) pyprogrammer 0 5,505 Feb-12-2020, 02:52 PM
Last Post: pyprogrammer

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020