Advertisement
Stewie410

RandoMatho

Aug 14th, 2018
317
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.96 KB | None | 0 0
  1. # RandoMatho.py
  2. # Author:           Alex Paarfus <rapaarfus139@gmail.com>
  3. # Date:             2018-08-14
  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. # Functions
  27. # mathop -- perform a math operation
  28. #   requires:
  29. #       param1 == operator as String
  30. #       param2 == number
  31. #       param3 == number
  32. def __mathop(op, a, b):
  33.     # Eval operator is valid
  34.     print("mathop called with the following inputs: ")
  35.     print("operator: \"" + op + "\"")
  36.     print("number 1: \"" + str(a) + "\"")
  37.     print("number 2: \"" + str(b) + "\"")
  38.     if (op == "+"):
  39.         return a + b
  40.     if (op == "-"):
  41.         return a - b
  42.     if (op == "/"):
  43.         return a / b
  44.     if (op == "*"):
  45.         return a * b
  46.     return "unknown"
  47.  
  48. # Loop forever, unless told to quit
  49. while (True):
  50.     print("What operation would you like to do?")
  51.     print("\tAddtion        \t+")
  52.     print("\tSubtraction    \t-")
  53.     print("\tDivision       \t/")
  54.     print("\tMultiplication \t*")
  55.     print("\tQuit           \tq")
  56.     strInput = input()
  57.     if not strInput:
  58.         print("Error: no input.")
  59.         print("")
  60.         continue
  61.     if (strInput == "q"):
  62.         break
  63.     # Generate our random numbers
  64.     num1 = random.randint(100,1000) / 100
  65.     num2 = random.randint(100,1000) / 100
  66.     result = __mathop(strInput, num1, num2)
  67.     print ("\n" + str(num1) + " " + strInput + " " + str(num2) + " = " + str(result) + "\n")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement