472,974 Members | 1,961 Online

# negative number problem

so heres my code

Expand|Select|Wrap|Line Numbers
1. input = open('DATA1.txt', 'r')
2. output = open('OUT1.txt', 'w')
4. for x in range(1):
5.     set = numbers[x]
6.     a = float(set[6])
7.     b = float(set[4])
8.     c = float(set[2])
9.     d = float(set[0])
10.     r = (a - b)**2 + (c - d)**2
11.     answer = round(r * 3.14169,3)
13. output.close()
14.
and heres what data1.txt has
Expand|Select|Wrap|Line Numbers
1. 1.312 2 3 4
2.
What my problem is if I change the 1.312 to a negative 1.312 it errors out and gives me

Expand|Select|Wrap|Line Numbers
1. Traceback (most recent call last):
2.   File "circle.py", line 6, in <module>
3.     a = float(set[6])
4. ValueError: empty string for float()
5.
It seem to happen to both negative integer and float numbers.
Nov 26 '07 #1
1 9023
bvdet
2,851 Expert Mod 2GB
so heres my code

Expand|Select|Wrap|Line Numbers
1. input = open('DATA1.txt', 'r')
2. output = open('OUT1.txt', 'w')
4. for x in range(1):
5.     set = numbers[x]
6.     a = float(set[6])
7.     b = float(set[4])
8.     c = float(set[2])
9.     d = float(set[0])
10.     r = (a - b)**2 + (c - d)**2
11.     answer = round(r * 3.14169,3)
13. output.close()
14.
and heres what data1.txt has
Expand|Select|Wrap|Line Numbers
1. 1.312 2 3 4
2.
What my problem is if I change the 1.312 to a negative 1.312 it errors out and gives me

Expand|Select|Wrap|Line Numbers
1. Traceback (most recent call last):
2.   File "circle.py", line 6, in <module>
3.     a = float(set[6])
4. ValueError: empty string for float()
5.
It seem to happen to both negative integer and float numbers.
Don't use set as a variable name because you will mask Python's built in set() function. Let's call it item. item is the first element in a list of one element. You are trying to apply float() to a minus sign because you are slicing the string "-1.312 2 3 4". You should split the string, then process the four numbers in the resulting list.
Expand|Select|Wrap|Line Numbers
1. input = open('data.txt', 'r')
2. output = open('output.txt', 'w')
4. for item in numberList:
5.     items = item.split()
6.     a = float(items[3])
7.     b = float(items[2])
8.     c = float(items[1])
9.     d = float(items[0])
10.     r = (a - b)**2 + (c - d)**2
11.     answer = round(r * 3.14169,3)