如何在 python 中打印函数的结果?
How do you print a function's result in python?
def add(a,b):
return a+b
print(a+b)
def subtract(a,b):
return a-b
print(a-b)
def multiply(a,b):
return a*b
print(a*b)
def divide(a,b)
return a/b
print(a/b)
print('Please select an operation:')
print('1.Add')
print(2.Subtract')
print(3.Multiply')
print(4.Divide')
choice=input('Enter a choice\'1/2/3/4\')
a=int(input('Enter first number')
b=int(input('Enter second number')
if choice=='1':
#these are the sections that arent working
add(a,b)
elif choice=='2':
#when run, it wont print the function even though Ive called it
subtract(a,b)
elif choice=='3':
multiply(a,b)
elif choice=='4':
divide(a,b)
如果有人能提供解决方案,我将不胜感激。谢谢
调换所有函数中 print
和 return
的顺序
def divide(a,b)
print(a/b)
return a/b
函数中的任何内容都不会在 return
之后执行,因此将达到 print
条语句中的 none 条。
函数中 return
语句之后的任何代码都不会 运行。
如果你想让打印成为函数的一部分,你必须这样做:
def add(a,b):
print(a+b)
return a+b
def subtract(a,b):
print(a-b)
return a-b
def multiply(a,b):
print(a*b)
return a*b
def divide(a,b):
print(a/b)
return a/b
但是,更好的方法是 print
主函数中 returns 函数的编号。例如:
def add(a,b):
return a+b
def subtract(a,b):
return a-b
def multiply(a,b):
return a*b
def divide(a,b):
return a/b
print('Please select an operation:')
print('1.Add')
print('2.Subtract')
print('3.Multiply')
print('4.Divide')
choice = input("Enter a choice: '1/2/3/4'")
a = int(input('Enter first number'))
b = int(input('Enter second number'))
if choice == '1':
result = add(a, b)
elif choice == '2':
result = subtract(a, b)
elif choice == '3':
result = multiply(a, b)
elif choice == '4':
result = divide(a, b)
print(result)
def add(a,b):
return a+b
print(a+b)
def subtract(a,b):
return a-b
print(a-b)
def multiply(a,b):
return a*b
print(a*b)
def divide(a,b)
return a/b
print(a/b)
print('Please select an operation:')
print('1.Add')
print(2.Subtract')
print(3.Multiply')
print(4.Divide')
choice=input('Enter a choice\'1/2/3/4\')
a=int(input('Enter first number')
b=int(input('Enter second number')
if choice=='1':
#these are the sections that arent working
add(a,b)
elif choice=='2':
#when run, it wont print the function even though Ive called it
subtract(a,b)
elif choice=='3':
multiply(a,b)
elif choice=='4':
divide(a,b)
如果有人能提供解决方案,我将不胜感激。谢谢
调换所有函数中 print
和 return
的顺序
def divide(a,b)
print(a/b)
return a/b
函数中的任何内容都不会在 return
之后执行,因此将达到 print
条语句中的 none 条。
函数中 return
语句之后的任何代码都不会 运行。
如果你想让打印成为函数的一部分,你必须这样做:
def add(a,b):
print(a+b)
return a+b
def subtract(a,b):
print(a-b)
return a-b
def multiply(a,b):
print(a*b)
return a*b
def divide(a,b):
print(a/b)
return a/b
但是,更好的方法是 print
主函数中 returns 函数的编号。例如:
def add(a,b):
return a+b
def subtract(a,b):
return a-b
def multiply(a,b):
return a*b
def divide(a,b):
return a/b
print('Please select an operation:')
print('1.Add')
print('2.Subtract')
print('3.Multiply')
print('4.Divide')
choice = input("Enter a choice: '1/2/3/4'")
a = int(input('Enter first number'))
b = int(input('Enter second number'))
if choice == '1':
result = add(a, b)
elif choice == '2':
result = subtract(a, b)
elif choice == '3':
result = multiply(a, b)
elif choice == '4':
result = divide(a, b)
print(result)