4
votes

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.

3
use raw_input() on Python 2.7 instead of input(). The latter evals its input i.e., it works like eval(raw_input()). - jfs

3 Answers

5
votes

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.

2
votes

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?

0
votes

If your are using Mac, there is file .DS_STORE which causes the ascii error while reading contents of your folder.

So do these if you're using mac

  • Remove all DS_Store files

  • Select Applications > Utilities to launch Terminal.

  • Enter the following UNIX command:

  • sudo find / -name ".DS_Store" -depth -exec rm {} \;

  • When prompted for a password enter your Mac OS X Administrator password