Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
我有一段代码在python idle中运行得很好,但在终端中运行得不正确_Python_Python 3.x - Fatal编程技术网

我有一段代码在python idle中运行得很好,但在终端中运行得不正确

我有一段代码在python idle中运行得很好,但在终端中运行得不正确,python,python-3.x,Python,Python 3.x,我使用的是python 3.5.2,除了缺少冒号之外,我看不出您编写的代码有任何特殊问题。这就是我成功运行的原因。似乎你没有复制粘贴你正在运行的代码,因为你说你的代码中有冒号。所以也许可以试试我的,看看在性格方面是否有差异 def main(): name = input('Typer your name and press enter: ') name_list = name.split() print(name_list) first = nam

我使用的是python 3.5.2,除了缺少冒号之外,我看不出您编写的代码有任何特殊问题。这就是我成功运行的原因。似乎你没有复制粘贴你正在运行的代码,因为你说你的代码中有冒号。所以也许可以试试我的,看看在性格方面是否有差异

def main(): 
     name = input('Typer your name and press enter: ')
     name_list = name.split()

     print(name_list)

     first = name_list[0][0]
     middle = name_list[1][0]
     last = name_list[2][0]

     print(first.upper(),'.', middle.upper(),'.', last.upper()) 

main()

您可能还想了解名称长度小于或等于3个单词时的处理。

您是在Python 2中运行代码,而不是在Python 3中运行代码。。。观察

def main():
    name = input('Type your name and press enter: ')
    name_list = name.split()

    print(name_list)

    first = name_list[0][0]
    middle = name_list[1][0]
    last = name_list[2][0]

    print(first.upper(), '.', middle.upper(), '.', last.upper())

main()
您可以看到,我的默认
python
实际上是python2

$ python3 script.py
Typer your name and press enter: ang go koms
['ang', 'go', 'koms']
A . G . K

名称列表在第三行,名称也像其他名称一样缩进。您是否收到任何错误或终端响应?您需要将
defmain()
更改为
defmain():
我确实有defmain():我忘记把它放在这里了。每次我在terminalIt中运行中间名时,中间名的语法都是无效的。对于没有中间名且显示的名称长度不超过3的任何名称,中间名都将失败。但除此之外,它在我能看到的终端中工作得很好。请让我们知道您是如何在终端中执行它的,以及错误是什么。当它运行时,我输入了ang go koms作为名称。它在go中的o下表示无效语法。它仍然无法在我的终端中正常运行。谢谢你的帮助。你说的终点站到底是什么意思?您的意思是在操作系统的终端上使用命令“python_file.py”执行python文件吗?谢谢大家的帮助!我看到了输入与原始输入的区别。非常感谢。
$ python3 script.py
Typer your name and press enter: ang go koms
['ang', 'go', 'koms']
A . G . K
$ python --version
Python 2.7.13