Reputation: 41
I'm trying to create a program that uses random floats to perform an operation selected by the user. In the current set up, the program goes straight to printing "Quitting. Thank you for using my program!" regardless of which operation I choose.
print ( "This program performs mathematical functions with random numbers." )
op = input("Choose a math operation (Please type +, -, *, /, % to select an operation, or type q to quit ): ")
import random
random.randint(1,10)
random.random()*10
while (op != "q"):
if (op == "+"):
def add2(a,b):
print ("mathop called with",a,"and",b)
c=a+b
return c
elif (op == "-"):
def subtract2(a,b):
print ("mathop called with",a,"and",b)
c=a-b
return c
elif (op == "*"):
def multiply2(a,b):
print ("mathop called with",a,"and",b)
c=a+b
return c
elif (op == "/"):
def divide2(a,b):
print ("mathop called with",a,"and",b)
c=a/b
return c
elif (op == "%"):
def remainder2(a,b):
print ("mathop called with",a,"and",b)
c=a%b
return c
else:
print ("Quitting. Thank you for using my program!")
op = input("Choose a math operation (Please type +, -, *, /, % to select an operation, or type q to quit ): ")
Upvotes: 0
Views: 82
Reputation: 443
you are defining your functions in your if else statements but never calling them, I switched your code around a bit and it seems to be working now.
import random
def add2(a,b):
print("mathop called with",a,"and",b)
c=a+b
return c
def subtract2(a,b):
print("mathop called with",a,"and",b)
c=a-b
return c
def multiply2(a,b):
print("mathop called with",a,"and",b)
c=a*b
return c
def divide2(a,b):
print("mathop called with",a,"and",b)
c=a/b
return c
def remainder2(a,b):
print("mathop called with",a,"and",b)
c=a%b
return c
print("This program performs mathematical functions with random numbers.")
op = input("Choose a math operation (Please type +, -, *, /, % to select an operation, or type q to quit ): ")
a = random.randint(1,10)
b = random.random()*10
while (op != "q"):
if (op == "+"):
print(add2(a,b))
elif (op == "-"):
print(subtract2(a,b))
elif (op == "*"):
print(multiply2(a,b))
elif (op == "/"):
print(divide2(a,b))
elif (op == "%"):
print(remainder2(a,b))
else:
print("Quitting. Thank you for using my program!")
op = input("Choose a math operation (Please type +, -, *, /, % to select an operation, or type q to quit ): ")
Upvotes: 2
Reputation: 513
You should call the function to using it. Try this:
import random
def add2(a,b):
print ("mathop called with",a,"and",b)
c=a+b
return c
def subtract2(a,b):
print ("mathop called with",a,"and",b)
c=a-b
return c
def multiply2(a,b):
print ("mathop called with",a,"and",b)
c=a*b
return c
def divide2(a,b):
print ("mathop called with",a,"and",b)
c=a/b
return c
def remainder2(a,b):
print ("mathop called with",a,"and",b)
c=a%b
return c
print ( "This program performs mathematical functions with random numbers." )
p = random.randint(1,10)
q = random.random()*10
while True:
op = input("Choose a math operation (Please type +, -, *, /, % to select an operation, or type q to quit ): ")
if (op == "+"):
print(add2(p, q))
elif (op == "-"):
print(subtract2(p, q))
elif (op == "*"):
print(multiply2(p,q))
elif (op == "/"):
print(divide2(p, q))
elif (op == "%"):
print(remainder2(p, q))
elif (op == 'q'):
print ("Quitting. Thank you for using my program!")
break
Upvotes: 0
Reputation: 1039
There are several issues with this code.
Firstly, you do not call any of your functions. Instead, you simply defined them. For example, if you enter '+' then you will enter your if condition where op == '+'
. However, nothing gets executed since you are not actually calling the function add2(a,b)
. You simply defined it.
Secondly, you do not save generated random values into the variables. Instead of:
random.randint(1,10)
random.random()*10
You could probably write something like this:
a = random.randint(1, 10)
b = random.random() * 10
Thirdly, there is no need to use brackets when defining if conditions.
After slight modification you could probably write something like the following code:
import random
def add2(a, b):
print("mathop called with", a, "and", b)
c = a + b
return c
def subtract2(a, b):
print("mathop called with", a, "and", b)
c = a - b
return c
def multiply2(a, b):
print("mathop called with", a, "and", b)
c = a + b
return c
def divide2(a, b):
print("mathop called with", a, "and", b)
c = a / b
return c
def remainder2(a, b):
print("mathop called with", a, "and", b)
c = a % b
return c
op = None
while op != "q":
op = input("Choose a math operation (Please type +, -, *, /, % to select an operation, or type q to quit ): ")
a = random.randint(1, 10)
b = random.random() * 10
if op == "+":
print(add2(a, b))
elif op == "-":
print(add2(a, b))
elif op == "*":
print(add2(a, b))
elif op == "/":
print(add2(a, b))
elif op == "%":
print(add2(a, b))
else:
print("Quitting. Thank you for using my program!")
The given code can further be improved by eliminating if conditions and replacing with a dictionary. Something like this would work:
import random
def add2(a, b):
print("mathop called with", a, "and", b)
c = a + b
return c
def subtract2(a, b):
print("mathop called with", a, "and", b)
c = a - b
return c
def multiply2(a, b):
print("mathop called with", a, "and", b)
c = a + b
return c
def divide2(a, b):
print("mathop called with", a, "and", b)
c = a / b
return c
def remainder2(a, b):
print("mathop called with", a, "and", b)
c = a % b
return c
op = None
operations = {'+': add2, '-': subtract2, '*': multiply2, '/': divide2, '%': remainder2}
while op != "q":
op = input("Choose a math operation (Please type +, -, *, /, % to select an operation, or type q to quit ): ")
a = random.randint(1, 10)
b = random.random() * 10
if op in operations:
operations[op](a, b)
print("Quitting. Thank you for using my program!")
You can further improve the code above by exploring some other tricks you can do in python.
Upvotes: 0