4

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....

4
  • The code after List.append(ValueList) should be outside for loop. Commented Dec 17, 2015 at 9:46
  • where does it crash? What output do you get? Any exceptions? Commented Dec 17, 2015 at 9:46
  • This doesn't look right: value9=math.ceil(value8p1//10). Why the double slash? Commented Dec 17, 2015 at 9:48
  • // is the python integer division operator (which will round towards zero). It's output is a an integer so the ceil will do nothing Commented Dec 17, 2015 at 10:26

3 Answers 3

2

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)
Sign up to request clarification or add additional context in comments.

1 Comment

@CODE_KILLER Check this.
1

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)

Comments

1

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)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.