• Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.


    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

Python [OOP] I can't create an object - I got an invalid syntax error.

Kaworu

Active Coder
Hi.

I created a simple script to generate safe passwords. When I tried to rewrite it as OO code, I suddenly have an error.

My code is this:

Python:
import string
import random

class RandomPassword:

    def __init__(self):
        self.lc = string.ascii_lowercase
        self.uc = string.ascii_uppercase
        self.nr = string.digits
        self.special = string.punctuation

    def random_lowercase(self):
        random_lc = random.choice(self.lc)
        return random_lc

    def random_uppercase(self):
        random_uc = random.choice(self.uc)
        return random_uc

    def random_nr(self):
        random_nr = random.choice(self.nr)
        return random_nr

    def random_special(self):
        random_special = random.choice(self.special)
        return random_special

    def generate_safe_password(self, size = 20):
        random_list = list()

        while size != 0:
            random_step = random.randint(1,4)
            if random_step == 1:
                x = random_lowercase()
                random_list.append(x)
            elif random_step == 2:
                x = random_uppercase()
                random_list.append(x)
            elif random_step == 3:
                x = random_nr()
                random_list.append(x)
            elif random_step == 4:
                x = random_special()
                random_list.append(x)
            
            size = size - 1
        
        return random_list

    def string_from_list(self, my_list):
        my_string = "".join(my_list)

        return my_string

    def final_result(self):
        a = RandomPassword.generate_safe_password()
        b = RandomPassword.string_from_list(a)
        
        return b
    
############################
# PROGRAM
############################

if __name__ == "__main__":

    pass = RandomPassword()
    a = pass.final_result()
    print("Your new safe password is: ", a)

The error is in the first line after if __name__ == "__main__". Exactly in the place when there is an equality sign. SyntaxError: invalid syntax

Its kinda hard to say what's wrong. Indexation? Seems alright, Something else?

I would appreciate any help ;-)
 
I tried your code and kept getting lots of errors.
Here is a simple example for you to work with.

Python:
# Do the imports
from random import sample
import string


class Generate:
    '''
        Generate uses letters, digits, and special characters
        to generate a 8 character password
    '''
    def __init__(self, count=8):
        ''' Combine all usable characters '''
        characters = string.ascii_letters + string.digits + '!@#&+*'
      
        # We want at least 8 characters
        if count < 8:
            count = 8
      
        # Let's keep the character count under 12'
        if count > 12:
            count = 12
          
        # Get a sample of  8 characters
        self.password = ''.join(sample(characters, count))
      
    def __str__(self):
        # Return the password as a string
        return self.password
      

print(f'Default 8 characters -> {Generate()}')

print(f'10 Characters -> {Generate(10)}')

Code:
Default 8 characters -> z+*9o7Nd
10 Characters -> Hb#KUQDan2



I corrected your code. Now it works.
Other than the error mentioned above, when calling a function in class you must prepend self.
Example instead of func(), it needs to be self.func()

Python:
import string
import random

class RandomPassword:

    def __init__(self):
        self.lc = string.ascii_lowercase
        self.uc = string.ascii_uppercase
        self.nr = string.digits
        self.special = string.punctuation

    def random_lowercase(self):
        random_lc = random.choice(self.lc)
        return random_lc

    def random_uppercase(self):
        random_uc = random.choice(self.uc)
        return random_uc

    def random_nr(self):
        random_nr = random.choice(self.nr)
        return random_nr

    def random_special(self):
        random_special = random.choice(self.special)
        return random_special

    def generate_safe_password(self, size = 20):
        random_list = list()

        while size != 0:
            random_step = random.randint(1,4)
            if random_step == 1:
                x = self.random_lowercase()
                random_list.append(x)
            elif random_step == 2:
                x = self.random_uppercase()
                random_list.append(x)
            elif random_step == 3:
                x = self.random_nr()
                random_list.append(x)
            elif random_step == 4:
                x = self.random_special()
                random_list.append(x)
            
            size = size - 1
        
        return random_list

    def string_from_list(self, my_list):
        my_string = "".join(my_list)

        return my_string

    def final_result(self):
        a = RandomPassword().generate_safe_password()
        b = RandomPassword().string_from_list(a)
        
        return b
    
############################
# PROGRAM
############################

if __name__ == "__main__":

    mypass = RandomPassword()
    a = mypass.final_result()
    print("Your new safe password is: ", a)
 
Last edited:
Top Bottom