# New to Python

Discussion in 'Python' started by w, Sep 21, 2004.

1. ### wGuest

excuse my ignorance. I cannot get this simple program to calculate
the formula.

The error message is unsupported operand type(s) str. I know the data
that I'm reading from the file is a string but I cannot convert it to
a float or int.

The code a sample form the data file are below.

# Calculate Gross Profit from SIL File

import os.path

in_file = open( "s3cga1.new", "r")

while 1:
if not data:
break

if data[0:1] == "(":

nCost = data[ 99:106]
nPack = data[ 44:48]

nRetail = data[ 79:87]
nQuan = data[ 87:90]
nAllowance = data[ 147:154]

nGp = ( ( nRetail / nQuan ) - ( ( nCost - nAllowance) / nPack ) ) / (
nRetail / nQuan )

print nCost, nPack, nQuan, nRetail, nAllowance

EXAMPLE OF DATA FILE
(00007756725434,'*BREYER STRWBRY ICE ',0007,0006,' 56 OZ ','000216
',0004.95,001,2004260,0024.43000,0000000,00000.00,000,0000000,0000000,0004.00000,2004262,2004282),
(00007756725433,'*BREYER PEACH ICE CR',0007,0006,' 56 OZ ','000224
',0004.95,001,2004260,0024.43000,0000000,00000.00,000,0000000,0000000,0004.00000,2004262,2004282),

w, Sep 21, 2004

2. ### Thomas =?ISO-8859-1?Q?Kr=FCger?=Guest

x = int(y)
or
x = float(y)
???

Thomas

Thomas =?ISO-8859-1?Q?Kr=FCger?=, Sep 21, 2004

3. ### Jimmie WebbGuest

I tried x = float(nCost) but keep getting an error

ValueError: Invalid literal for float(): EATE TA

Jimmie Webb, Sep 21, 2004
4. ### Marc 'BlackJack' RintschGuest

Maybe the slice values are just wrong? Have you printed all your
variables after picking them from the string to make sure they contain
what you expect?

Ciao,
Marc 'BlackJack' Rintsch

Marc 'BlackJack' Rintsch, Sep 21, 2004
5. ### Jimmie WebbGuest

Yes, They look good to me. The nCost Value is 0022.63.
but I still get the error ValueError: Invalid literal for float(): EATE TA.

What does the 'EATE TA' mean?

Thanks!

Jimmie Webb, Sep 21, 2004
6. ### Jere KahanpaaGuest

Come on, do some basic debugging! Insert 'print repr(nCost)' before the
conversation and look at what you really are feeding to the float()
function. Well, we already now it: at some point you feed the string 'EATE
TA' to float(), and it obviously cannot convert this to a number.

Jere

Jere Kahanpaa, Sep 21, 2004
7. ### Lee HarrGuest

Put your print statement _before_ you do any calculations, so you
can see what you are trying to calculate with ...

Lee Harr, Sep 21, 2004
8. ### Peter HansenGuest

Traceback (most recent call last):
Traceback (most recent call last):
Traceback (most recent call last):
File "<stdin>", line 1, in ?
ValueError: invalid literal for float(): check your data file and

-Peter

Peter Hansen, Sep 21, 2004
9. ### Jimmie WebbGuest

Gee whiz. Don't jump down my throat for some oversite. It's not like I
haven't put forth an effort trying to find out the problem before posting!
Yeah, your right I was reading the "CREATE TABLE line out of the SIL file.
Thanks (I think)

Jimmie Webb, Sep 21, 2004
10. ### Jimmie WebbGuest

group. Glad to know there are some people out there that can be "friendly"
and "helpful". It is nice to know some people will help and not call you
lazy or stupid. Sorry, I just felt like I walked into the middle of a
pit-bull attack.

-Jimmie Webb

',0004.95,001,2004260,0024.43000,0000000,00000.00,000,0000000,0000000,0004.0
0000,2004262,2004282),

Jimmie Webb, Sep 23, 2004