简体   繁体   English

脚本在解释器中运行奇怪。 -i标志有什么影响?

[英]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.

相关问题 我的SDK有自己的python解释器。 如何为其安装模块? - My SDK has its own python interpreter. How do I install modules for it? VS Code 需要一个 Python 解释器。 为什么这是必要的? - VS Code needs a Python Interpreter. Why is this necessary? 找不到工作的 python 解释器。 团结,Firebase - Could not find a working python interpreter. Unity, Firebase 带有 docker 解释器的 VS 代码。 无法导入模块 - VS code with docker interpreter. Can't import module ctypes在Python解释器中表现得很奇怪 - ctypes behaving strangely in Python interpreter 我可以复制 Python 解释器并重命名它而不会产生不良影响吗? - Can I copy a Python interpreter and rename it without ill effect? python time.time() 和 time.perf_counter() 在 window、ubuntu 和 Z2567A554EC9305EB18AC2DZ8 解释器中的结果不同。 它来自病毒吗? - python time.time() and time.perf_counter() are different result in window, ubuntu and web based interpreter. is it from viirus? 代码从解释器运行,但不在编辑器中运行 - code runs from interpreter but not in editor 当setup.py与Python版本3解释器一起运行时,如何构建py2 wheel软件包? - How do I build a py2 wheel package, when setup.py runs with a Python version 3 interpreter? 如何将 arguments 传递给通过管道传输到标准输入上的 Python 解释器的脚本? - How do I pass arguments to a script piped to the Python interpreter on stdin?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM