Python Forum
Login with using class
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Login with using class
#1
users={"meryem":"4444","ahmet":"sehir123","gakkos":"2323"}
login=raw_input("Username: ")
inventory = {'asparagus': [10, 5], 'broccoli': [15, 6], 'carrots': [18, 7],
             'apples': [20, 5], 'banana': [10, 8], 'berries': [30, 3],
             'eggs': [50, 2], 'mixed fruit juice': [0, 8], 'fish sticks': [25, 12],
             'ice cream': [32, 6], 'apple juice': [40, 7], 'orange juice': [30, 8],
             'grape juice': [10, 9]}
market_inventory=[]
customers_basket=[]
if login in users:
    psw=raw_input('Password: ')
    if users[login]==psw:
        print('Successfully logged in!'),('\n'),"Welcome,",(login),"Please choose one of the following options by entering the corresponding menu number."

    else:
        print('Your user name and/or password is not correct. Please try again!')
else:
    print "Your user name and/or password is not correct. Please try again!"
that is my login code how ı translate into class ?
Reply
#2
class userpass:
    users = {"meryem":"4444","ahmet":"sehir123","gakkos":"2323"}
    inventory = {'asparagus': [10, 5], 'broccoli': [15, 6], 'carrots': [18, 7],
             'apples': [20, 5], 'banana': [10, 8], 'berries': [30, 3],
             'eggs': [50, 2], 'mixed fruit juice': [0, 8], 'fish sticks': [25, 12],
             'ice cream': [32, 6], 'apple juice': [40, 7], 'orange juice': [30, 8],
             'grape juice': [10, 9]}
    market_inventory = []
    customers_basket = []
    def login(self):
        while True: #added
            login = raw_input('Username:')
            if login in self.users.keys():
                psw = raw_input('Password:')
                if self.users[login] == psw:
                    print 'Successfully logged in!','\n','Welcome,',login,"Please choose one of the following options by entering the corresponding menu number."
                    break #added
                else:
                    print 'Your user name and/or password is not correct. Please try again!'
            else:
                print "Your user name and/or password is not correct. Please try again!"
            
Output:
>>> log = userpass() >>> log.login() Username:gakkos Password:2323 Successfully logged in! Welcome, gakkos Please choose one of the following options by entering the corresponding menu number. >>>
recapping/learning while providing solution , wont mind if someone will correct me, i appreciate that.
i assume you hv done this in python 2.x (raw_input function available) so i left out the brackets in print statement.
i added while and break statement so the code will keep asking for correct username & password
swallow osama bin laden
Reply
#3
For security, the program should ask the password previous to cheking the login name, otherwise an attacker could find valid login names.
Reply
#4
(Feb-03-2018, 06:12 AM)ka06059 Wrote:
class userpass:
    users = {"meryem":"4444","ahmet":"sehir123","gakkos":"2323"}
    inventory = {'asparagus': [10, 5], 'broccoli': [15, 6], 'carrots': [18, 7],
             'apples': [20, 5], 'banana': [10, 8], 'berries': [30, 3],
             'eggs': [50, 2], 'mixed fruit juice': [0, 8], 'fish sticks': [25, 12],
             'ice cream': [32, 6], 'apple juice': [40, 7], 'orange juice': [30, 8],
             'grape juice': [10, 9]}
    market_inventory = []
    customers_basket = []
    def login(self):
        while True: #added
            login = raw_input('Username:')
            if login in self.users.keys():
                psw = raw_input('Password:')
                if self.users[login] == psw:
                    print 'Successfully logged in!','\n','Welcome,',login,"Please choose one of the following options by entering the corresponding menu number."
                    break #added
                else:
                    print 'Your user name and/or password is not correct. Please try again!'
            else:
                print "Your user name and/or password is not correct. Please try again!"
            
Output:
>>> log = userpass() >>> log.login() Username:gakkos Password:2323 Successfully logged in! Welcome, gakkos Please choose one of the following options by entering the corresponding menu number. >>>
recapping/learning while providing solution , wont mind if someone will correct me, i appreciate that.
i assume you hv done this in python 2.x (raw_input function available) so i left out the brackets in print statement.
i added while and break statement so the code will keep asking for correct username & password

thanks a lot my friend it Works :))

if I don't understand somewhere could ı ask you ?

that is normal
menu=True
while menu:
    print ("""
                                1.Search for a product.
                                2.See Basket
                                3.Check Out
                                4.Logout
                                5.Exit
                                """)
    answer = raw_input("Your choice: ")


    if answer=="1":


                        message3=raw_input("Please select which item you want to add to your basket (Enter 0 for main menu): ")

                        for key,value in inventory.items():
                            if key in inventory.items():
                                print "found",value,"similar items"

                            elif message3 in key:
                                print key, ":", value
ı have to create menü but it can't see my code. ı changed little bit but doesn't work when ı change item in inventory program can not add basket when ı chhose 2 in menü.
Reply
#5
few lines need to recheck...
if key in inventory.items(): is always false, so your code will never execute the print "found",value,"similar items" statement.

nothing is wrong withelif message3 in key: but i prefer elif message3 == key: depend on how we view the code
swallow osama bin laden
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020