简体   繁体   中英

How to enter commands repeatedly?

I've got some problems while implementing a BST. The problem is that I have the 'insert' function that I can insert the nodes in the tree. What I want to do is that I want to type commands in the command line in the form of python3 main.py 4 2 9 the three numbers are the nodes I want to insert in order. Next, I want to type python3 main.py 5 3 8 , the three numbers are nodes I want to insert in the previous BST I bulit, which has 4,2,9 in it.

However, every time I execute the python script, it will lose the tree before and build a new one, indicating that I always have three nodes in the BST and it is not what I want. I want a growing tree but I can't think of a way to keep typing commands in the command line before I exit the terminal, I've checked for the argparse module but it only tells me how to parse the argument in the command line, still missing the 'keep typing' part, can anyone tells me a way to do this or is it poosible?

If you want your data structure to persist through sessions, then you must store it in some fashion. An easy way is to minify the BST and then use the pickle module to store it. Otherwise, modify your program to accept input over the course of its lifetime, not only as command line input.

Instead of taking command line arguments, you could try using the input() function, which takes user input and can store it in a variable. For documentation on how to use it, go here .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM