Reputation: 19
When I prompt the console to ask the user to input a number for the calculator, I want to check if what the user input is a number. In the first_input() I used a if else condition to check if what the user input was a number. Although if false the function is called again to prompt the user to type a number, it returns none once i try calculating it into my calculator, why is this and how can I properly return the number properly after the user fails to input a number?
# Operations variable
oper = "+-*/"
# Calculates basic operations
def calc(x, op, y):
for i in oper:
if i == str(op):
return eval(str(x) + op + str(y))
# Main function that controls the text-based calculator
def console_calculator():
def first_input():
x = input('Type your first number: ')
if x.isnumeric():
return x
else:
print('Please type in a number')
first_input()
def operation_input():
operat = input('Type one of the following, "+ - * /": ')
return operat
def next_input():
y = input('Type your next number: ')
return y
answer = calc(first_input(), operation_input(), next_input())
print(answer)
console_calculator()
Upvotes: 0
Views: 565
Reputation: 12237
You actually hid the answer in your own question:
how can I properly return the number
You forgot to return
in your else branch:
# Calculates basic operations
def calc(x, op, y):
for i in op:
if i == str(op):
return eval(str(x) + op + str(y))
# Main function that controls the text-based calculator
def console_calculator():
def first_input():
x = input('Type your first number: ')
if x.isnumeric():
return x
else:
print('Please type in a number')
return first_input() # missing return
def operation_input():
operat = input('Type one of the following, "+ - * /": ')
return operat
def next_input():
y = input('Type your next number: ')
return y
answer = calc(first_input(), operation_input(), next_input())
print(answer)
console_calculator()
But you also have a compilation error because you refer to oper
but the parameter is called op
.
This merely answers your question, but doesn't address the architecture (which others will, as they cover better implementations).
Upvotes: 0
Reputation: 27515
I'd suggest a try/except block. This way it will return the integer itself especially since you're already turning them into strings in the eval block.
x = input('Type your first number: ')
try:
return int(x)
except ValueError:
print('Please type in a number')
...
Also to keep asking the user for an integer until they input the correct value I'd use a while loop.
Upvotes: 1
Reputation: 9071
Instead of using two functions for user input you can use single function call it two times. and you also need to add return
before function call in user input.
def console_calculator():
def user_input():
x = input('Type your number: ')
if x.isnumeric():
return x
else:
print('Please type in a number')
return user_input()
def operation_input():
operat = input('Type one of the following, "+ - * /": ')
return operat
answer = calc(user_input(), operation_input(), user_input())
print(answer)
Upvotes: 0