Day 122コマンド履歴をスタックで管理するCLI

2026-06-26 JST ・ 難易度: 実用 ・ カテゴリ: 実用データ構造

Pythonコード

1class CLI:2    def __init__(self):3        self.history = []4 5    def run(self):6        while True:7            try:8                command = input("コマンドを入力してください: ")9                self.history.append(command)10                print("コマンド履歴: ", self.history)11            except Exception as e:12                print("エラーが発生しました: ", str(e))13 14    def get_history(self):15        return self.history16 17def main():18    cli = CLI()19    cli.run()20 21if __name__ == "__main__":22    main()

解説

次に試してみよう