1

I am trying to input a number, and will create x amount of declared empty lists.

Ex: Input number >> 3
Result:
1 = []
2 = []
3 = []

So I am thinking convert the input str into int, then insert the int value into range (x). Then do a loop that can create x amount of number with '= []'. In my final work, I would want to take out the data stored in these list into different fields.

Below is code:

number = input("Input number")
number = int(number)

x = range (number)
for y in x.attributes:
    str(y) = []

#a loop that insert values in list into fields
driver.find_element_by_name('Name').send_keys(1[0])
driver.find_element_by_name('GRADE').send_keys(1[1])
driver.find_element_by_name('DATE').send_keys(1[2])

Error msg:
str(y) = []
^
SyntaxError: cannot assign to function call
I think declared value should str, so I have str(y) there.

Appreciate the help with some short explain or what kind of guide i should look for. Thanks

SCH
  • 83
  • 1
  • 1
  • 8

1 Answers1

2

Values (such as 3, "hello", True, [], etc) are referenced by variables, which have a name. For example:

a = 4

a is a variable (whose name is "a"), and it references the 4 value.

Not all names are valid though. For example, "1" is not a valid name for a variable. How could the interpreter know that 1 is a variable and not a value?

Moreover, you can't assign a value to another value. I.e., you can't do something like this:

4 = True

It simply doesn't make any sense.

In your specific case, str(y) is an expression that returns a value. For example, if y=3 then str(y) would return "3". Being a value, you can't assign to it another value! I.e., you can't do

str(y) = []

Instead, you might want to create a variable (with a proper name) and assign a value to it.

If you really want to create a variable with a name generated at runtime, you might want to look at this question. It is not recommended though. Instead, you might want to create an array of values:

arr = []
for i in range(100):
    arr.append([])
# arr[i] is your i-th variable
Riccardo Bucco
  • 13,980
  • 4
  • 22
  • 50