0
import getpass

class UserRegistration(object):

    def __init__(self):
        pass

    def register(self):
        register = self.register
        self.username_and_password = {}
        username = raw_input("Choose Username> ")
        password = getpass.getpass("Choose Password> ")
        confirm_password = getpass.getpass("Confirm Password> ")
        if password == confirm_password:
            self.username_and_password[username] = password
        else:
            print "Passwords didn't match"
            return register

go = UserRegistration()
go.register()

Simple program that prompts user for username and password

If the passwords don't match, I want it to restart the process and prompt the user to enter password again

At the moment, it prints the string but doesn't restart the process.

Any ideas?

karthikr
  • 97,368
  • 26
  • 197
  • 188
z82n
  • 115
  • 2
  • 11
  • Not quite a dupe, but this may be useful: [Asking the user for input until they give a valid response](http://stackoverflow.com/questions/23294658/asking-the-user-for-input-until-they-give-a-valid-response) – Kevin Aug 25 '14 at 16:13

2 Answers2

4

Call the method again:

   else:
        print "Passwords didn't match"
        self.register()

def register(self): is a method of the class, so you call it with self.register,, there is no need to use register = self.register

If you just want to prompt for the password and store multiple instance details in the dict:

class UserRegistration(object):
    username_and_password = {}
    def __init__(self):
        self.username = ""
    def register_name(self):
        self.username = raw_input("Choose Username> ")
        self.register_pass()

    def register_pass(self):
        password = getpass.getpass("Choose Password> ")
        confirm_password = getpass.getpass("Confirm Password> ")
        if password == confirm_password:
            self.username_and_password[self.username] = password
        else:
            print "Passwords didn't match"
            self.register_pass()

You can also use a while loop:

class UserRegistration(object):
    username_and_password = {}
    def __init__(self):
        self.username = ""
    def register_name(self):
        while True:
            self.username = raw_input("Choose Username> ")
            if self.username in self.username_and_password:
                print "Username taken, please try again"
                continue
            else:
                return self.register_pass()

    def register_pass(self):
        while True:
            password = getpass.getpass("Choose Password> ")
            confirm_password = getpass.getpass("Confirm Password> ")
            if password == confirm_password:
                self.username_and_password[self.username] = password
                return
            else:
                print "Passwords didn't match"
                continue
Padraic Cunningham
  • 176,452
  • 29
  • 245
  • 321
  • This will reset `self.username_and_password` and potentially lose any previously stored credentials. – hennes Aug 25 '14 at 16:04
  • @hennes, why would you want to store the credentials if they are incorrect? The OP wants to call the method again if the passwords do not match – Padraic Cunningham Aug 25 '14 at 16:05
  • the Username is correct, it is the passwords that don't match – z82n Aug 25 '14 at 16:06
  • @Padraic Cunningham No, that's not what I meant. The code in the question just gave me the idea that `self.username_and_password` was meant to store multiple users' credentials (because it's a dict). But then again it gets reset on every call to `register` so that doesn't work anyway. Nah, just forget what I said. :P – hennes Aug 25 '14 at 16:08
  • @z82n, if you want to just re enter the passwords , you would want a separate method that takes the username, you cannot call the the method and expect to only run part if it. – Padraic Cunningham Aug 25 '14 at 16:10
  • 2
    Caution: if the function calls itself, the script will crash with `maximum recursion depth exceeded` after a thousand consecutive failed attempts. (devil's advocate: if your users aren't the persistent type, this isn't much of a practical problem) – Kevin Aug 25 '14 at 16:10
1

You need to call self.register(), and calling register() would do this, but in a way that will not pay off in the long run. (Though what you really need is just a loop. Recursion will result a pointless copies of your class on the stack.)

What is wrong here indicates the need to expound a really basic Python concept:

Python names do not do or invoke things. Only the operators do, and they do so by calling named methods of some object. So it is the parentheses, or the dot, if you are using a property, that cause the execution. Functions, and even methods or properties are objects. Saying 'return register' means 'return the object representing function that does this', not 'invoke this function and return the result'.

Also, since people have a hard time thinking of a method, rather than a function, as an object, assigning self.register to another variable is bizarre. Referring to bound things with local variables, especially of the same name, is generally a way to create bugs, unless you are doing something quite ornate.