我想创建一个简单的数组程序并打印出数组元素,但在我输入员工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