I want to take the answer that was evaluated after using the calculator to perform more operations to it, but when I try using the answer variable into the calc()
function it returns this error, TypeError: 'in <string>' requires string as left operand, not function
.
How can I have it where I can constantly take the answer and keep performing more operations with it? Also, I am having trouble figuring out how I can constantly use this calculator instead of the script just finishing after one calculation. What would be the best way to keep it running until I don't want it to otherwise?
# Calculates basic operations
def calc(x, op, y):
if op in "+-*/":
ans = eval(str(x) + op + str(y))
return ans
# Main function that controls the text-based calculator
def console_calculator():
def user_input():
while True:
x = input('Type your first number: ')
try:
return int(x)
except ValueError:
try:
return float(x)
except ValueError:
print('Please type in a number...')
def operation_input():
while True:
operation = input('Type one of the following, "+ - * /": ')
if operation in "+-*/":
return operation
else:
print('Please type one of the following, "+ - * /"...')
answer = calc(user_input(), operation_input(), user_input())
print(answer)
print(calc(str(answer), operation_input, user_input)) # This line of code throws the error
console_calculator()
when I try using the answer variable into the calc() function it returns this error, "TypeError: 'in ' requires string as left operand, not function".
When you pass a function without parenthesis like argument, you are passing the function object and not the value returned by the the function.
print(calc(str(answer), operation_input(), user_input()))