Python Programming Error

Questions about programming languages and debugging
Post Reply
User avatar
solidsnake
forum buddy
forum buddy
Posts: 16
Joined: 15 Sep 2008, 16:00
15

Python Programming Error

Post by solidsnake »

Ok, as i was granted permission to make a new topic on this here is the little problem.

I am learning Python from 3 different books. Beginning Python From Novice To Professional, Wrox - Beginning Python, and finally Byte of Python.

Here is a three trial on a program that would authenticate a user name and a password to allow anyone permission to enter (enter where i don't know, it is just something i tried to do as a first program)

Code: Select all

Usr = ["mark" , "foo" , "boo"]
Pas = ["12345"]
Printable = "Welcome! Have a nice day"
while True:
    User = raw_input("Username: ")
    Pass = raw_input("Password: ")
    if User == Usr and Pass == Pas:
       print Printable
       Break
else:
    print "Access denied, Please try again"

Code: Select all

Usr = ["mark" , "foo" , "boo"]
Pas = ["12345"]
Printable = "Welcome! Have a nice day"
while True:
    User = raw_input("Username: ")
    Pass = raw_input("Password: ")
    if User == Usr = True and Pass == Pas = True:
       print Printable
       Break
else:
    print "Access denied, Please try again"

Code: Select all

Usr = ["mark" , "foo" , "boo"]
Pas = ["12345"]
Printable = "Welcome! Have a nice day"
while True:
    User = raw_input("Username: ")
    Pass = raw_input("Password: ")
    if User in Usr = True and Pass in Pas = True:
       print Printable
       Break
else:
    print "Access denied, Please try again"

Code: Select all

Usr = ["mark" , "foo" , "boo"]
Pas = ["12345"]

while True:
    User = raw_input("Username: ")
    Pass = raw_input("Password: ")
    if User in Usr = True 
    if Pass in Pas = True:
       print "Welcome! Have a nice day"
       Break
else:
    print "Access denied, Please try again"
The thing that annoys me the most is it only loops the Username then password entry nothing more. Type the right username or even password it will still loop.

As you can see different combinations doesn't do any good. I even have another file using a dictionary and a tuple. Still nothing. I know it is the While loop and i tried to change the condition but still...

Thanks in advance

User avatar
maboroshi
Dr. Mab
Dr. Mab
Posts: 1624
Joined: 28 Aug 2005, 16:00
18

try this

Post by maboroshi »

Try this :-)

and I will try and explain

Code: Select all

Usr = ["mark", "foo1", "foo2"]
Pas = "12345"
Printable = "Welcome! Have a nice day"
while True:
    User = raw_input("Username: ")
    Pass = raw_input("Password: ")
    if User == Usr[0] and Pass == Pas:
       print Printable
       break
    else:
        print "Access denied, Please try again"
Your using a list for the username/password so you should reference the list when identifying the username/password

so Usr[0]

takes the first indice out of the list which is "mark" in any case you could use Usr[1] and Usr[2] for foo1 and foo2. "break" should be lower case python is case sensitive

and your else statement should be in the loop if you want to exit the loop upon a wrong user pass

type

Code: Select all

    else:
        print "Access denied, Please try again"
        break
anyway hope this helped :-)

User avatar
solidsnake
forum buddy
forum buddy
Posts: 16
Joined: 15 Sep 2008, 16:00
15

Post by solidsnake »

Oh dear GOD. That really worked AMAZING :D

thank you thank you thank you thank you thank you thank you thank you
(i swore i will thank the one doing this like never before :P)

Now the solution you provided is actually awesome since it is the first time the program worked like it should be. But one problem remain. If there is 3 users like now i need to make a 3 if blocks for each user to be confirmed in this user/pas program. Kinda easy. But if there is 100 users now that won't be practical. Any ideas how to over come this ? I thought about tuples but it is already the same idea. later on even tuples can't change so i can't really "edit" any user from inside the program.

T H A N K Y O U !!!!!!!!!

G-Brain
Fame ! Where are the chicks?!
Fame ! Where are the chicks?!
Posts: 467
Joined: 08 Nov 2007, 17:00
16
Location: NL

Post by G-Brain »

Here's a decent example I hacked up:

Code: Select all

users = {
    'mark' : 'test',
    'foo'  : '12345',
    'boo'  : 'foo'
}

while True:
    input_user = raw_input('Username: ')
    input_pass = raw_input('Password: ')
    
    if input_user in users:
        if users[input_user] == input_pass:
            print 'User exists. Password correct. Successfully logged in'
            break
        else:
            print 'User exists. Password wrong.'
    else:
        print 'User does not exist'
If you'd want to add or edit users within your code, you could do it like this:

Code: Select all

users['myuser'] = 'mypassword'
Please note that in real world applications, the error messages would be a lot less verbose for security reasons, and the passwords would be encrypted and stored in a database or something, not in the code itself.

For learning purposes, this is pretty awesome though.

Look up "dictionaries", because that's what the users variable is in my example (also note the use of the in operator in the first if statement. Pretty simple.).
I <3 MariaLara more than all of you

User avatar
solidsnake
forum buddy
forum buddy
Posts: 16
Joined: 15 Sep 2008, 16:00
15

Post by solidsnake »

Thanks... that adds more options. I did try with dictionaries at first but sadly it gave me the same outcome with the while loop. That is when i tried with lists and tuples.

For the passwords... This just as you said for learning nothing more. Since i even haven't gotten into databases so i dunno yet how to use them.

Thank you very much

User avatar
solidsnake
forum buddy
forum buddy
Posts: 16
Joined: 15 Sep 2008, 16:00
15

Post by solidsnake »

Well another problem arrised when i made this code as well..
This time Python game me a weird error after saving and trying to run the program.

The error was :
Tapping (something): EOF in same linesegment
it also highlighted the line after my last code line but not in red, in grey an didn't run it. Anyone know what could trigger this?

Here is the code:

Code: Select all

Name_Company = []
Bought = []
Sell = []
Stock_Value = []
Initial_Stock = []
Stock_Remained = []
Date = []

date = raw_input("Date: ")
company_name = raw_input("What is the name of company you bought from: ")
bought = int (raw_input("How much stock did you buy: "))
sold = int (raw_input("How much stock did you sell: "))
initial_stock = int(raw_input("How many Stocks did you have: "))
stock_value = int (raw_input("what is the Stock value now: "))


Date.append(date)
Initial_Stock.append(initial_stock)
Name_Company.append(company_name)
Bought.append(bought)
Sell.append(sold)
Stock_Value.append(stock_value)
if sold <= 0:
    Stock_Remained.append(initial_stock + (stock_value * bought))
    print "Your today's business outcome is as follows: "
    print "************************************************************************"
    print "" , "Date" , "    " , "Company Name" , "    " , "Sold" , "  " , "Bought" , "  " , "Initial Value" , "  " , "Stock Value" , "  " , "Stock Remained"
    print "" , Date , "    " , Name_Company , "    " , Sell , "  " , Bought , "  " , Initial_Stock , "  " , Stock_Value , "  " , Stock_Remained
    print "************************************************************************"

elif bought > sold:
    Stock_Remained.append(initial_stock - ((stock_value * bought) -(stock_value * sold)))
    print "Your today's business outcome is as follows: "
    print "************************************************************************"
    print "" , "Date" , "    " , "Company Name" , "    " , "Sold" , "  " , "Bought" , "  " , "Initial Value" , "  " , "Stock Value" , "  " , "Stock Remained"
    print "" , Date , "    " , Name_Company , "    " , Sell , "  " , Bought , "  " , Initial_Stock , "  " , Stock_Value , "  " , Stock_Remained
    print "************************************************************************"

else:
    Stock_Remained.append(initial_stock - ((stock_value * sold) -(stock_value * bought))
    print "Your today's business outcome is as follows: "
    print "************************************************************************"
    print "" , "Date" , "    " , "Company Name" , "    " , "Sold" , "  " , "Bought" , "  " , "Initial Value" , "  " , "Stock Value" , "  " , "Stock Remained"
    print "" , Date , "    " , Name_Company , "    " , Sell , "  " , Bought , "  " , Initial_Stock , "  " , Stock_Value , "  " , Stock_Remained
    print "************************************************************************"

User avatar
Nerdz
The Architect
The Architect
Posts: 1127
Joined: 15 Jun 2005, 16:00
18
Location: #db_error in: select usr.location from sucko_member where usr.id=63;
Contact:

Post by Nerdz »

Give a man a fish, you feed him for one day.
Learn a man to fish, you feed him for life.

User avatar
maboroshi
Dr. Mab
Dr. Mab
Posts: 1624
Joined: 28 Aug 2005, 16:00
18

Usually

Post by maboroshi »

Usually EOF statements are because your forgot a ")" or something least what I found in my code when working in python

anyway post a link to the source so I don't have to copy paste (so I can simply right click the file and download) and I will see if I can help you but I encourage you to try and solve the problem yourself ;) As you will learn more than just giving up and posting in a forum

User avatar
solidsnake
forum buddy
forum buddy
Posts: 16
Joined: 15 Sep 2008, 16:00
15

Post by solidsnake »

Thanks a lot. I usually never post on a forum except as a last resort, but this time i didn't know what EOF meant. Usually python give more info with the error or a less vague one.

I will try to think about the [ ")" ] Issue, if i still didn't get it i will post the code for download.

Again Thank you.

User avatar
Uner
Fame ! Where are the chicks?!
Fame ! Where are the chicks?!
Posts: 123
Joined: 08 Dec 2007, 17:00
16
Location: 45*26'57.41* N

Post by Uner »

Oh god,

Oh damn, haven't messed around with Python for a long time...

Hmm. Can't you just....

Code: Select all

usr = raw_input('Enter Username... ')
while usr != 'Unerly':
    usr = raw_input('Wrong Username. Try Again... ')
    

password = raw_input('Enter password... ')
while password != 'example':
    password = raw_input('Wrong Password. Try Again... ')
    
print 'Access Granted'
It's a hell lot easier.

User avatar
solidsnake
forum buddy
forum buddy
Posts: 16
Joined: 15 Sep 2008, 16:00
15

Post by solidsnake »

Yes it is alot easier. lol

Say if you want to implement a list for passwords and usernames all you got to do is change the code to be like for example

in Xlist instead = "something" ?


I tried to check my code for the missing or misplaced ", "), "], ] but nothing was missing or miss typed. I can't get around this EOF error anyone can help in that? Sorry to be asking alot...

User avatar
Uner
Fame ! Where are the chicks?!
Fame ! Where are the chicks?!
Posts: 123
Joined: 08 Dec 2007, 17:00
16
Location: 45*26'57.41* N

Post by Uner »

Oh, you wanna make a list, okay, lol.

Post Reply