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

1

你不调用变量,你调用函数。

您想要做的是从您的函数返回值。然后,您可以将这些值存储在变量中或将它们传递给另一个函数。

例如:

def get_numbers():
    x = int(input("Enter value of X: "))
    y = int(input("Enter value of Y: "))
    return x, y

def add_numbers(x, y):
    return x+y

x, y = get_numbers()
sum = add_numbers(x, y)
print("The total is:", sum)

由于我们只使用全局变量xy(函数外部的变量)来保存返回的值,get_numbers()直到它们被传递给add_numbers(),我们可以消除这些变量,并将返回的值get_numbers()直接传递给add_numbers()

sum = add_numbers(*get_numbers())

前面的星号get_numbers()告诉 Python 该函数返回多个值,并且我们希望将这些作为两个单独的参数传递给add_numbers()函数。

现在我们看到了同样的事情sum——它只是用来保存数字,直到我们打印它。所以我们可以将整个事情组合成一个语句:

print("The total is:", add_numbers(*get_numbers()))

您已经对此很熟悉了——int(input(": "))毕竟您的代码中有。input()返回字符串,然后将其传递给int(). 您编写的函数的工作方式与 Python 中内置的函数完全相同。

现在,像这样在彼此内部编写函数调用有点令人困惑,因为它们是通过最里面的调用执行的get_numbers(),首先,然后add_numbers(),最后print()——与它们的编写方式相反!因此,使用临时变量以便您可以按照实际执行的顺序编写代码是有意义的。

于 2013-06-25T22:09:47.153 回答
0

我会听从kindall的建议并选择重写;重写可能会更具可读性和更短。但是,如果您想使用现有的一些代码,请考虑以下事项:

举个program1()例子。在program1()中,您定义一个name1变量和一个name2变量。这些变量只存在于 的定义中program1()。如果您想要另一个函数,特别series()是使用变量,则需要重新构造series()以接受参数。例如:

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(name1, name2)

def series(name1, name2):
    ...

这样,您可以将一个函数中定义的变量传递给另一个函数。对所有方法执行此操作可能有助于缓解您遇到的一些问题。

于 2013-06-26T14:59:18.750 回答