Как выполнить это вложенное при истинном l oop? - PullRequest
0 голосов
/ 31 марта 2020
def add(fm_list):
    while True:  
        msg = input("What do you want to create?: "
                    "\n Insert 1 for Folder "
                    "\n Insert 2 for Subfolder.\n")
        if msg == "1":
            folder = input("Name of the folder: ")
            choice = input("Do you want to add a subfolder to this folder?: (y/n): ")

            if choice.lower() == "y":
                # ... do things ...
                break
            elif choice == "n":
                # ... do things ...
                break
            else:
                print("Please choose between (y/n)\n")

        elif msg == "2":
            store = input("Where do you want to store your subfolder?: "
                          "\n Insert A to store it in the DEFAULT folder"
                          "\n Insert B to store it in a new folder\n")
            if store.lower() == "a":
                # do things ...
                break
            elif store.lower() == "b":
                # do things ...
                break
        else:
            print("Invalid entry!!!\n")
            continue

Заранее спасибо за ответы. У меня есть моя функция add() здесь, я хотел бы, когда я попадаю на сцену elif msg == 2 ... когда пользователь вводит что-то еще, кроме доступных опций (a или b), тогда он получает запрос назад, чтобы выбрать подходящий опция (Другими словами, я снова даю руку и спрашиваю пользователя, где хранить подпапку) ... вместо этого он запрашивает обратно в начале кода.

msg = input("What do you want to create?: "
                    "\n Insert 1 for Folder "
                    "\n Insert 2 for Subfolder.\n")

... Спасибо

1 Ответ

0 голосов
/ 01 апреля 2020

Ну, через некоторое время и некоторой помощи, я наконец-то смог сделать то, что хотел, мы создали рекурсивную функцию (не уверен, что это она называется tho), а затем вызвали ее позже с помощью функции add: ....

        def getstore(fm_list):
            store = input("Where do you want to store your subfolder?: "
                          "\n Insert A to store it in the DEFAULT folder"
                          "\n Insert B to store it in a new folder\n")
            if store.lower() == "a":
                subfolder = input("What is the name of the subfolder: ")
                fm_list.append(subfolder)
                print("Your subfolder will be store to the Default folder: ")
                os.chdir('F:\\Test\\Default')  # this is the Default folder already in the test directory
                os.makedirs(subfolder, exist_ok=True)
                print(subfolder + " was added to the folder named Default")
                quit()
            elif store.lower() == "b":
                folder = input("Name of the folder: ")
                fm_list.append(folder)
                os.chdir('F:\\Test')
                os.makedirs(folder, exist_ok=True)
                subfolder = input("Name of the subfolder: ")
                fm_list.append(subfolder)
                os.chdir('F:\\Test\\' + folder)
                os.makedirs(subfolder, exist_ok=True)
                print(folder + " was added to the list of folder.")
                print(subfolder + " was added to " + folder)
                quit()
            else:
                getstore(fm_list)

потом, когда мы доберемся до elif msg == "2", мы называем getstore(fm_list) просто так ...

 elif msg == "2":
                getstore(fm_list)
                break
            else:
                print("Invalid entry!!!\n")
                continue

И все.

...