4

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.

chilliefiber
  • 571
  • 2
  • 7
  • 18
  • use `raw_input()` on Python 2.7 instead of `input()`. The latter evals its input i.e., it works like `eval(raw_input())`. – jfs Oct 11 '14 at 19:36

3 Answers3

5

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.

agf
  • 171,228
  • 44
  • 289
  • 238
2

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?

DevLounge
  • 8,313
  • 3
  • 31
  • 44
  • So in the links you gave me it says that « in python source code is u"\u00AB"and » is u"\u00BB". Should I include this inside "" or not? – chilliefiber Oct 11 '14 at 18:45
  • I deleted them, but it's still not working. I get the same mistake – chilliefiber Oct 11 '14 at 18:57
  • Here is the punctuation variable that I currently have: punctuation = ("!", ".", ":", ";", ",", "?", "'", "@", "£", "$", "~", "^","%", "#", "&", "/", range(0, 11)) – chilliefiber Oct 11 '14 at 18:58
  • Try without the Sterling Pound symbol – DevLounge Oct 11 '14 at 19:00
  • I did that and removed the full stop at the end of the last print statement and I get this output in terminal:pedro@pedro-Inspiron-3521:~/Desktop$ python passwordgenerator.py Password generator will create a random customizable password. Choose your options wisely. How many letters do you want in your password?9 Do you want capital letters in your password? Y/NY Traceback (most recent call last): File "passwordgenerator.py", line 7, in caps = str(input("Do you want capital letters in your password? Y/N")) File "", line 1, in NameError: name 'Y' is not defined – chilliefiber Oct 11 '14 at 19:03
  • Sorry because it looks kinda messy, but I couldn't put any more characters in that last comment. I think this mistake is an exception or sth. However, I have never written any code that deals with exceptions. It's something like try ..... finally, right? – chilliefiber Oct 11 '14 at 19:06
  • Try using raw_input instead of input to see if it works – DevLounge Oct 11 '14 at 19:09
  • Yes, it worked. Thanks a lot. Btw, why did the shift to raw_input do the trick – chilliefiber Oct 11 '14 at 19:12
  • raw_input must be used instead of input, when you want to capture strings or numbers. FYI, Python 3's input is actually raw_input, the old input has been removed from Python 3. – DevLounge Oct 11 '14 at 19:15
  • 1
    To answer your question, I have no idea what a digit is, or what string.punctuation and string.digits would do. However, I'll surely search it. Thanks, kind stranger – chilliefiber Oct 11 '14 at 19:16
  • @Apero Those characters should be fine, he's specified the file is UTF-8. He just needs to move the coding line to the top of the file. – agf Oct 11 '14 at 19:29
  • True, but as I mentioned, this couldn't be used by a user when he has to type his password, so it's better to use only items from strings.punctuation – DevLounge Oct 11 '14 at 19:44
  • 1
    Sorry @agf and Apero , can't upvote any of your answers, tried to and it says Vote up requires 15 reputation :( – chilliefiber Oct 11 '14 at 20:46
  • Forget about that, now I was able to upvote, it's done – chilliefiber Oct 11 '14 at 20:55
0

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

Alfergon
  • 5,463
  • 6
  • 36
  • 56
Deepak
  • 1
  • 2
  • While this is a good answer and possible, a more likely cause on a Mac is copying from an editor to a terminal which leads to non-Python friendly leading whitespace. Simply replacing the leading whitespace will correct the issue. – David Hoelzer Nov 23 '15 at 13:01