Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # File name: MyEquationCalculator.py
- # Author: Daven Lin
- # Date created: 1/27/2016
- # Date last modified: 1/27/2016
- # Python Version: 3.4.3
- print("\nWelcome to My Equation Calculator")
- print("\nThe equation you enter must follow this syntax:"
- "\n<operand><space><operator_entered_as_a_string><space><operand>")
- # Set variables
- opList = ["plus", "minus", "times", "divided-by", "equals"]
- # Read user's equation as a string
- equation = input("\nPlease, enter your equation by following the syntax expressed above: ")
- # define functions
- def plus(x,y):
- z = x + y
- print(z)
- def minus(x,y):
- z = x - y
- print(z)
- def times(x,y):
- z = x * y
- print(z)
- def divide(x,y):
- x = float(x)
- y = float(y)
- z = x / y
- print(z)
- # Echo to the screen what the user has entered
- print('The equation you entered is "%s".' %equation,)
- # Parse the equation into a list
- theParts = equation.split()
- if len(theParts) == 0 :
- print("\nHave you simply pressed the Enter key? Please, enter an equation next time! :)")
- elif len(theParts) == 1 :
- print('\nYou have enetered the equation "%s"' %equation, end= " ")
- print("which is not a complete and valid equation")
- print('since it contains a sequence of letters which are not operands nor operator. Please, try again.')
- elif len(theParts) == 2 :
- print('\nYou have enetered the equation "%s"' %equation, end= " ")
- print("which is not a complete and valid equation")
- print('since it contains the operator "%s"' %equation, end=" ")
- print('Please, try again.')
- elif len(theParts) == 3 : # Valid equation, compute it and prints its result.
- # For debug purposes - We can index a list just like a string using [index]
- print("\nThe equation entered by the user is %s %s %s." %(theParts[0], theParts[1], theParts[2]))
- print("\nBye!")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement