0

私はしばらくここを見回してきましたが、何もうまくいきませんでした。別の関数で使用するために、ある関数から入力変数を呼び出そうとしています。以下のコード (具体的には number1、number2、name1、および name2)。

これは Python 3.3.2 です。これが非常に基本的な質問である場合は申し訳ありません。

import os import sys ##Folder making program. def main(): print("Follow the instructions. The output will be put in the root directory (C:/)") var = input("Press enter to continue") print("What do you want to do?") print("1. Create a folder with a series of numbered subfolders") print("2. Create a folder that contains subfolders with unique names") print("3. Exit the program.") input1 = input(": ") if input1 == '1': program1() elif input1 == '2': program2() elif input1 == '3': sys.exit def program1(): print("Input name of the main folder") name1 = input(": ") if name1 == "": print("The main folder needs a name.") program1() print("Input the name of sub folders") name2 = input(": ") series() def program2(): print("Input name of the main folder") name1 = str(input(": ")) if name1 == "": print("The main folder needs a name.") program2() print("Input the name of sub folders.") name2 = str(input(": ")) creation2() def series(): print("Put STARTING number of subdirectories:") ##code that will repeat this prompt if the user puts in a string. Accepts answer if input is an integer while True: try: number1 = int(input(": ")) break except ValueError: print ("invalid"); print("confirmed") print("Put ENDING number of subdirectories:") ##code that will repeat this prompt if the user puts in a string. Accepts answer if input is an integer while True: try: number2 = int(input(": ")) break except ValueError: print ("invalid"); total = number2 - number1 ##Makes sure the values work, restarts this section of the program program if values are 1 or less than 0 if total < 0: print('be reasonable') series() if total == 0: print('be reasonable') series() if total == 1: print("make the folder yourself") series() print("confirmed") while number1 <= number2: path = "c:\\" name3 = name2 + ' ' + str(number1) ##puts path to root directory ##makes main folder ##makes sub folders in main folder. Subfolders are named "Name2 1", "Name2 2", etc ##os.makedirs(path + name1) os.makedirs(path + name1 + '\\' + name3) number1 = number1 + 1 contains = str('has been created, it contains') containstwo = str('subdirectories that are named') print((name1, contains, total, containstwo, name2)) menu def again(): while True: print("Input the name of a new subfolder") newname = input(": ") os.makedirs(path + name1 + '\\' + newname) if newname == "": again() if newname == "1": main() print("To make another folder, input the name. To return to the menu, input 1 .") def creation2(): path = "c:\\" ##puts path to root directory ##makes main folder ##makes one subfolder in main folder, the subfolder has is name2. os.makedirs(path + name1 + '\\' + name2) contains = str('has been created, it contains') print((name1, contains, name2)) print("Make another named folder within the main folder? (y/n)") another = input(":") while another == "y": again() if restart == "n": menu() main()

4

2 に答える 2