Stewie410

RandoMathoLimted

Aug 15th, 2018
239
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.08 KB | None | 0 0
  1. # RandoMathoLimited.py
  2. # Author:           Alex Paarfus <[email protected]>
  3. # Date:             2018-08-15
  4. #
  5. # Requirements:
  6. #   Import random
  7. #   Math to be built into a function
  8. #       Requires 3 params:
  9. #           $1 == [+,-,/,*]
  10. #           $2 == number
  11. #           $3 == number
  12. #       Returns:
  13. #           $2 $1 $3 == $return
  14. #           Needs to print $return
  15. #           if $1 !allowed; return "unknown"
  16. #   Infinite Loop to:
  17. #       Prompt the user for an operator [+,-,/,*,q]
  18. #           Must accept 'q' to quit
  19. #   num1 and num2 MUST be generated with methods from Random
  20. #       Must be between 0.0 and 10.0
  21. #       Based on example: 1, 1.1, 1.11 are valid
  22.  
  23. # import random
  24. import random
  25.  
  26.  
  27. # Functions
  28. # __mathop -- perform a math operation
  29. #   requires:
  30. #       param1 == operator as String
  31. #       param2 == number
  32. #       param3 == number
  33. #
  34. # -param1 will always be a string, so we'll determine the operator by doing
  35. # simple string comparisons
  36. # -param2 & param3 will always be numbers
  37. def __mathop(op, a, b):
  38.     # display the parameters passed to method
  39.     print("mathop called: " + op + " " + str(a) + " " + str(b))
  40.     # deterime which operator "op" is
  41.     # if one is found, return a (op) b
  42.     if (op == "+"):
  43.         return a + b
  44.     if (op == "-"):
  45.         return a - b
  46.     if (op == "/"):
  47.         return a / b
  48.     if (op == "*"):
  49.         return a * b
  50.     # default is "unknown"
  51.     return "unknown"
  52.  
  53. # Preface statement
  54. print("=============================================================================")
  55. print("== This program performs basic mathematic operations on two random numbers ==")
  56. print("== Both generated numbers will be between 0.0 and 10.0                     ==")
  57. print("== Math Operations: Addition, Subtraction, Multiplication and Division     ==")
  58. print("=============================================================================")
  59.  
  60. # infinite loop
  61. while (True):
  62.     # prompt user for input
  63.     strInput = input("Enter an operator [+,-,/,*] or q to quit: ")
  64.     # if the user enters the letter "q"
  65.     if (strInput == "q"):
  66.         # break out of the loop and, as a result, quit the program
  67.         break
  68.     # Generate our two random numbers
  69.     # To get numbers that look like that in the example, generating based on the range
  70.     # of 100 to 1,000; then dividing that number by 100
  71.     #
  72.     # This way we will always receive numbers that are between 0.0 <= num <= 10.0
  73.     # However, this also ensure that we only have at most two decimal places per number
  74.     # eg:       1, 8.7, 3.14, etc
  75.     num1 = random.randint(100, 1000) / 100
  76.     num2 = random.randint(100, 1000) / 100
  77.     # print the inputs: "num1 strInput num2 = (returnVal)"
  78.     # I've opted to return the method's value directly into the print statement
  79.     # While it doesn't reall matter here; this could potentially save of memory, maybe
  80.     # Its also just a little cleaner than having a third variable that gets defined and
  81.     # used all of twice.
  82.     print(str(num1) + " " + strInput + " " + str(num2) + " = " + str(__mathop(strInput, num1, num2)))
Advertisement
Add Comment
Please, Sign In to add comment