[英]I have a code that is running perfectly in the python idle but it is not running in the terminal correctly
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()
我正在使用python 3.5.2
您正在Python 2中而不是Python 3中运行代码。
$ python script.py
Typer your name and press enter: ang go koms
Traceback (most recent call last):
File "script.py", line 13, in <module>
main()
File "script.py", line 2, in main
name = input('Typer your name and press enter: ')
File "<string>", line 1
ang go koms
^
SyntaxError: invalid syntax
因此,您的“错误”。 查找input
与raw_input
的区别……这是一个常见问题。
现在,尝试使用Python3
$ python3 script.py
Typer your name and press enter: ang go koms
['ang', 'go', 'koms']
A . G . K
您可以看到我的默认python
实际上是Python 2
$ python --version
Python 2.7.13
除了缺少的冒号外,我看不到您编写的代码有任何特定问题。 这就是我成功运行的结果。 似乎您没有复制粘贴正在运行的代码,因为您说代码中包含冒号。 所以也许尝试我的,看看在字符方面是否有区别。
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()
您可能还希望查看名称长于或短于3个单词的情况。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.