gpt4 book ai didi

python-2.7 - 非ASCII字符错误python

转载 作者:行者123 更新时间:2023-12-04 23:49:24 24 4
gpt4 key购买 nike

我是一个初学者程序员,正在尝试编写一个会生成随 secret 码的 python 脚本。但是,我总是收到非 ASCII 字符错误,即使我声明了编码 #utf-8,正如 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 行
语法错误:第 9 行文件 passwordgenerator.py 中的非 ASCII 字符“\xc2”,但未声明编码;见 http://www.python.org/peps/pep-0263.html详情

我检查了链接,但我什么都听不懂,可能是因为我不是英语母语人士,而且因为我不久前开始编程。在此先感谢您的帮助。

最佳答案

#coding: utf-8行需要在文件的顶部以允许非 ASCII 字符。
£ , « , 和 » , 不是 ASCII。

虽然您可能不希望密码中包含非 ascii 字符,而且大多数网站都不允许使用非 ASCII 字符,但理论上没有理由不允许它们。

关于python-2.7 - 非ASCII字符错误python,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26317759/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com