只能调用一个函数的return,而不调用里面的其他指令吗?
Can only the return of a function be called instead of other instructions in it?
我正在尝试使用以下脚本编写一个简单的计算器:
def math(num_1, num_2):
global operation
if operation == '+':
x = num_1 + num_2
elif operation == '-':
x = num_1 - num_2
elif operation == '*':
x = num_1 * num_2
elif operation == '/':
x = num_1 / num_2
return float(x)
def opp():
print("To add, press '+'")
print("To add, press '-'")
print("To multiply, press '*'")
print("To divide, press '/'")
def inp():
num_1 = input("Enter first number: ")
num_2 = input("Enter second number: ")
return float(num_1), float(num_2)
a, b = inp()
opp()
operation = input()
result = math(a, b)
print("The result is: " + str(result))
它的工作原理是先要求 2 个数字输入,然后再进行操作。
我想让它要求在 2 个数字输入之间进行操作。
为此,我想要以下内容:
def opp():
print("To add, press '+'")
print("To add, press '-'")
print("To multiply, press '*'")
print("To divide, press '/'")
operation = input()
return operation
def inp():
num_1 = input("Enter first number: ")
opp()
num_2 = input("Enter second number: ")
return float(num_1), float(num_2)
然后我想将opp()
的输出输入math()
,但是当我尝试用[=13=替换math()
中的operation
变量时],然后执行整个 opp()
函数,包括它的打印语句。
有没有办法把opp()
的return输入到math()
?
运行 Python 3.8.3 Windows 10
不要使用 global
- 这是在程序内部移动数据的糟糕方法。相反,只需 inp()
return operation
,并将其作为参数传递给 math()
。
def math(operation, num_1, num_2):
if operation == '+':
x = num_1 + num_2
elif operation == '-':
x = num_1 - num_2
elif operation == '*':
x = num_1 * num_2
elif operation == '/':
x = num_1 / num_2
return float(x)
def opp():
print("To add, press '+'")
print("To add, press '-'")
print("To multiply, press '*'")
print("To divide, press '/'")
operation = input()
return operation
def inp():
num_1 = input("Enter first number: ")
operation = opp()
num_2 = input("Enter second number: ")
return operation, float(num_1), float(num_2)
operation, a, b = inp()
result = math(operation, a, b)
print("The result is: " + str(result))
示例:
Enter first number: 2
To add, press '+'
To add, press '-'
To multiply, press '*'
To divide, press '/'
*
Enter second number: 3
The result is: 6.0
我正在尝试使用以下脚本编写一个简单的计算器:
def math(num_1, num_2):
global operation
if operation == '+':
x = num_1 + num_2
elif operation == '-':
x = num_1 - num_2
elif operation == '*':
x = num_1 * num_2
elif operation == '/':
x = num_1 / num_2
return float(x)
def opp():
print("To add, press '+'")
print("To add, press '-'")
print("To multiply, press '*'")
print("To divide, press '/'")
def inp():
num_1 = input("Enter first number: ")
num_2 = input("Enter second number: ")
return float(num_1), float(num_2)
a, b = inp()
opp()
operation = input()
result = math(a, b)
print("The result is: " + str(result))
它的工作原理是先要求 2 个数字输入,然后再进行操作。 我想让它要求在 2 个数字输入之间进行操作。 为此,我想要以下内容:
def opp():
print("To add, press '+'")
print("To add, press '-'")
print("To multiply, press '*'")
print("To divide, press '/'")
operation = input()
return operation
def inp():
num_1 = input("Enter first number: ")
opp()
num_2 = input("Enter second number: ")
return float(num_1), float(num_2)
然后我想将opp()
的输出输入math()
,但是当我尝试用[=13=替换math()
中的operation
变量时],然后执行整个 opp()
函数,包括它的打印语句。
有没有办法把opp()
的return输入到math()
?
运行 Python 3.8.3 Windows 10
不要使用 global
- 这是在程序内部移动数据的糟糕方法。相反,只需 inp()
return operation
,并将其作为参数传递给 math()
。
def math(operation, num_1, num_2):
if operation == '+':
x = num_1 + num_2
elif operation == '-':
x = num_1 - num_2
elif operation == '*':
x = num_1 * num_2
elif operation == '/':
x = num_1 / num_2
return float(x)
def opp():
print("To add, press '+'")
print("To add, press '-'")
print("To multiply, press '*'")
print("To divide, press '/'")
operation = input()
return operation
def inp():
num_1 = input("Enter first number: ")
operation = opp()
num_2 = input("Enter second number: ")
return operation, float(num_1), float(num_2)
operation, a, b = inp()
result = math(operation, a, b)
print("The result is: " + str(result))
示例:
Enter first number: 2
To add, press '+'
To add, press '-'
To multiply, press '*'
To divide, press '/'
*
Enter second number: 3
The result is: 6.0