I'm trying to set up a login procedure. Here's what I'm trying:

import users
import sys

print()
print("Please enter your username.")
print()
username = input(">")
if username not in users.usrlist:
	print()
	print('User does not exist.')
	sys.exit()
else:
	user = users.username
	print()
	print('Please enter your password. Note that for security reasons, nothing will appear as you type.')
	print()
	pw = getpass.getpass(">")
	if pw == user['password']:
		print()
		print('Welcome, ' + user['name'] + '.')
		print()
	else:
		print()
		print('Incorrect password.')
		sys.exit()

The bit that isn't working is the "user = users.username" bit. I'm guessing it's trying to look for the entry "username" in the users.py file instead of changing the literal string "username" to the value of the user-input username. Is there a way to append whatever the user inputs to the end of "users." so that if the user enters, for instance, "Bob", it will look for "users.Bob"?

if username not in users.usrlist:

It appears that usrlist contains the user names, so you would want to append any new names to that list.

It appears that usrlist contains the user names, so you would want to append any new names to that list.

Right, I'd be doing that manually to start out. The problem I'm still having is with this, though:

Is there a way to append whatever the user inputs to the end of "users." (note the dot at the end) so that if the user enters, for instance, "Bob", it will look for "users.Bob"?

My current workaround is to add one of these for each existing user:

if username is 'Bob' or 'bob':
	user = users.bob
	print('Please enter your password')
...

The problem is that I'd have to write almost the entire logon code again and again for every single user that exists, which could potentially take up a ton of space and is very inefficient. I also think that the concept of using user input to determine the target would be handy in several other situations.

Is it just impossible to add user input to the end of "something." (note the dot) so that it ends up as "something.userinputhere" for the purpose of making the computer look at the 'userinputhere' section of the 'something.py' file?

How about you use a dictionary for usrlst. here's an example of a dictionary:

>>> usr_dict = { 'bob': 'foobar', 'joe': 'barfood', 'sally':'choco' }
>>> usr_inp = 'Bob'
>>> if usr_inp.lower() in usr_dict:
...     print usr_dict[usr_inp.lower()]
...     
foobar
>>>

Also, you should not be using input , you should be using raw_input

This part is a little screwy. You have username entered from the console so there is no need to use users.username instead, so a modified version of your code would be

if username not in users.usrlist:
	print()
	print('User does not exist.')
	sys.exit()
else:
	## user = users.username   Not necessary
	print( "\nHello" + username)
	print('Please enter your password. Note that for security reasons, nothing will appear as you type.')
	print()
	pw = getpass.getpass(">")
                ##   this line changed to username
	if pw == user.username['password']:
		print()
		print('Welcome, ' + username + '.')
		print()
	else:
		print()
		print('Incorrect password.')
		sys.exit()

Also note that in the above post the user name was changed to lower case to make it case insensitive which will find "Bob", "bob", "BOB" etc. Generally, a password program uses a dictionary with the user name as the key, pointing to the password as in Jim699's example. Since we don't have the code for users.py it's difficult to tell what the data structure is, but it appears to be a class, which is overkill for this. For future posts, please include the relevant parts of users.py. Then you can get more specific help.

Also, you should not be using input , you should be using raw_input

Note the print() statement. The OP is maybe using 3.0, especially if a string is returned for the input name statement.

I'm using Python 3. Okay, I'll provide logon.py and users.py both in their entirity to try and explain what I'm trying to accomplish here. I may be over-complicating it or going about it the wrong way, but this is all I can come up with.

import users
import sys

print()
print("Please enter your username.")
print()
username = input(">")
if username not in users.usrlist:
	print()
	print('User does not exist.')
	sys.exit()
else:
	user = users.username
	print()
	print('Please enter your password. Note that for security reasons, nothing will appear as you type.')
	print()
	pw = getpass.getpass(">")
	if pw == user['password']:
		print()
		print('Welcome, ' + user['name'] + '.')
		print()
	else:
		print()
		print('Incorrect password.')
		sys.exit()

while True:
	command = input(">")
	if command == "value":
		print(user['value'])
	if command == "info":
		print(user['info'])
usrlist = {
	'bob',
	'harvy',
	'oscar'
	}

oscar = {
	'name':'Bob Bobsworth',
	'password':'fishinglure',
	'info':'This is info specific to Bob.',
	'value':10
	}

harvy = {
	'name':'Harvy Halo',
	'password':'freakinscarabs',
	'info':'This is info specific to Harvy.',
	'value':4
	}
	
oscar = {
	'name':'Oscar T. Grouch',
	'password':'ilovetrash',
	'info':'This is info specific to Oscar.',
	'value':18
	}

So, depending on which user you're "logged in" as, you'll get different responses to the commands "info" and "value".

First, usrlist is not a list but a dictionary and you should get a syntax error (a list uses brackets usrlist = [] not {} ). Anyway, note the use of a dictionary of dictionaries to do this at the bottom for 'bob'. A dictionary hashes it's keys, meaning that it won't print in the order it was entered, so the first print is in a different order, but you want to look something up in a dictionary, not print in some order.

"""   This code is commented because it will yield a syntax error
usrlist = {
      'bob',
      'harvy',
      'oscar'
      }
       
oscar = {
      'name':'Oscar T. Grouch',
      'password':'ilovetrash',
      'info':'This is info specific to Oscar.',
      'value':18
      }
print type(usrlist), usrlist
print "\n", type(oscar), oscar
"""
#
# Use a dictionary of dictionaries
usr_dic = {}       ## main dictionary 

usr_dic['bob'] = {}   ## a sub dictionary for 'bob's info
usr_dic['bob']['name'] = 'Bob Bobsworth'
usr_dic['bob']['password'] = 'fishinglure'
usr_dic['bob']['info'] = 'This is info specific to Bob.'
usr_dic['bob']['value'] = 10

# another way to add data using 'oscar'
usr_dic['oscar'] = {}   ## a sub dictionary for 'oscar's info
this_user = usr_dic['oscar']
this_user['name'] = 'Oscar T. Grouch'
this_user['password'] = 'ilovetrash'
this_user['info'] = 'This is info specific to Oscar.'
this_user['value'] = 18

print usr_dic   ## entire dictionary
print

## example of lookup (of course there are only two keys)
if 'bob' in usr_dic:
   print usr_dic['bob']
   print "name for bob ==>", usr_dic['bob']['name']
   print "info for bob ==>", usr_dic['bob']['info']

##   a simple simulate input test
print "\nSimulating input"
name_input = 'oscar'
if name_input in usr_dic:
   print "     user name is OK"
   passw_input = 'ilovetrash'
   # or
   # this_user = usr_dic[name_input]
   # if this_user['password'] == passw_input:
   if usr_dic[name_input]['password'] == passw_input:
      print "     welcome", name_input
   else:
      print "Error encoumtered"
This article has been dead for over six months. Start a new discussion instead.