如何在终端中显示 Python 程序的最终返回值?
How to display the final returned value from a Python program in terminal?
我只是在编写一个 Python 程序,它接受 2 个整数和 returns 总和。我遇到的问题是终端从不显示返回值,所以我不得不求助于打印语句。有什么办法可以让终端显示程序的返回值吗?
代码如下:
import argparse
def simple_addition():
parser = argparse.ArgumentParser(description = "Simple Addition Program to Test CLI")
# each line add AN argument to our terminal inout
parser.add_argument("first_number", type = int, help = "enter the first integer")
parser.add_argument("second_number", type = int, help = "enter the second integer")
args = parser.parse_args()
# storing the entered argument internally within our code for ease of access
first = args.first_number
second = args.second_number
# calculate and return the sum
sum = first + second
print("A PRINT CALL (not return): ", sum)
return sum
simple_addition()
下面是我运行这个程序时的终端截图
Is there any way to have the terminal display the returned value of the program?
是的,通过使用 print
。这就是它的用途。
so I have to resort to making a print statement
您不会像不“求助”+
运算符以将两个数字相加那样“求助”它。这就是它的用途。
但是,打印 和 从函数返回结果确实不是好的设计(例如,它会阻止您在不这样做的情况下重用该函数)我不想打印结果)。
与其在函数内部打印结果,不如在调用代码中打印返回值:
print(simple_addition())
或
result = simple_addition()
print(result)
#! /usr/bin/env python3
import argparse
def simple_addition():
parser = argparse.ArgumentParser(description = "Simple Addition Program to Test CLI")
# each line add AN argument to our terminal inout
parser.add_argument("first_number", type = int, help = "enter the first integer")
parser.add_argument("second_number", type = int, help = "enter the second integer")
args = parser.parse_args()
# storing the entered argument internally within our code for ease of access
first = args.first_number
second = args.second_number
# calculate and return the sum
sum = first + second
#print("A PRINT CALL (not return): ", sum)
return sum
if __name__ == '__main__':
print(simple_addition())
- 在第一行添加 shebang
chmod +x
- 不要在函数内部打印
- 只有当它是主要的时候才这样做(这样你就可以在需要时导入模块)
然后就可以直接调用了
./"CLI Testing.py" 35 45
我只是在编写一个 Python 程序,它接受 2 个整数和 returns 总和。我遇到的问题是终端从不显示返回值,所以我不得不求助于打印语句。有什么办法可以让终端显示程序的返回值吗?
代码如下:
import argparse
def simple_addition():
parser = argparse.ArgumentParser(description = "Simple Addition Program to Test CLI")
# each line add AN argument to our terminal inout
parser.add_argument("first_number", type = int, help = "enter the first integer")
parser.add_argument("second_number", type = int, help = "enter the second integer")
args = parser.parse_args()
# storing the entered argument internally within our code for ease of access
first = args.first_number
second = args.second_number
# calculate and return the sum
sum = first + second
print("A PRINT CALL (not return): ", sum)
return sum
simple_addition()
下面是我运行这个程序时的终端截图
Is there any way to have the terminal display the returned value of the program?
是的,通过使用 print
。这就是它的用途。
so I have to resort to making a print statement
您不会像不“求助”+
运算符以将两个数字相加那样“求助”它。这就是它的用途。
但是,打印 和 从函数返回结果确实不是好的设计(例如,它会阻止您在不这样做的情况下重用该函数)我不想打印结果)。
与其在函数内部打印结果,不如在调用代码中打印返回值:
print(simple_addition())
或
result = simple_addition()
print(result)
#! /usr/bin/env python3
import argparse
def simple_addition():
parser = argparse.ArgumentParser(description = "Simple Addition Program to Test CLI")
# each line add AN argument to our terminal inout
parser.add_argument("first_number", type = int, help = "enter the first integer")
parser.add_argument("second_number", type = int, help = "enter the second integer")
args = parser.parse_args()
# storing the entered argument internally within our code for ease of access
first = args.first_number
second = args.second_number
# calculate and return the sum
sum = first + second
#print("A PRINT CALL (not return): ", sum)
return sum
if __name__ == '__main__':
print(simple_addition())
- 在第一行添加 shebang
chmod +x
- 不要在函数内部打印
- 只有当它是主要的时候才这样做(这样你就可以在需要时导入模块)
然后就可以直接调用了
./"CLI Testing.py" 35 45