#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2013
    Posts
    6
    Rep Power
    0

    Question Help with my password system?


    Hello again. I need help with this code for my computing coursework - I am very new to python and coding in general.
    The code below is my attempt to create a system which gives you the password of an account of your choice. At the moment, I have a 'test' account in my student accounts list.

    Code:
    if v == ('3'):
                            studentaccountpasswords = 1
                            while studentaccountpasswords == 1:
                                print (mystudentaccounts[0:1000])
                                print ('Please type in the number of the account you would like to discover the password of. \nPlease note that 0 would gather the first account on the list, 1 the second and so on.')
                                accountnumber = input ('Account number: ')
                                if accountnumber in mystudentaccounts and mystudentpasswords:
                                    print (mystudentpasswords[accountnumber])
                                else:
                                    print ('This is an invalid account. Please try again.')
    However, if I input a '0' into the input it gives me this:

    This is an invalid account. Please try again.
    ['test']
    Please type in the number of the account you would like to discover the password of.
    Please note that 0 would gather the first account on the list, 1 the second and so on.
    Account number:
    and so on. All I would like to do is have the password of the account be printed when a number is inputted such as 0.
    The number '0' should be linked to the account and its password.
    I'm not sure what I'm doing wrong, can anyone help? Sorry for being so new.
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    78
    Rep Power
    2
    Tip1: Try to divide your program into functions or classes
    Tip2: Your post has weird formatting in the code section
    Tip3: the line "if accountnumber in mystudentaccounts and mystudentpasswords" seems off.
    Tip4: unittests They have helped me a bunch, and I'm sure they could help you too.
    Tip5: keep your lines below the 80 character limit.
    Last edited by leonnaley2; November 11th, 2013 at 06:04 AM.
  4. #3
  5. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,966
    Rep Power
    481
    I suspect the problem is failure to distinguish between str (that's string) and int (meaning integer) data types. '0' is a string, not a number.

    We need more information to understand.

    Do you use python 2 or python 3? The input function behaves differently.

    What are the data types of the mysterious
    mystudentaccounts and mystudentpasswords?


    I suggest that instead of providing all this information you rewrite your program to use a dictionary. Something like
    Code:
    import pprint
    
    accounts = {
        'school email': ';ae3as;ld',
        'gmail': 'asdf;lksajf',
        'bank': '838293873939872098'
        }
    
    #...
    
    if v == '3':
        pprint.pprint(accounts.keys())
        key = input('which account?')
        if key in accounts:
            print(accounts[key])
        else:
            print('This is an invalid account.')
    [code]Code tags[/code] are essential for python code and Makefiles!
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    May 2009
    Posts
    524
    Rep Power
    34
    This statement will probably always be true
    Code:
    if accountnumber in mystudentaccounts and mystudentpasswords:
    as "and mystrdentpasswords" will always evaluate to True unless it is an empty string or list, in which case it will always be False, and since we don't know what type of container it is or what it contains no more help can be offered. What are you trying to do here as account number should only be in studentaccounts (and you don't need a separate container to store the passwords)..
    Last edited by dwblas; November 11th, 2013 at 11:53 AM.
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2013
    Posts
    6
    Rep Power
    0
    Originally Posted by b49P23TIvg
    I suspect the problem is failure to distinguish between str (that's string) and int (meaning integer) data types. '0' is a string, not a number.

    We need more information to understand.

    Do you use python 2 or python 3? The input function behaves differently.

    What are the data types of the mysterious
    mystudentaccounts and mystudentpasswords?


    I suggest that instead of providing all this information you rewrite your program to use a dictionary. Something like
    Code:
    import pprint
    
    accounts = {
        'school email': ';ae3as;ld',
        'gmail': 'asdf;lksajf',
        'bank': '838293873939872098'
        }
    
    #...
    
    if v == '3':
        pprint.pprint(accounts.keys())
        key = input('which account?')
        if key in accounts:
            print(accounts[key])
        else:
            print('This is an invalid account.')
    I'm running Python 3.3.0
    the data types of both are strings, but they can also be integers, e.g. student08
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2013
    Posts
    6
    Rep Power
    0
    Originally Posted by b49P23TIvg
    I suspect the problem is failure to distinguish between str (that's string) and int (meaning integer) data types. '0' is a string, not a number.

    We need more information to understand.

    Do you use python 2 or python 3? The input function behaves differently.

    What are the data types of the mysterious
    mystudentaccounts and mystudentpasswords?


    I suggest that instead of providing all this information you rewrite your program to use a dictionary. Something like
    Code:
    import pprint
    
    accounts = {
        'school email': ';ae3as;ld',
        'gmail': 'asdf;lksajf',
        'bank': '838293873939872098'
        }
    
    #...
    
    if v == '3':
        pprint.pprint(accounts.keys())
        key = input('which account?')
        if key in accounts:
            print(accounts[key])
        else:
            print('This is an invalid account.')
    Using the code you posted, how would I append something into the accounts 'dictionary'? For example, how would I append a newly created password by the user into the accounts list so it came under 'password'?
  12. #7
  13. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,966
    Rep Power
    481
    Let's add a feature to your program. Instead of hard-coding the account information into the program source we will access it from a file---which could be encrypted.

    Code:
    # treat this entirely as pseudo code
    
    
    # initialization code
    with open('password_database_system.dat', 'rb') as inf: # restore accounts from permanent memory
        accounts = pickle.unpickle(inf)
    
    
    # ...
    
    
    # new menu option
    if v == 'a':  # add a new account
        new_account = prompt_for_str('new account name: ')
        new_password = prompt_for_str('    its password: ')
        accounts[new_account] = new_password # modify the accounts dictionary
        with open('password_database_system.dat', 'wb') as ouf: # save on permanent memory
            pickle.pickle(ouf, accounts)
    Last edited by b49P23TIvg; November 22nd, 2013 at 09:37 AM.
    [code]Code tags[/code] are essential for python code and Makefiles!
  14. #8
  15. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2013
    Posts
    6
    Rep Power
    0
    Originally Posted by b49P23TIvg
    Let's add a feature to your program. Instead of hard-coding the account information into the program source we will access it from a file---which could be encrypted.

    Code:
    # treat this entirely as pseudo code
    
    
    # initialization code
    with open('password_database_system.dat', 'rb') as inf: # restore accounts from permanent memory
        accounts = pickle.unpickle(inf)
    
    
    # ...
    
    
    # new menu option
    if v == 'a':  # add a new account
        new_account = prompt_for_str('new account name: ')
        new_password = prompt_for_str('    its password: ')
        accounts[new_account] = new_password # modify the accounts dictionary
        with open('password_database_system.dat', 'wb') as ouf: # save on permanent memory
            pickle.pickle(ouf, accounts)
    Okay, thanks
    one quick question, what does 'rb' and 'wb' mean when opening the file?
  16. #9
  17. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,966
    Rep Power
    481
    http://www.cygwin.com/cygwin-ug-net/using-textbinary.html
    [code]Code tags[/code] are essential for python code and Makefiles!
  18. #10
  19. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2013
    Posts
    6
    Rep Power
    0
    Originally Posted by b49P23TIvg
    http://www.cygwin.com/cygwin-ug-net/using-textbinary.html
    When my program starts I now get this error:
    Code:
    Traceback (most recent call last):
      File "N:\Sixth Form\Computer Science\Coursework\Self made coding examples\TeacherInterface test 3.py", line 3, in <module>
        myteacheraccounts = pickle.unpickle(inf)
    NameError: name 'pickle' is not defined
    I'm not quite sure what the problem is?
  20. #11
  21. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,966
    Rep Power
    481
    keep this under your pillow


    Read about the pickle module in the extensive online python documents. Follow another link here

    Import the pickle module. And what is unclear about
    Originally Posted by me
    # treat this entirely as pseudo code
    ?

    Finally, you can avoid quoting the prior entry either by deleting it from the edit window or by using the "post reply" button instead of the reply button. Keep trying.
    [code]Code tags[/code] are essential for python code and Makefiles!

IMN logo majestic logo threadwatch logo seochat tools logo