gpt4 book ai didi

python - 代码以交互方式运行时显示结果,但从 shell 运行时不显示

转载 作者:太空宇宙 更新时间:2023-11-04 10:32:11 27 4
gpt4 key购买 nike

我从另一位发帖人那里借用了这个小科学记数法脚本:Display a decimal in scientific notation .

def format_e(n):
a = '%E' % n
return a.split('E')[0].rstrip('0').rstrip('.') + 'E' + a.split('E')[1]

format_e(Decimal('40800000000.00000000000000'))
# '4.08E+10'

format_e(Decimal('40000000000.00000000000000'))
# '4E+10'

format_e(Decimal('40812300000.00000000000000'))

当从终端或 Python Shell 作为模块操作时,该函数工作正常。但是,当像“python Converter.py”一样运行时,它会立即终止而不返回上述三个示例中的任何一个。

最佳答案

交互式解释器自动回显任何不返回 None 的表达式的结果。在脚本中,您需要显式打印您想要查看的结果:

print format_e(Decimal('40800000000.00000000000000'))
print format_e(Decimal('40000000000.00000000000000'))
print format_e(Decimal('40812300000.00000000000000'))

关于python - 代码以交互方式运行时显示结果,但从 shell 运行时不显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25774953/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com