如果条件返回内部

__init__.py在每个子目录中添加一个空文件,使它们成为模块。


.

├── code

│   ├── __init__.py

│   └── scripts

│       ├── __init__.py

│       └── script1.py

└── main.py

然后,如果您调用hello了一个函数,则code/scripts/script1.py可以通过以下方式导入该函数:


from code.scripts.script1 import hello

hello("yo")


阿晨1998
浏览 188回答 3
3回答

手掌心

不确定您想要实现的目标,但根据您提供的信息:def hi (n):&nbsp; &nbsp; while (n < 5):&nbsp; &nbsp; &nbsp; &nbsp; print("we are inside hi")&nbsp; &nbsp; &nbsp; &nbsp; n -= 1简单来说,return在函数内部使用就是返回后面的值,None没有值就返回。此外,函数的执行在 return 语句执行后立即终止。您可以使用该return语句,但是如果您想迭代它是不正确的,因为您的函数将终止其执行。还请记住,一旦您执行循环的迭代,在您的函数内将不会有更多的语句要执行,这意味着return将执行返回的隐式语句None,然后函数再次结束执行。

宝慕林4294392

你可以使用这个:def hi (n):&nbsp; &nbsp; while n <= 5:&nbsp; &nbsp; &nbsp; &nbsp; print("we are inside hi")&nbsp; &nbsp; &nbsp; &nbsp; n+=1&nbsp; &nbsp;return nn=1hi(n)您需要一个循环来迭代,并且 return 语句将从函数调用中退出。

MMMHUHU

当然,你需要一个循环来进行迭代。如果您只想打印语句 4 次,只需创建一个循环范围。def hi ():&nbsp; &nbsp; for n in range(4):&nbsp; &nbsp; &nbsp; &nbsp; print(n+1," we are inside hi")hi()
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python