Reputation: 47
I am creating a script that asks the user how many data sets they would want to compare.
The user enters a number and I was wondering if there is a way, using loops, to creating a number of variables for the number the user entered.
input_SetNum = input("How many data sets are you comparing: ")
print ("You entered " + input_SetNum)
data_sets = {}
for i in range(1, input_SetNum+1):
data_sets[i] = input("Please enter the file path a data set: ")
Upvotes: 1
Views: 86
Reputation: 3803
You're definitely on the right path. Your code isn't that different than what I would generate for this same task. Most of my changes are a matter of preference, mainly that I'd use a list instead of a dictionary, since there doesn't appear to be any overwhelming reason to use a dictionary in this case; you appear to mainly be using it in the same way you'd use an array in another language, or as a list that you don't need to explicitly .append()
or .extend()
new items to.
# Python 3. For Python 2, change `input` to `raw_input` and modify the
# `print` statement.
while True: # Validate that we're getting a valid number.
num_sets = input('How many data sets are you comparing? ')
try:
num_sets = int(num_sets)
except ValueError:
print('Please enter a number! ', end='')
else:
break
data_sets = []
# Get the correct number of sets. These will be stored in the list,
# starting with `data_sets[0]`.
for _ in range(num_sets):
path = input('Please enter the file path a data set: ')
data_sets.append(path)
If you entered 3
for the number of sets, this would set data_sets
equal to
['data_set_0', 'data_set_2', 'data_set_3']
for the appropriate input.
Upvotes: 0
Reputation: 3651
You could use a dictionary.
data_sets = {}
for i in range(1, input_SetNum+1):
data_sets[i] = # data set value here
Edit:
If you are using Python 3, then you full code should be:
input_SetNum = input("How many data sets are you comparing: ")
print ("You entered " + input_SetNum)
data_sets = {}
for i in range(1, int(input_SetNum)+1):
data_sets[i] = input("Please enter the file path a data set: ")
print(data_sets)
Printing data_sets
will produce this result when 3 is inputted:
{1: '/path/file1', 2: '/path/file2', 3: '/path/file3'}
If you are using Python 2.7, then you should replace all of the input()
s with raw_input
s.
Edit 2:
To open CSV files based on their paths, you can use code like this under what you've already done.
for key in data_sets:
with open(data_sets[key]) as current_file:
# do stuff here
It may also be possible to instead use open()
on the input()
you used before for the file path.
data_sets[i] = open(input("Please enter the file path a data set: "))
I am not 100% sure if this will work, as I am not very familiar with CSV files, but it can't hurt and if it does work, it would be easier to compare the data sets.
Upvotes: 2
Reputation: 13216
You can define variables in the workspace using
for i in input_SetNum:
vars()["var%s=value" % (i)] = value
You will probably want to put this inside a class in the long term, where the variables for the class are defined by,
class SomeObj:
def __init__(self, input_SetNum, values):
for i in input_SetNum:
vars(self)["var%s=value" % (i)] = values[i]
Upvotes: 0