Assigning values to variables in a list using a loop

前端 未结 5 844
醉梦人生
醉梦人生 2020-11-28 12:15
var_list = [one, two, three]
num = 1
for var in var_list:
    var = num
    num += 1

The above gives me an error that \'one\' doesn\'t exist. Can y

5条回答
  •  甜味超标
    2020-11-28 12:56

    A variable doesn't exist until you create it. Simply referencing a variable isn't enough to create it. When you do [one, two, three] you are referencing the variables one, two and three before they are created.

提交回复
热议问题