Variable not set in for-loop in Python

1

I'm having a problem with a code that should read 4 values using raw.input().split() and then a for-loop to transform those values into float . The interpreter returns me:

  

"name 'val' is not defined"

Follow the code:

p=raw.input().split()
for i in range (0,4)
    val[i]=float(p[i]) 
    
asked by anonymous 26.08.2016 / 00:46

1 answer

1

The code has some problems. As the self-descriptive error presented by the compiler fails to declare the val list before using it.

Maybe it's just version issue, but I used the input() function to run correctly.

: was missing at the end of the for line.

This only works under very specific circumstances, which is not a problem for an exercise, but would be in actual application. It's still not the best way to do it, but it works:

val = []
p = input('Digite uma sequência de números ').split()
for i in range(0, 4):
    val.append(float(p[i]))

See working on ideone .

    
26.08.2016 / 01:24