[英]Script runs strangely in interpreter. What is the effect of the -i flag?
I recently made a program which prints a word diagonally. 我最近制作了一个对角打印一个单词的程序。 Whenever I go to the console/python interpreter and type
每当我去console / python解释器并输入
python3 "xxx.py"
, it will just continue onto the next line and won't do anything. python3 "xxx.py"
,它将继续到下一行,不会做任何事情。
However, if I do: python3 -i "xxx.py"
it enters python and lets me enter inputs for my program 但是,如果我这样做:
python3 -i "xxx.py"
它进入python并让我输入我的程序的输入
Why is this happening? 为什么会这样?
My code (copied from comment below): 我的代码(从下面的评论复制):
def diagonal(text, right_to_left = False):
#Code for diagonal
diagonal()
I got this error: 我收到了这个错误:
Traceback (most recent call last): File "question1.py", line 17, in <module> diagonal()
TypeError: diagonal() missing 1 required positional argument: 'text'
The flag -i
tells python to process the script, and then enter interactive mode. 标志
-i
告诉python处理脚本,然后进入交互模式。 Without the -i
python will just process the script and then exit. 没有
-i
python将只处理脚本然后退出。
A script might define functions, classes etc, but not call them. 脚本可以定义函数,类等,但不能调用它们。 If you want the script to do something, you must have at least one line in your script that calls a function.
如果希望脚本执行某些操作,则脚本中必须至少有一行调用函数。
The usual pattern is: 通常的模式是:
#class and function definitions
def print_diagonal(x):
#code for diagonal
def main():
#code for running the program
word = input()
print_diagonal(word)
#run the program
main()
The error you get is because the diagonal function has one required argument, the text. 你得到的错误是因为对角函数有一个必需的参数,即文本。 You need to supply this argument in some way.
你需要以某种方式提供这个论点。 You could use an
input
function in the code (as in my example), or you could use the command line by import sys
, and reading sys.argv[]
. 您可以在代码中使用
input
函数(如我的示例中所示),或者您可以通过import sys
使用命令行,并读取sys.argv[]
。 The python documentation has examples of this python文档有这样的例子
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.