Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # RandoMathoLimited.py
- # Author: Alex Paarfus <[email protected]>
- # Date: 2018-08-15
- #
- # 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
- #
- # -param1 will always be a string, so we'll determine the operator by doing
- # simple string comparisons
- # -param2 & param3 will always be numbers
- def __mathop(op, a, b):
- # display the parameters passed to method
- print("mathop called: " + op + " " + str(a) + " " + str(b))
- # deterime which operator "op" is
- # if one is found, return a (op) b
- if (op == "+"):
- return a + b
- if (op == "-"):
- return a - b
- if (op == "/"):
- return a / b
- if (op == "*"):
- return a * b
- # default is "unknown"
- return "unknown"
- # Preface statement
- print("=============================================================================")
- print("== This program performs basic mathematic operations on two random numbers ==")
- print("== Both generated numbers will be between 0.0 and 10.0 ==")
- print("== Math Operations: Addition, Subtraction, Multiplication and Division ==")
- print("=============================================================================")
- # infinite loop
- while (True):
- # prompt user for input
- strInput = input("Enter an operator [+,-,/,*] or q to quit: ")
- # if the user enters the letter "q"
- if (strInput == "q"):
- # break out of the loop and, as a result, quit the program
- break
- # Generate our two random numbers
- # To get numbers that look like that in the example, generating based on the range
- # of 100 to 1,000; then dividing that number by 100
- #
- # This way we will always receive numbers that are between 0.0 <= num <= 10.0
- # However, this also ensure that we only have at most two decimal places per number
- # eg: 1, 8.7, 3.14, etc
- num1 = random.randint(100, 1000) / 100
- num2 = random.randint(100, 1000) / 100
- # print the inputs: "num1 strInput num2 = (returnVal)"
- # I've opted to return the method's value directly into the print statement
- # While it doesn't reall matter here; this could potentially save of memory, maybe
- # Its also just a little cleaner than having a third variable that gets defined and
- # used all of twice.
- print(str(num1) + " " + strInput + " " + str(num2) + " = " + str(__mathop(strInput, num1, num2)))
Advertisement
Add Comment
Please, Sign In to add comment