我是一個初學程序員,試圖編寫一個將生成隨機密碼的python腳本。但是,即使我聲明瞭編碼#utf-8,我總是會得到一個非ASCII字符錯誤,正如Stack Overflow中的另一個類似問題所述。這是源代碼:非ASCII字符錯誤python
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 "."
這是終端輸出
佩德羅@佩德羅-靈-3521:〜/桌面$蟒passwordgenerator.py
文件 「passwordgenerator.py」,線9 SyntaxError:第9行文件passwordgenerator.py中的非ASCII字符'\ xc2',但未聲明編碼;詳情請見http://www.python.org/peps/pep-0263.html
我查了一下鏈接,但是我不明白一件事,也許是因爲我不是一位母語英語的人,而且自從我不久前開始編程,感謝您的幫助。
使用的raw_input'()'上,而不是'輸入的Python 2.7() '。後者證明了它的輸入,即它的工作原理類似於'eval(raw_input())'。 – jfs 2014-10-11 19:36:56