我是一名初学者,正在尝试编写一个生成随机密码的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行 语法错误:文件passwordgenerator.py中的非ASCII字符“\xc2”,但没有声明编码;请参见http://www.python.org/peps/pep-0263.html了解详情。
我查看了链接,但无法理解其中的内容,可能是因为我不是以英语为母语的人,而且我刚开始学习编程。提前感谢您的帮助。
raw_input()
而不是input()
。后者会评估其输入,即它的工作方式类似于eval(raw_input())
。 - jfs