I'm writing a python program that will search for a password that is the first piece in a string. I'm using raw_input() to do it, and here's what the code is like

list = ['Nether\n']
password = list[0]
print password
if raw_input('Please Enter Password: ') == password:
    print "Successful"

This code will print 'Nether' with a line after it (it's supposed to be like that), but when I put it into the raw_input() statement, it doesn't accept the password. Please note this is a simplified version of the code but it is the area where the bug is originating, I can post the whole thing if necessary.

Edited by Reverend Jim: Moved to programming forum

6 Months
Discussion Span
Last Post by Gribouillis

Here is an experiment in the python console

>>> s = raw_input('Please Enter Password: ')
Please Enter Password: Nether
>>> s

You see that the string has been stripped of the \n character. It means that you could define

thelist = ['Nether']

One usually avoids list as a variable name in python, because it is already a useful built-in symbol.
A useful step with raw_input() is to remove the white characters at both end with the .strip() method. Here is what happens if the user answers with a lot of space characters:

>>> s = raw_input('Spam: ')
Spam:       Eggs    
>>> s
'      Eggs    '
>>> s.strip()

You could use

thelist = ['Nether']
password = thelist[0]
print password
if raw_input('Please Enter Password: ').strip() == password:
    print "Successful"

A slightly safer strategy is to use getpass.getpass() which does not echo the password in the console

>>> from getpass import getpass
>>> s = getpass('Please enter password: ')
Please enter password: 
>>> s.strip()

It is very unsafe to hard code the password in plain text in your python program: everybody can read it. A trick is to use hashlib

>>> from hashlib import sha224
>>> thelist = ['570500279d16b4ff46eed5b83473e56beef1ddaa0867329c464b2d1f']
>>> s = raw_input('Please enter password: ').strip()                                                               
Please enter password: Nether
>>> if sha224(s).hexdigest() == thelist[0]:
...  print('Correct!')

Also notice that Nether is an extremely weak password as it contains a small number of ascii characters. An example of a strong password is 6VuBY&{-&4}u_o7w%NAD

Edited by Gribouillis

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.