简体   繁体   English

在 Git Z7024FF0860DEC6AB1519A3F71C7D879 的 python 文件中运行 function

[英]Running a function in a python file in Git Bash

I have a python file called lab01.py and it contains two functions我有一个名为 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

I need to print out the output of sum_digits(x) function in Git Bash.我需要在 Git Z7024Z3860DEC7D6B771 中打印出 sum_digits(x) function 的 output Here is my command line I entered这是我输入的命令行

$ cd ~/desktop/programming/lab01
$ python -c 'import lab01; print lab01.sum_digits(10)'

The first command line runs successfully but I got a SyntaxError for the second line.第一个命令行成功运行,但第二行出现 SyntaxError。 Any help?有什么帮助吗? 在此处输入图像描述

Try adding the parenthesis to the print function:尝试将括号添加到打印 function:

$ python -c 'import lab01; print(lab01.sum_digits(10))'

The syntax without the parenthesis is for python 2, and your Git Bash emulator should be running python 3 if that's what you installed on your computer. The syntax without the parenthesis is for python 2, and your Git Bash emulator should be running python 3 if that's what you installed on your computer.

Python 2 print statement was replaced with a function (hence the parenthesis) in Python 3. So you need to use print() . Python 2 print 语句被替换为 Python 3. 所以你需要使用print()

edit: also, if that's the code you are using for the lab01.py file, your return seems to be outside the function block.编辑:另外,如果这是您用于 lab01.py 文件的代码,那么您的返回似乎在 function 块之外。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM