我剛學了python,我想看看有什么可能。當我制作這個測驗腳本時,代碼出錯了。
class test():
list = {}
value = 0
def __init__(self, *args) -> None:
y = 0
self.value = test.value
test.list[self.value] = []
for x in args:
self.x = y
y = 1
test.list[self.value].append(x)
test.value =1
print (test.list[self.value])
for i in test.list[self.value]: print (self.i)
a = test("a", "b", "c", "d")
此代碼應輸出:
["a", "b", "c", "d"]
0
1
2
3
但相反,我收到一條錯誤訊息,提示“AttributeError 'test' 物件沒有屬性 'i'”。我如何讓代碼通過 a 的所有屬性并輸出它的值。
uj5u.com熱心網友回復:
我猜錯誤發生在這里:
for i in test.list[self.value]: print (self.i)
應該:
for i in test.list[self.value]: print (i)
更新
在此示例中,您首先獲得兩個數字,然后是字母:
class test():
def __init__(self, *args) -> None:
my_list = []
value = 0
y = 0
value = 0
for x in args:
my_list.append(x)
print(y)
y = 1
print (my_list)
for i in my_list: print (i)
a = test("a", "b", "c", "d")
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/435234.html
標籤:Python python-3.x 列表 目的
上一篇:匯總使用樣本
下一篇:如何將元組串列轉換為矩陣?
