【发布时间】:2020-08-11 18:07:27
【问题描述】:
我有一个名为 lab01.py 的 python 文件,它包含两个函数
def both_positive(a, b):
return a > 0 and b > 0
def sum_digits(x):
"""
Sum all the digits of x.
"""
ans = 0
num = str(x)
for ele in num:
ans += eval(ele)
return ans
我需要在 Git Bash 中打印 sum_digits(x) 函数的输出。这是我输入的命令行
$ cd ~/desktop/programming/lab01
$ python -c 'import lab01; print lab01.sum_digits(10)'
【问题讨论】:
-
用于打印功能调用
() -
@Alan Hoover 感谢您的帮助!你的意思是 $python -c 'import lab01;打印 (lab01.sum_digits(10))'
-
@hck007
$python -c 'import lab01; print(lab01.sum_digits(10)) -
为什么在这里使用
eval()?一个简单的int()电话不会解决问题吗?此外,除非绝对必要,否则请不要将信息作为图像共享。请参阅:meta.stackoverflow.com/questions/303812/…、idownvotedbecau.se/imageofcode、idownvotedbecau.se/imageofanexception。