1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

DEPARTUREAdvent Calendar 2023

Day 3

色々な言語で簡単なREPLを実装する #python

Last updated at Posted at 2023-12-02

前回からの続きです。
色々な言語で簡単なREPLを実装する #ruby

REPLとは

こちらの記事を参照してください。

Pythonで実装する

def main():
    while True:
        try:
            print("python> ", end="")
            # prompt = eval(input())
            prompt = input()

            if prompt is not None:
                print(prompt)
        except KeyboardInterrupt:
            print("\nBye!")
            break
        except EOFError:
            break
        except Exception as e:
            print(f"Error: {e}")

if __name__ == "__main__":
    main()

実行する

$ python main.py
python> 1 + 1
1 + 1
python> foobarbaz
foobarbaz
python> ^C
Bye!
1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?