Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # RandoMatho.py
- # Author: Alex Paarfus <rapaarfus139@gmail.com>
- # Date: 2018-08-14
- #
- # Requirements:
- # Import random
- # Math to be built into a function
- # Requires 3 params:
- # $1 == [+,-,/,*]
- # $2 == number
- # $3 == number
- # Returns:
- # $2 $1 $3 == $return
- # Needs to print $return
- # if $1 !allowed; return "unknown"
- # Infinite Loop to:
- # Prompt the user for an operator [+,-,/,*,q]
- # Must accept 'q' to quit
- # num1 and num2 MUST be generated with methods from Random
- # Must be between 0.0 and 10.0
- # Based on example: 1, 1.1, 1.11 are valid
- # import random
- import random
- # Functions
- # mathop -- perform a math operation
- # requires:
- # param1 == operator as String
- # param2 == number
- # param3 == number
- def __mathop(op, a, b):
- # Eval operator is valid
- print("mathop called with the following inputs: ")
- print("operator: \"" + op + "\"")
- print("number 1: \"" + str(a) + "\"")
- print("number 2: \"" + str(b) + "\"")
- if (op == "+"):
- return a + b
- if (op == "-"):
- return a - b
- if (op == "/"):
- return a / b
- if (op == "*"):
- return a * b
- return "unknown"
- # Loop forever, unless told to quit
- while (True):
- print("What operation would you like to do?")
- print("\tAddtion \t+")
- print("\tSubtraction \t-")
- print("\tDivision \t/")
- print("\tMultiplication \t*")
- print("\tQuit \tq")
- strInput = input()
- if not strInput:
- print("Error: no input.")
- print("")
- continue
- if (strInput == "q"):
- break
- # Generate our random numbers
- num1 = random.randint(100,1000) / 100
- num2 = random.randint(100,1000) / 100
- result = __mathop(strInput, num1, num2)
- print ("\n" + str(num1) + " " + strInput + " " + str(num2) + " = " + str(result) + "\n")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement