By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,922 Members | 1,689 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,922 IT Pros & Developers. It's quick & easy.

negative number problem

P: 5
so heres my code

Expand|Select|Wrap|Line Numbers
  1. input = open('DATA1.txt', 'r')
  2. output = open('OUT1.txt', 'w')
  3. numbers = input.readlines()
  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)
  12.     output.writelines('%s\n' % answer)
  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
Share this Question
Share on Google+
1 Reply


bvdet
Expert Mod 2.5K+
P: 2,851
so heres my code

Expand|Select|Wrap|Line Numbers
  1. input = open('DATA1.txt', 'r')
  2. output = open('OUT1.txt', 'w')
  3. numbers = input.readlines()
  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)
  12.     output.writelines('%s\n' % answer)
  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')
  3. numberList = input.readlines()
  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)
  12.     output.writelines('%s\n' % answer)
  13. output.close()
Nov 27 '07 #2

Post your reply

Sign in to post your reply or Sign up for a free account.