简体   繁体   English

使用Python简介文本的字符串处理问题

[英]String processing issue using a Python intro text

What's wrong with this bit of Python? 这一点Python有什么问题? Entering the names does not work. 输入名称不起作用。

def main():
    print("This program generates computer usernames.\n")

    # get user's first and last names
    first = input("Please enter your first name (all lowercase): ")
    last = input("Please enter your last name (all lowercase): ")

    # concatenate first initial with 7 chars of the last name
    uname = first[0] + last[:7]

    # output the username
    print("Your username is:", uname)

main()

Now, running the program results in this -- no idea what it's all about. 现在,运行该程序会导致这一点 - 不知道它是什么。

cd '/Users/ek/Desktop/' && '/usr/bin/pythonw'                     '/Users/ek/Desktop/fun.py'  && echo Exit status: $? && exit 1
EKs-Mac-mini:~ ek$ cd '/Users/ek/Desktop/' && '/usr/bin/pythonw'  '/Users/ek/Desktop/fun.py'  && echo Exit status: $? && exit 1


This program generates computer usernames.

Please enter your first name (all lowercase): Bob


Traceback (most recent call last):
File "/Users/ek/Desktop/fun.py", line 14, in <module>
main()
File "/Users/ek/Desktop/fun.py", line 5, in main
first = input("Please enter your first name (all lowercase): ")
File "<string>", line 1, in <module>
NameError: name 'Bob' is not defined
EKs-Mac-mini:Desktop ek$ 

In your code, change input (which works in Python 3) to raw_input (which works in Python 2) and see what happens. 在您的代码中,将input (在Python 3中工作)更改为raw_input (在Python 2中工作)并查看会发生什么。

And to confirm which Python version you're using, run this Terminal command: python --version 要确认您正在使用哪个Python版本,请运行此终端命令: python --version

Instead of changing input to raw_input so your script will work in Python 2, you could add a shebang line so the Terminal shell will run your script in Python 3. 您可以添加一个shebang行,以便终端shell在Python 3中运行您的脚本,而不是将input更改为raw_input以便您的脚本可以在Python 2中运行。

A shebang line tells the shell (1) that the file is a shell script, (2) which Python interpreter to use, and (3) where to find that interpreter (the file path) . 一个shebang行告诉shell (1)该文件是一个shell脚本,(2)使用哪个Python解释器,以及(3)在哪里找到该解释器(文件路径)

Then you should just be able to run $ fun.py in Terminal. 然后你应该能够在终端中运行$ fun.py You shouldn't need to specify $ python3 fun.py . 您不需要指定$ python3 fun.py


Two ways to write the shebang line. 编写shebang线的两种方法。

1) Put this line at the top of your fun.py file: 1)将此行放在fun.py文件的顶部:

#! /usr/bin/env python3

That is the recommended way to write the shebang line . 这是编写shebang线的推荐方法 It works in UNIX-like operating systems (including Linux, Mac OS X, etc.) 它适用于类UNIX操作系统(包括Linux,Mac OS X等)

2) Or, specify the complete absolute path to the location where Python 3 is installed. 2)或者,指定安装Python 3的位置的完整绝对路径。 In your case: 在你的情况下:

#! /Library/Frameworks/Python.framework/Versions/3.5/bin/python3

If you don't know the absolute path, find it using the Terminal command $ which python3 . 如果您不知道绝对路径,请使用Terminal命令$ which python3找到它。


PS As an alternative, changing the default version of Python on your system from Python 2 to Python 3 might also work. PS作为替代方案, 将系统上Python的默认版本从Python 2更改为Python 3也可能有效。 But it has other bad consequences so don't do it . 但它有其他不良后果, 所以不要这样做

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM