CODE_KILLER
CODE_KILLER

Reputation: 41

Python list error, list index out of range

I don't understand what i have done wrong!

x=7
List=[]
for i in range(0,x):
    verify=False
    while verify==False:
        Values=input("Enter Code")
        verify=Values.isdigit()

    ValueList=int(Values)
    List.append(ValueList)
    value1=int(List[0])
    value2=int(List[1])
    value3=int(List[2])
    value4=int(List[3])
    value5=int(List[4])
    value6=int(List[5])
    value7=int(List[6])
    value8p1=(value1*1)+(value2*3)+(value3*1)+(value4*3)+(value5*1)+(value6*3)+(value7*1)
    value9=math.ceil(value8p1//10)
    print(value9)

My Teacher doesn't know either, I feel like it's something stupid....

Upvotes: 4

Views: 554

Answers (3)

JDurstberger
JDurstberger

Reputation: 4255

If I enter 1234 then Values contains the string "1234".

You then cast the string to an integer

ValueList=int(Values) # converts string "1234" to integer 1234

And add the integer to the empty list

List.append(ValueList)# List not looks like this List=[1234]

So your list only contains one element.
Therefore List[1] is out of range

btw: Do not name your values after built in names like list.
Please see the official naming conventions.

Edit

When you correct your indentation your code is still dangerous.
if you change x to something < 7 your code will crash.

Still dangerous code

x=7 
List=[]
for i in range(0,x):
    verify=False
    while verify==False:
        Values=input("Enter Code")
        verify=Values.isdigit()

    ValueList=int(Values)
    List.append(ValueList)

value1=int(List[0])
value2=int(List[1])
value3=int(List[2])
value4=int(List[3])
value5=int(List[4])
value6=int(List[5])
value7=int(List[6])
value8p1=(value1*1)+(value2*3)+(value3*1)+(value4*3)+(value5*1)+(value6*3)+(value7*1)
value9=math.ceil(value8p1//10)
    print(value9)

So you should make your calculation in a loop to avoid index errors:

updated code

x = 4
values = []
for i in range(0, x):
    verify = False
    while verify == False:
        value = input("Enter Code")
        verify = value.isdigit()

    values.append(int(value))

result = 0
for index, value in enumerate(values):
    if index % 2 == 0:
        result += value * 3
    else:
        result += value

finalResult = math.ceil(result // 10)
print(finalResult)

Upvotes: 1

Pradeep
Pradeep

Reputation: 1398

Here's the working code:

import math

x=7
List=[]
for i in range(0,x):
    verify=False
    while verify==False:
        Values=raw_input("Enter Code")
        verify=Values.isdigit()
        print verify

    ValueList=int(Values)
    List.append(ValueList)
value1=int(List[0])
value2=int(List[1])
value3=int(List[2])
value4=int(List[3])
value5=int(List[4])
value6=int(List[5])
value7=int(List[6])
value8=(value1*1)+(value2*3)+(value3*1)+(value4*3)+(value5*1)+(value6*3)+(value7*1)
value9=math.ceil(value8//10)
print(value9)

Upvotes: 1

Kenly
Kenly

Reputation: 26758

It's just an indentation problem.

import math


x=7
List=[]
for i in range(0,x):
    verify=False
    while verify==False:
        Values=input("Enter Code")
        verify=Values.isdigit()

    ValueList=int(Values)
    List.append(ValueList)
value1=int(List[0])
value2=int(List[1])
value3=int(List[2])
value4=int(List[3])
value5=int(List[4])
value6=int(List[5])
value7=int(List[6])
value8p1=(value1*1)+(value2*3)+(value3*1)+(value4*3)+(value5*1)+(value6*3)+(value7*1)
value9=math.ceil(value8p1//10)
print(value9)

Upvotes: 2

Related Questions