I've worked on a random user generator in C a long time ago, so I thought I'd make an advanced version of it in Python(3.2). I'll have you know, I am an amateur in Python and even programming for that matter so I'd really appreciate some criticism on my code and what more can be added.
About the program - I've added a default list of users, you can modify the list to your convenience and for data persistence, I made use of the pickle module. The random module plays the pivotal role generating the output making use of the choice method. Those along with the os module if the program is being run for the first time on a pc, it will create a data file with the default list.After some digging around, I managed to find os.getlogin() from the python library which was exactly what I was looking for (to get the computer's name making sure it can get the data file to the correct directory). I'm a little skeptical about that part, wondering whether or not it will work on every pc.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 | '''Program to generate a random member from a list of users,
the members of the default list can be removed or added to your convenience'''
import random,pickle,os#three of the modules used in the below code
g=os.getlogin()#gets the name of the computer
def adduser():#function definition for adding users to the list
while 1==1:
print(a)
k=input('enter the name of the user(leave blank if none):')
if k=='':
break
else:
a.append(k)
f=open('random_members.data','wb')
pickle.dump(a,f)
f.close()
def deluser():#function definition for deleting users from the list
while 1==1:
print(a)
k=input('enter the name of the user(leave blank if none):')
if k=='':
break
else:
for l in range(0,len(a)):
if k==a[l-1]:
del a[l-1]
f=open('random_members.data','wb')
pickle.dump(a,f)
f.close
if os.path.isfile('C:\\Users'+os.sep+g+os.sep+'random_members.data') != True:#checking if the data file is already present in the computer, will make one if it is being run for the first time
a=['warun','Morpheus3000','coolpcguy','David_007','fatalevolution','erif','[xubz]','GTX OC']#default list of members
f=open('random_members.data','wb')
pickle.dump(a,f)#dumps the default list
f.close()
else:#this block grabs the list from the data file
f=open('random_members.data','rb')
a=pickle.load(f)
f.close()
y=input('would you like to add more users to the list y/n or press d to delete users:')
if y=='y':#block for adding users
adduser()
print(a)
t=input('do you want to delete some users(y/n):')#provides one chance to remove some users
if t=='y':
deluser()#function call to delete users
print('and the random member is',random.choice(a))
else:
print('and the random member is',random.choice(a))
elif y=='n':#quickest way to get an output
print('and the random member is',random.choice(a))
elif y=='d':
deluser()
print(a)
t=input('do you want to add new users(y/n):')#provides one chance to add some users
if t=='y':
adduser()#function call to add users
print('and the random member is',random.choice(a))
else:
print('and the random member is',random.choice(a))
else:#this block is used if the input taken is unrecognisable however, I need to figure out a way to get it back to the beginning
print('unrecognisable command')
|
When I first started working on this, I thought it would come in handy when a group of members(in a forum/community) are playing a little game/challenge/performing a task and need to choose a member at random. Any other use will be encouraged.