Reputation: 1
I've been looking around here for a while, but nothing has worked. I'm trying to call input variables from one function for use in another function. In the code below (specifically number1, number2, name1, and name2).
This is Python 3.3.2. I apologize if this is a very basic question.
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()
Upvotes: 0
Views: 285
Reputation: 18521
I would follow kindall's advice and opt for a rewrite; the rewrite would probably be much more readable and shorter. However, if you want to use some of the existing code you have now, consider the following:
Take program1()
for example. In program1()
, you define a name1
variable and a name2
variable. These variables only exist in the definition of program1()
. If you would like another function, specifically series()
, to use the variables, you need to restructure series()
to accept parameters. For example:
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):
...
This way you can pass on variables defined in one function to another function. Doing this for all of your methods may help alleviate some of the problems you are having.
Upvotes: 0
Reputation: 184091
You don't call variables, you call functions.
What you want to do is return values from your function. You can then store these values in variables or pass them to another function.
For example:
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)
Since we are only using the global variables x
and y
(the ones outside the function) to hold the values returned by get_numbers()
until they are passed to add_numbers()
, we could just eliminate those variables, and pass the values returned by get_numbers()
directly to add_numbers()
:
sum = add_numbers(*get_numbers())
The asterisk in front of get_numbers()
tells Python that this function returns more than one value, and that we want to pass these as two separate parameters to the add_numbers()
function.
Now we see the same thing is true of sum
-- it`s only used to hold the number until we print it. So we can combine the whole thing into one statement:
print("The total is:", add_numbers(*get_numbers()))
You're already familiar with this -- you have int(input(": "))
in your code, after all. input()
returs the string, which is then passed to int()
. Functions you write work exactly the same way as ones that are built into Python.
Now, writing the function calls inside each other like this gets little confusing because they are executed with the innermost call, get_numbers()
, first, then add_numbers()
, and finally print()
-- the opposite of the way they are written! So using the temporary variables so you can write the code in the order it will actually be executed can make sense.
Upvotes: 1