Advertisement
Guest User

Untitled

a guest
Feb 11th, 2016
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.87 KB | None | 0 0
  1. # File name: MyEquationCalculator.py
  2. # Author: Daven Lin
  3. # Date created: 1/27/2016
  4. # Date last modified: 1/27/2016
  5. # Python Version: 3.4.3
  6.  
  7. print("\nWelcome to My Equation Calculator")
  8. print("\nThe equation you enter must follow this syntax:"
  9. "\n<operand><space><operator_entered_as_a_string><space><operand>")
  10.  
  11. # Set variables
  12. opList = ["plus", "minus", "times", "divided-by", "equals"]
  13.  
  14. # Read user's equation as a string
  15. equation = input("\nPlease, enter your equation by following the syntax expressed above: ")
  16.  
  17. # define functions
  18. def plus(x,y):
  19. z = x + y
  20. print(z)
  21.  
  22. def minus(x,y):
  23. z = x - y
  24. print(z)
  25.  
  26. def times(x,y):
  27. z = x * y
  28. print(z)
  29.  
  30. def divide(x,y):
  31. x = float(x)
  32. y = float(y)
  33. z = x / y
  34. print(z)
  35.  
  36.  
  37. # Echo to the screen what the user has entered
  38. print('The equation you entered is "%s".' %equation,)
  39.  
  40. # Parse the equation into a list
  41. theParts = equation.split()
  42.  
  43.  
  44. if len(theParts) == 0 :
  45. print("\nHave you simply pressed the Enter key? Please, enter an equation next time! :)")
  46.  
  47. elif len(theParts) == 1 :
  48. print('\nYou have enetered the equation "%s"' %equation, end= " ")
  49. print("which is not a complete and valid equation")
  50. print('since it contains a sequence of letters which are not operands nor operator. Please, try again.')
  51.  
  52. elif len(theParts) == 2 :
  53. print('\nYou have enetered the equation "%s"' %equation, end= " ")
  54. print("which is not a complete and valid equation")
  55. print('since it contains the operator "%s"' %equation, end=" ")
  56. print('Please, try again.')
  57.  
  58. elif len(theParts) == 3 : # Valid equation, compute it and prints its result.
  59.  
  60.  
  61. # For debug purposes - We can index a list just like a string using [index]
  62. print("\nThe equation entered by the user is %s %s %s." %(theParts[0], theParts[1], theParts[2]))
  63.  
  64.  
  65. print("\nBye!")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement