I am using easygui to get a list of integers from the user. It outputs something like this:
fieldValues = [1,2]
I want to convert that list to:
var1 = 1
var2 = 2
I am using easygui to get a list of integers from the user. It outputs something like this:
fieldValues = [1,2]
I want to convert that list to:
var1 = 1
var2 = 2
(EDITED to explain my reasoning more clearly)
One reason why you might want to convert a list to variables is to make the code more self-documenting. A variable like password
is easier to understand than fieldValue[3]
, for instance.
However, as @Blender suggested, it is probably better not to assign the elements of a list to individual variables.
One way to achieve both goals would be to refer to the elements of the list by name rather than by number. In other languages, you could use an enumerated variable (e.g. an enum
in Java or C#). You could use the enum package in Python for a fully robust solution or you could do something like this:
username, password = range(2)
print(fieldValue[username])