473,473 Members | 1,962 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

negative number problem

5 New Member
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 9102
bvdet
2,851 Recognized Expert Moderator Specialist
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: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.