Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Non ASCII character error python

I'm a beginner programmer trying to write a python script that will generate random passwords. However, I always get a non-ASCII character error even though I declared the coding #utf-8, as mentioned in another similar question here in Stack Overflow. This is the source code:

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 "."

This is the terminal output

pedro@pedro-Inspiron-3521:~/Desktop$ python passwordgenerator.py

File "passwordgenerator.py", line 9 SyntaxError: Non-ASCII character '\xc2' in file passwordgenerator.py on line 9, but no encoding declared; see http://www.python.org/peps/pep-0263.html for details

I checked the link but I couldn't understand a thing, maybe because I'm not a native english speaker and since I started programming not long ago.Thanks in advance for your help.

like image 214
chilliefiber Avatar asked Mar 18 '23 05:03

chilliefiber


2 Answers

The #coding: utf-8 line needs to be at the top of the file to allow non-ASCII characters.

£, «, and », are not ASCII.

While you probably don't want non-ascii characters in your password, and they wouldn't be allowed on most sites, there is no reason they couldn't theoretically be permitted.

like image 50
agf Avatar answered Mar 28 '23 09:03

agf


You have the >> and << symbols in your punctuations list.

These are not valid. Try to find their unicode code instead

http://www.fileformat.info/info/unicode/char/bb/index.htm http://www.fileformat.info/info/unicode/char/ab/index.htm

EDIT: Oh, but wait, this is a password generator, so NO do not even put the double chevrons as part of the punctuations list, as these are not valid in any password.

punctuation = ("!", ".", ":", ";", ",", "?", "'", "@", "$", "~", "^", "%", "#", "&", "/", range(0, 11))

Also, why do you add digits to the punctuation tuple?

Why not using string.punctuation and string.digits instead?

like image 31
DevLounge Avatar answered Mar 28 '23 09:03

DevLounge