0

・IDとPasswordを管理するAccountクラスの作成

辞書型のプライベート変数 __data を作成
・以降,keyがID,valueがPasswordとなるようにする
コンストラクタで初期アカウントの作成
・引数なし
・初期アカウント:ID が root Password が root とする
アカウントの登録を行うregisterメソッド
・引数は、id, pw
・返り値は、”登録完了”
パスワードが合っていればアカウントのパスワードの更新を行うupdateメソッド
・引数は、更新するアカウントのid, 元のパスワードold_pw, 新しいパスワードnew_pw
・返り値は、"更新完了" or "パスワードが間違っています"
パスワードが合っていればアカウントを削除するdeleteメソッド
・引数は、id, pw
・返り値は、”削除完了” or “パスワードが間違っています”
・実行クラスの作成

Accountクラスのオブジェクトを作成
”終了”が入力されるまで,処理を繰り返す
のようなプログラムを作りたいのですがここで止まってしまっています。この先どうすればよいのでしょうか?

実行結果の目標はこちらです。

画像の説明をここに入力

現状のソースコード:

class Account:
  
    def __init__(self):
        self.__data = {'root': 'root'}

    @property
    def register(self,id,pw):
        id=input("アカウント名を入力->")
        pw=input("パスワードを入力->")
        return "登録完了"

    @property  
    def update(self,id,old_pw,new_pw):
        id=input("アカウント名を入力->")
        old_pw=input("旧パスワードを入力->")
        new_pw=input("新パスワードを入力->")
        if self.if==id and self.pw==pw:
            return "更新完了"
        else:
            return "パスワードが間違っています"
    
    @property    
    def delete(self,id,pw):
        id=input("アカウント名を入力->")
        pw=input("パスワードを入力->")
        if self.id==id and self.pw==pw:
            return "削除完了"
        else:
            return "パスワードが間違っています"
        
print("1.登録\n2.変更\n3.削除\n9.終了")
while True:
    a=input()
    if a==1:
        print(f"処理を選択->{a}")
        ac=Accoun()
        ac.register()
    
    elif a==2:
        print(f"処理を選択->{a}")
        ac=Accoun()
        ac.update()
    
    elif a==3:
        print(f"処理を選択->{a}")
        ac=Accoun()
        ac.delete()
    
    elif a==9:
        print("終了")
        break

1 件の回答 1

1

練習問題か何かに取り組んでいるのでしょうか?

細かい指摘は横において、まずはここから始められてはいかがでしょうか?
Python チュートリアル

おそらく以下のようにすれば良いとは思います。

# 予約語を変数名にするのはタブー

class user_account():
    def __init__(self):
        self.user_account = dict()

        # rootユーザーがデフォルトで必要?
        # パスワード管理に暗号化しなくてよいのか?
        def_root_password = 'root'
        self.user_account.setdefault(
            'root',
            def_root_password,
        )

    def show(self):
        print(self.user_account)

    def entry(self):
        user_id = input("アカウント名を入力->")
        # パスワードの入力を表示させるのは良いのか?
        # パスワードの再確認が必要ではないか?
        user_password = input("パスワードを入力->")

        # user_idの重複はどうする?
        self.user_account.setdefault(
            user_id, user_password
        )

        print('登録完了')

        self.show()

        return

    def update(self):
        user_id = input("アカウント名を入力->")
        # パスワードの入力を表示させるのは良いのか?
        # パスワードの再確認が必要ではないか?

        if user_id in self.user_account:
            reg_password = self.user_account[user_id]
            old_password = input("旧パスワードを入力->")
            if reg_password == old_password:
                new_password = input("新パスワードを入力->")
                self.user_account[user_id] = new_password
                print('更新完了')
            else:
                # passwordが間違っていたら?
                pass
        else:
            # user_id 登録されてなかったら?
            pass

        self.show()

        return

    def delete(self):
        user_id = input("アカウント名を入力->")
        # パスワードの入力を表示させるのは良いのか?
        # パスワードの再確認が必要ではないか?
        # rootが消えても良いか?

        if user_id in self.user_account:
            reg_password = self.user_account[user_id]
            password = input("パスワードを入力->")
            if reg_password == password:
                self.user_account.pop(user_id)
                print('削除完了')
            else:
                # passwordが間違っていたら?
                pass
        else:
            # user_id 登録されてなかったら?
            pass

        self.show()

        return


instAccount = user_account()

while True:
    print("1.登録\n2.変更\n3.削除\n9.終了")
    a = input('キー入力:')
    print(f"処理を選択->{a}")
    if a == '1':
        instAccount.entry()

    elif a == '2':
        instAccount.update()

    elif a == '3':
        instAccount.delete()

    elif a == '9':
        print("終了")
        break

この質問に回答するには、ログインする必要があります。

求めていた回答ではありませんか? のタグが付いた他の質問を参照する。