Jul-21-2017, 04:23 PM
I made a simple calculator application in Python over the course of around an hour and a half today. Is there anything that I could improve on in terms of simplicity or better ways of getting the job done? Thanks! (I started learning Python earlier this week, but I've worked with other languages in the past).
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
# This is a simple calculator application. # Nolan Welch import time #This is needed in order to space out the output so the user isn't overwhelmed. time.sleep( 1 ) print ( "Python Calculator made by Nolan Welch, 7/21/2017.\n\n\n" ) time.sleep( 2.5 ) # Defining the calculator function, which is used to make all the calculations based on user input def calculator(): var1 = input ( "Enter first number: " ) var2 = input ( "Enter second number: " ) if (checkIfInt(var1) and checkIfInt(var2)): var1 = int (var1) var2 = int (var2) if (userInput = = "*" ): print ( "The product of the first and second number is " + str (mul(var1, var2))) elif (userInput = = "/" ): print ( "The first number divided by the second number is " + str (div(var1, var2))) elif (userInput = = "+" ): print ( "The sum of the first and second numbers is " + str (add(var1, var2))) elif (userInput = = "-" ): print ( "The first number minus the second number is " + str (sub(var1, var2))) else : print ( "Invalid input." ) time.sleep( 2 ) main() else : print ( "Both inputs must be integers.\nTaking you back to the calculator..." ) time.sleep( 2 ) calculator() def add(num1, num2): return (num1 + num2) def mul(num1, num2): return (num1 * num2) def div(num1, num2): return (num1 / num2) def sub(num1, num2): return (num1 - num2) # Defining a function that checks whether or not the input is an integer(used in the calculator function) def checkIfInt(possibleInt): try : val = int (possibleInt) return True except ValueError: return False # After the entire program has been run, this function checks whether or not the user would like to use the calculator again def again(): repeat = input ( "Would you like to use the calculator again? " ).lower() if (repeat = = "y" or repeat = = "yes" ): main() elif (repeat = = "n" or repeat = = "no" ): print ( "See you next time!" ) time.sleep( 2 ) quit() else : while ( True ): print ( "Sorry, invalid input." ) time.sleep( 1 ) again() def main(): global userInput userInput = input ( "Which mathematical function would you like to do? Insert *, /, +, or -. Type HELP for help. " ) if (userInput = = "HELP" ): print ( "\n* = Multiplication\n/ = Division\n+ = Addition\n- = Subtraction" ) secondaryInput = input ( "\nType \"BACK\" to go back to the calculator. " ) if (secondaryInput = = "BACK" ): main() else : while ( True ): backInput = input ( "Invalid input. Please type \"BACK\" to return to the calculator. " ) if (backInput = = "BACK" ): main() # Making sure that the user inputs one of the four accepted operators elif (userInput ! = "*" and userInput ! = "/" and userInput ! = "+" and userInput ! = "-" ): print ( "Invalid input!" ) time.sleep( 2 ) main() else : calculator() again() main() """ This entire program seems to be out of order, but I put some functions before others for the sake of simplicity in the final code and in an effort to avoid errors. """ |