472,974 Members | 1,961 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,974 software developers and data experts.

negative number problem

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
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')
  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

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

Similar topics

25
by: Jason | last post by:
Hi, below is example code which demonstrates a problem I have encountered. When passing a number to a function I compare it with a string's size and then take certain actions, unfortunately during...
16
by: JKop | last post by:
Take a class like the following: class Finger { public: double length; }
13
by: Ron | last post by:
Hi all I'm deciding whether to use the PK also as an account number, invoice number, transaction number, etc that the user will see for the respective files. I understand that sometimes a...
5
by: Subrahmanyam Arya | last post by:
Hi Folks , I am trying to solve the problem of reading the numbers correctly from a serial line into an intel pentium processor machine . I am reading 1 byte and 2byte data both positive...
15
by: jaks.maths | last post by:
How to convert negative integer to hexadecimal or octal number? Ex: -568 What is the equivalent hexadecimal and octal number??
39
by: Frederick Gotham | last post by:
I have a general idea about how negative number systems work, but I'd appreciate some clarification if anyone would be willing to help me. Let's assume we're working with an 8-Bit signed integer,...
3
by: Steven D'Aprano | last post by:
Problem: I have an application where I need to print integers differently depending on whether they are positive or negative. To be more specific, I have to print something that looks like: ...
20
by: Casey | last post by:
Is there an easy way to use getopt and still allow negative numbers as args? I can easily write a workaround (pre-process the tail end of the arguments, stripping off any non-options including...
2
by: majie30 | last post by:
Write this program using functions. Write a program that accepts any number from the keyboard and tells you whether it is a nonnegative integer. The number should be sent to the function int_test(...
0
lee123
by: lee123 | last post by:
i am making a program that is about algebra and i want to know how can i get it to put negative and positive numbers in a text box for example if i had a problem that is: -6(2x + 10) = -48, ...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.