如何检查我输入到基于文本的计算器中的内容是否是数字?
How can i check if what I input into my text based calculator is an number?
当我提示控制台要求用户为计算器输入数字时,我想检查用户输入的是不是数字。在 first_input() 中,我使用了 if else 条件来检查用户输入的内容是否为数字。尽管如果为 false,该函数会再次调用以提示用户输入数字,但一旦我尝试将其计算到我的计算器中,它 returns none,为什么会这样,我该如何正确 return 用户输入数字失败后的正确数字?
# 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()
您可以使用单个函数调用两次,而不是使用两个函数进行用户输入。并且您还需要在用户输入的函数调用之前添加 return
。
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)
我建议 try/except 块。这样它将 return 整数本身,特别是因为您已经在 eval 块中将它们转换为字符串。
x = input('Type your first number: ')
try:
return int(x)
except ValueError:
print('Please type in a number')
...
此外,为了不断询问用户输入整数,直到他们输入正确的值,我会使用 while 循环。
您实际上将答案隐藏在自己的问题中:
how can I properly return the number
您忘记在您的 else 分支中 return
:
# 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()
但是你也有编译错误,因为你引用了oper
但是参数调用的是op
.
这仅回答了您的问题,但并未解决架构问题(其他人会解决,因为它们涵盖了更好的实现)。
当我提示控制台要求用户为计算器输入数字时,我想检查用户输入的是不是数字。在 first_input() 中,我使用了 if else 条件来检查用户输入的内容是否为数字。尽管如果为 false,该函数会再次调用以提示用户输入数字,但一旦我尝试将其计算到我的计算器中,它 returns none,为什么会这样,我该如何正确 return 用户输入数字失败后的正确数字?
# 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()
您可以使用单个函数调用两次,而不是使用两个函数进行用户输入。并且您还需要在用户输入的函数调用之前添加 return
。
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)
我建议 try/except 块。这样它将 return 整数本身,特别是因为您已经在 eval 块中将它们转换为字符串。
x = input('Type your first number: ')
try:
return int(x)
except ValueError:
print('Please type in a number')
...
此外,为了不断询问用户输入整数,直到他们输入正确的值,我会使用 while 循环。
您实际上将答案隐藏在自己的问题中:
how can I properly return the number
您忘记在您的 else 分支中 return
:
# 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()
但是你也有编译错误,因为你引用了oper
但是参数调用的是op
.
这仅回答了您的问题,但并未解决架构问题(其他人会解决,因为它们涵盖了更好的实现)。