我想創建一個簡單的數組程序並打印出數組元素,但在我輸入員工2的值後說:IndexError:list assignment index out of range。列表分配索引超出範圍錯誤
#Create constant for the number of employees.
SIZE = 3
#Create an array to hol the number of hours worked by each employee.
hours = [SIZE]
#Get the hours worked by employee 1.
hours[0] = int(input("Enter the hours worked by employee 1: "))
#Get the hours worked by employee 2.
hours[1] = int(input("Enter the hours worked by employee 2: "))
#Get the hours worked by employee 3.
hours[2] = int(input("Enter the hours worked by employee 3: "))
#Display the values entered.
print("The hours you entered are:")
print(hours[0])
print(hours[1])
print(hours[2])
你沒有設置任何尺寸的小時= [SIZE],你的列表只有一個索引 – PRMoureu
'[SIZE]'是一個1元素的列表,其唯一的元素是3號。 – user2357112