-1

I just finished Coursera's Python for Everybody 1st course.

To practice my skills, I decided to make a password and username login. Whenever I create a username, I get my user set error which says 'Invalid credentials'. Here is my code.

import time
import datetime

print ('storingData')
print("Current date and time: ", datetime.datetime.now())
while True:
    usernames = ['Admin']
    passwords = ['Admin']

    username = input ('Please enter your username, to create one, type in create: ')

    if username == 'create':
        newname = input('Enter your chosen username: ')
        usernames.append(newname)
        newpassword = input('Please the password you would like to use: ' )
        passwords.append(newpassword)
        print ('Temporary account created')
        continue

    elif username in usernames :
        dataNum = usernames.index (username)
        cpasscode = passwords[dataNum]

    else:
        print ('Wrong credentials, please try again')
        continue

    password = input ('Please enter your password: ')

    if password == cpasscode:
        print ('Welcome ', username)

The code as it appears in my editor

Jerry Stratton
  • 3,287
  • 1
  • 22
  • 30
Emp1
  • 11
  • 1
  • 2
    Could you please paste your code here so we can run it? – 101 Feb 06 '18 at 05:54
  • Where are you coming out of the while True loop ? – Teja Feb 06 '18 at 06:07
  • Looking at your provided code in the image, I can't find "Invalid credentials" anywhere - do you mean the "Wrong credentials, please try again" method, or is there a portion of code we don't see? – MutantOctopus Feb 06 '18 at 06:11
  • Also try initializing your arrays before while True loop – Teja Feb 06 '18 at 06:15
  • Please, please, please start practicing storing passwords correctly. You need a randomly generated salt/hash of 12 to 16 bytes in length, AND you need to use PBKDF2, BCrypt, SCrypt, or Argon2. I do have [a Python implementation example of PBKDF2 using hashlib at my Github repository](https://github.com/Anti-weakpasswords/PBKDF2-Python-Builtin) including test vectors as sample code. To save, generate random salt, save salt (plaintext), save iteration count, and save PBKDF2 result. To login again, fetch salt and iteration count, apply PBKDF2 again, and see if the result is what was stored. – Anti-weakpasswords Feb 07 '18 at 03:59
  • @Anti-weakpasswords I will. But this was just a program for fun and I just started python – Emp1 Feb 07 '18 at 04:34
  • @Anti-weakpasswords I will. But this was just a program for fun and I just started python – Emp1 Feb 07 '18 at 04:34

2 Answers2

2

In your code, you have initialized your usernames array right after the while statement. This means that every time it loops back to the beginning, it re-initializes, losing anything that your previously appended. If you move the array initialization outside of the loop, it should work as expected.

  • Oh thanks a lot. I had a mere thought that it would be something like that. Thanks – Emp1 Feb 06 '18 at 07:28
  • @Emp1 - if the answer solved your problem, don't forget to mark it as the correct answer with the checkmark below the vote buttons. It will allow people with similar problems to find it more easily, and will also remove this question from the "unanswered questions" list. – MutantOctopus Feb 07 '18 at 09:16
0

This works for python 3. for python 2 you must take input differently refer: Python 2.7 getting user input and manipulating as string without quotations

import time
import datetime

names = ['Admin']
pwds  = ['Admin']

while True:
    name = input('Name/create: ')

    if name == "create":
        name = input('New Name: ')
        pwd  = input('New Pwd : ')
        names.append(name)
        pwds.append(pwd)
        continue
    elif name in names:
        curpwdindex = names.index(name)
        print(names)
        curpwd = pwds[curpwdindex]
        givenpwd = input('Password: ')
        if givenpwd == curpwd:
            print("Welcome")
            break
        else:
            print("Inavlid Credential")
    else:
        print("Wrong Choice")
        continue
Teja
  • 141
  • 1
  • 6
  • is it possible that I can store these usernames and passwords in a list/array even after quitting the program. To be saved for later? – Emp1 Feb 06 '18 at 07:36
  • Yes you ca use JSON/CSV Format to store the data in a file. You can search about that. – Teja Feb 06 '18 at 09:59