2

I am required to write a class for Node and Binary Tree, both of which work perfectly fine. I'm also required to use a menu for the methods, when I try to add something, it adds but immediately resets to None and starts over again when add is called. This is the menu implementation.

if __name__ == '__main__':

    print("Menu \n"
        "\n"
        "1. Add item \n"
        "2. Get item \n"
        "3. Print Binary Tree in Inorder Transversal \n"
        "4. Exit \n")

    user_input = input("Please select an action from the menu: ")
    tree = BinaryTree()

    if user_input == "1":
        item = str(input("Please enter an item to add to the Binary Tree: "))
        bin_str = str(input("Please enter binary string to place in the Binary Tree: "))
        tree.add(item, bin_str)
        tree.print_inorder()

    elif user_input == "2":
        get_item = input("Please enter binary string of wanted item: ")
        get_bin_str = tree.get(get_item)
        print(get_bin_str)

    elif user_input == "3":
        tree.print_inorder())

    elif user_input == "4":
        sys.exit(0)

    else:
        print("Error: please select an action from the menu")
2
  • What do you mean 'it resets to None and starts over again' ? Commented May 23, 2016 at 16:10
  • 2
    The code you've shown will run only once irrespective of the input you provide since there is no loop. Assuming you put the loop, make sure to move the tree = BinaryTree() outside the menu as you dont want to reinitialize it everytime . Otherwise it will be losing its state. Commented May 23, 2016 at 16:30

1 Answer 1

1

As an above comment suggests, you need a while loop to allow for additional user input, without resetting the tree variable each time.

if __name__ == '__main__':

    tree = BinaryTree()
    while True:

        print("Menu \n"
            "\n"
            "1. Add item \n"
            "2. Get item \n"
            "3. Print Binary Tree in Inorder Transversal \n"
            "4. Exit \n")

        user_input = input("Please select an action from the menu: ")

        if user_input == "1":
            item = str(input("Please enter an item to add to the Binary Tree: "))
            bin_str = str(input("Please enter binary string to place in the Binary Tree: "))
            tree.add(item, bin_str)
            tree.print_inorder()

        elif user_input == "2":
            get_item = input("Please enter binary string of wanted item: ")
            get_bin_str = tree.get(get_item)
            print(get_bin_str)

        elif user_input == "3":
            tree.print_inorder())

        elif user_input == "4":
            sys.exit(0)

        else:
            print("Error: please select an action from the menu")
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.