Sep 8, 2015 04:14 AM (This post was last modified: Sep 8, 2015 04:16 AM by Reformer.)
Hi, I made this program about 2 days ago and wanted to share/ get your opinions on it. I am very new to python myself so forgive me if some of the stuff in there is messy/ wrong. I had a bit of help coding this from another HF user so thanks a bunch to him :).
logged_out= {
"users" :1,
"admins":1
}
#defines the dictionaries
username = raw_input ("Enter username: ")
password = raw_input ("Enter password: ")
def log_in(username,password):
info = "none"
success= False
if username in users and password == users[username]:
success= True
info = users[username]
print "Welcome %s " % info
elif username in users and password != users[username]:
success = False
print "Sorry try again"
if success == True and username == 'username123':
logged_in["users"] += 1
logged_out["users"] -=1
print logged_in
print logged_out
elif success == True and username == 'admin123':
logged_in["admins"] +=1
logged_in["admins"] -=1
print logged_in
print logged_out
logout = raw_input('Welcome to my program would you like to log out yes or no? ')
if logout == 'yes' and username == 'username123':
logged_in['users'] -=1
logged_out['users'] +=1
success = False
print logged_in
print logged_out
return 'See you later %s' % info
elif logout == 'yes' and username == 'admin123':#made a condtional for each username that way if one username signs off then both wont sign off
logged_in['admins'] -=1
logged_out['admins'] +=1
success = False
print logged_in
print logged_out
return 'See you later %s' % info
elif 'n' or 'no':
return 'well you better gtfo cus this program is over bitch'
else:
return 'no hablo espanol'
print log_in(username,password)
#don't know why the log_in/ log_out print statements aren't right :/
#needs to be worked on more. Currently does not work but not giving up!
#will store login/logout data
this will check if your account is in the database and if it is, it will grant you access
Code
#program that inserts new users to the data list
accounts = {
}
account_types = {
"users":0,
"admins":0
logged_in = {
"users":0,
"admins":0
}
logged_out = {
"users":1,
"admins":1
}
print raw_input("Welcome! To sign up please enter your username and password below: ")
if len(username) > 0 and len(password) > 0:
accounts[username]=password
print accounts
if username != "admin123":
logged_in["users"]+=1
print logged_in
elif username == "admin123":
logged_in["admins"]+=1
print logged_in
this inserts your account into the data list if you aren't signed up. I would implement this one into my first one but idk how to go about that yet :(.
Anyway, I would love it if you guys gave me your opinions on this and told me what i could've done better :). Thanks!
Sep 8, 2015 08:19 AM (This post was last modified: Sep 8, 2015 08:21 AM by donkeymon.)
Nice! that is potentially a great introductory step into something possibly OOP related :D, what I mean by that is you could possibly make a class or maybe simple just a function that can instantiate new accounts with your second code there :). the best part about functions is their variables aren't saved past that so you can continue to use them if you do have overlapping variables in each of those codes :). The closest thing I could get for you to show you more about functions that isn't just a wall of boring text is this :D it is in python 3 so some things may look a tad bit different.
I should say that, I know you are using functions in the first code, but you could also use one in the second so that you could simple call something like 'create_Acc()' that can take two parameters the user and password and check to make sure they are use-able and then it can store them in the dictionary :D
(Sep 8, 2015 08:19 AM)donkeymon Wrote: Nice! that is potentially a great introductory step into something possibly OOP related :D, what I mean by that is you could possibly make a class or maybe simple just a function that can instantiate new accounts with your second code there :). the best part about functions is their variables aren't saved past that so you can continue to use them if you do have overlapping variables in each of those codes :). The closest thing I could get for you to show you more about functions that isn't just a wall of boring text is this :D it is in python 3 so some things may look a tad bit different.
I should say that, I know you are using functions in the first code, but you could also use one in the second so that you could simple call something like 'create_Acc()' that can take two parameters the user and password and check to make sure they are use-able and then it can store them in the dictionary :D
hey man thanks for the feedback! dont know if you'll read this but if you do what do you mean by the variables in the functions aren't stored? I used functions because the whole time I've been learning python the thing that has been the hardest to grasp are functions so I thought writing a program using functions would make me more comfortable, but you said that variables in functions aren't stored so would the changes to the lists/dictionaries also not be stored permanently after the function ends? If so, then why use functions then if they don't save the things inside of them after they finish? Sorry if this question is long/hard to understand but i just really want to know why/when it would be good to use functions :).
(Sep 8, 2015 01:27 PM)SIM-1866423 Wrote: Use database and maybe basic security system ?
PHP Code
<?php import time import sqlite3(my prefer) or mysql
Time for security,3 wrong password = 1min sleep etc..
Database modules for admins&users and of course logs(X login X time etc..)
#Try HARDER
those sound like good ideas but i haven't really learned how to import or know what to import. Will definitely try it as I expand my knowledge of python. Thanks a lot for the advice really gave me some good ideas for the future :). Also how do you know what to import? Is there a list that can be found somewhere that has all the functions(dont remember what theyre called) that you can import into your code?
1)Python has too many module (core or external), so you can write ur own module or you can use modules.
2)Google.Ex: "Python list directory",First link, first comment
PHP Code
<?php from os import listdir
3)
PHP Code
<?php import os print "\n".join([x for x in dir(os) if x.islower() and not x.startswith("_")])
Sep 8, 2015 02:30 PM (This post was last modified: Sep 8, 2015 02:34 PM by Reformer.)
Quote:1)Python has too many module (core or external), so you can write ur own module or you can use modules.
This sounds interesting most likely will use it once i learn about it thanks! I didn't know you could actually make your own modules. Is making them by yourself better than just importing or does it depend? Sorry for asking too many questions lol
*edit was confused at first by 3 but then i ran it and found out that 1st one prints out the whole directory as long as its not lower case and does it in a more orderly manner. I feel like ive accomplished something :p (probably haven't but eeh).
Always I'm saying this, before start something learn basics.Before I start python,my "basics" education took six 6 months from me.Because I'm pretty stupid :)
Sep 8, 2015 03:28 PM (This post was last modified: Sep 8, 2015 03:30 PM by donkeymon.)
(Sep 8, 2015 01:23 PM)Reformer Wrote: hey man thanks for the feedback! dont know if you'll read this but if you do what do you mean by the variables in the functions aren't stored? I used functions because the whole time I've been learning python the thing that has been the hardest to grasp are functions so I thought writing a program using functions would make me more comfortable, but you said that variables in functions aren't stored so would the changes to the lists/dictionaries also not be stored permanently after the function ends? If so, then why use functions then if they don't save the things inside of them after they finish? Sorry if this question is long/hard to understand but i just really want to know why/when it would be good to use functions :).
no biggie man, good question functions begin to bring in the idea of variable scopes, and those are a lot easier than they can show to be. Variable scope is essentially the can and can not's of variable usage this video I think should help alot the same ides go to py2 :)
Functions are a massive bonus in programming although it will look kind of odd at this current time :P There are changes you can do to "Return" information back from a function :) TheNewBoston has a pretty nice youtube video tutorial on these basics :P here is the return statement I mentioned. I can only have 1 video posted within the forum post but the other one is right below :p
Always I'm saying this, before start something learn basics.Before I start python,my "basics" education took six 6 months from me.Because I'm pretty stupid :)
#Try HARDER 😏
thank you so much man! I shall now give birth to my very own module and name it: modulus the king of modules :p. About the second thing yeah man i can relate to it aswell. It's taken me some time and a lot of help (thankfully there are people like u and donkey that have helped me a lot xD) to get even half the basics. Will definitely keep going lol who knows maybe i'll code something amazing someday just gotta keep believing.
(Sep 8, 2015 03:28 PM)donkeymon Wrote: no biggie man, good question functions begin to bring in the idea of variable scopes, and those are a lot easier than they can show to be. Variable scope is essentially the can and can not's of variable usage this video I think should help alot the same ides go to py2 :)
Functions are a massive bonus in programming although it will look kind of odd at this current time :P There are changes you can do to "Return" information back from a function :) TheNewBoston has a pretty nice youtube video tutorial on these basics :P here is the return statement I mentioned. I can only have 1 video posted within the forum post but the other one is right below :p
Of course I should mentiona this particular video tutorial playlist appears to be for py3. but the changes are minor to what you need :D.
It seems that every time you reply i seem to learn something new about coding just by reading xD. You're a great help to me man. I should buy l33t just so i can rep u for all the help you've given me lol. Thanks again!
(Sep 8, 2015 03:54 PM)Reformer Wrote: thank you so much man! I shall now give birth to my very own module and name it: modulus the king of modules :p. About the second thing yeah man i can relate to it aswell. It's taken me some time and a lot of help (thankfully there are people like u and donkey that have helped me a lot xD) to get even half the basics. Will definitely keep going lol who knows maybe i'll code something amazing someday just gotta keep believing.
It seems that every time you reply i seem to learn something new about coding just by reading xD. You're a great help to me man. I should buy l33t just so i can rep u for all the help you've given me lol. Thanks again!
lol np, I never saw much point in rep man :p until they make it a requirement for something I directly require on here at least :), again if you have any questions a pm or another forums post should get a reply by me at some time :) I'd offer skype but really I don't touch it much anymore xD