【发布时间】:2014-12-06 16:56:19
【问题描述】:
我是一名初级程序员,正在尝试编写一个生成随机密码的 python 脚本。但是,即使我声明了编码 #utf-8,我总是会收到非 ASCII 字符错误,正如 Stack Overflow 中另一个类似问题中提到的那样。这是源代码:
import string
import random
#coding: utf-8
print "Password generator will create a random customizable password."
print "Choose your options wisely."
number = int(input("How many letters do you want in your password?"))
caps = str(input("Do you want capital letters in your password? Y/N"))
symbols = str(input( "Do you want punctuation, numbers and other symbols in your password? Y/N"))
punctuation = ("!", ".", ":", ";", ",", "?", "'", "@", "£", "$", "«", "»", "~", "^","%", "#", "&", "/", range(0, 11))
lowercase = string.ascii_lowercase
uppercase = string.ascii_uppercase
if caps == "N":
characters = lowercase
else:
characters = uppercase + lowercase
if symbols == "Y":
characters += punctuation
password = random.sample(characters, number)
print "The password is", password "."
这是终端输出
pedro@pedro-Inspiron-3521:~/Desktop$ python passwordgenerator.py
文件“passwordgenerator.py”,第 9 行 SyntaxError:第 9 行的文件 passwordgenerator.py 中的非 ASCII 字符“\xc2”,但未声明编码;详情见http://www.python.org/peps/pep-0263.html
我检查了链接,但我什么都听不懂,可能是因为我不是以英语为母语的人,而且我不久前开始编程。在此先感谢您的帮助。
【问题讨论】:
-
在 Python 2.7 上使用
raw_input()而不是input()。后者评估其输入,即它的工作方式类似于eval(raw_input())。
标签: python-2.7 non-ascii-characters