Home > Enterprise >  "Str object has no attribute append" Python
"Str object has no attribute append" Python

Time:09-22

I have no idea how to write python I am currently trying and have been stuck on this error for 3 hours, I have absolutely no idea what to do anymore

import os, sys
clear = lambda: os.system("clear")
from Account import *
account = Account() 
name = Account()
password = Account()

while True:
    print()
    print('User Authentication:')
    print('1. Login to Account')
    print('2. Create Account')
    print('3. Exit')
    print()

    action = input('Please Select Option: ')
    action = action[0]
    print()

    if action == '1':
        account.login(name, password)
    elif action == '2':
        account.create(name, password)
    elif action == "3":
      sys.exit()

For my main.py and for account.py here

import os

class Account():
  def __init__(self):
    self.name = []
    self.password = []
    self.clear = lambda: os.system("clear")
    
  def login(self, name, password):
    self.name = input("Please enter your username: ")
    if password != self.password or name != self.name:
      print("Incorrect Username or password")
    if password == self.password and name == self.name:
      print("Log in successful")
  def create(self, name, password):
    self.name = input("Please enter a username for your account: ")
    self.name.append(name)
    self.password = input("Please create a password for your account: ")
    self.password.append(password)

CodePudding user response:

You have redefined the name variable in create method, after that you thought it is still a list no it is not ,now it is a string and you need to rename the variables.

I don't know if that is right because I have no computer now but I will write it for you and you try and tell me.

import os

class Account():
  def __init__(self):
    self.names = []
    self.passwords = []
    self.clear = lambda: os.system("clear")
    
  def login(self, name, password):
    self.name = input("Please enter your username: ")
    if password != self.password or name != self.name:
      print("Incorrect Username or password")
    if password == self.password and name == self.name:
      print("Log in successful")
  def create(self, name, password):
    name = input("Please enter a username for your account: ")
    self.names.append(name)
    password = input("Please create a password for your account: ")
    self.passwords.append(password)

Your code has a lot of errors but I have tried to solve your main error only.

CodePudding user response:

it still doesnt work but whatecer im on the verge of killing myself, ikll just bhand it in

CodePudding user response:

You are overwriting self.name. Try this instead.

self.name.append(input("Please enter a username for your account: "))

third and fourth row from the bottom.

Also you probably dont want to use input() if you already pass some arguments to your functions

  • Related