0

i have a code that looks as follows, my problem is that im getting the error it just returns 0 no matter what.

import sys
import math

b = [2,5,9,13]
b = [int(x) for x in b[1:]]
print(b)

pair = b[0]


i=0
x=1
y=2
while i < pair:
    print (b[x]*(100 / 180))
    i = i+1
    x = x+1

I dont understand what is wrong I have tried, going thorugh it in my head all of the processes and I cant see why it doesn't work. i think it has something to do with the way I call the numbers on my list.

rasmus393
  • 15
  • 5
  • 2
    In python 2, `100 / 180` is zero, because the result of dividing two integers is always given as another integer. Try `100.0/180` instead. – khelwood Jun 27 '16 at 09:32
  • "going thorugh it in my head all of the processes". Better: use print functions with all relevant parameters. Print i, x and b[x] in the loop, as well as the while condition (thus `print (i < pair)`), to see when it's True and when False. –  Jun 27 '16 at 09:33
  • ty very much why is it so ? – rasmus393 Jun 27 '16 at 09:33
  • If `b = [5, 9, 13]; x = 3`, you will get `IndexError: list index out of range`. `b[3]` does not exist. – debug Jun 27 '16 at 09:39
  • nvm i got it now ty – rasmus393 Jun 27 '16 at 09:44

1 Answers1

2

In Python2, 100/180 will always return 0 because it uses an integer division, and doesn't return the float result of the division. You're multiplying something 0, thus the final 0.

Use 100/180.0 or float(100)/180 to use float-division.

Zashas
  • 736
  • 4
  • 11