I'm new at Python coding and in DaniWeb. Really nice forum :icon_cheesygrin: Need some help w/ the following code:

the content of saldo.txt is the following

``1000``

The code I was talkin' about:

``````# [...]

f = open('C:/saldo.txt')
print f
f.close()
if saldo <= 100:
a = 5
b = 70
if 1000 >= saldo > 100:
a = 50
b = 700
if 1000 < saldo < 2500:
a = 100
b = 1000
if 2500 <= saldo < 5000:
a = 250
b = 2000
if 5000 <= saldo < 10000:
a = 500
b = 5000
if 10000 <= saldo < 25000:
a = 1000
b = 7500
if 25000 <= saldo < 50000:
a = 2500
b = 10000
if 50000 <= saldo < 100000:
a = 5000
b = 10000
if saldo >= 100000:
a = 1
b = 1000000000
print (a, b)

# [...]``````

My problem is:

"a" is being printed as 1 instead of 50
"b" is being printed as 1000000000 instead of 700

:confused:

5
Contributors
5
Replies
6
Views
7 Years
Discussion Span
Last Post by snippsat

A string is always larger than a number (any number). Better re-read your other thread and if you are not sure, print type() for both variables.

Also, all this should be written using module bisect:

``````from bisect import bisect
limits = [100, 1000, 2500, 5000, 10000, 25000, 50000, 100000]
pairs = [
(5, 70), (50, 700), (100, 1000), (250, 2000),
(500, 5000), (1000, 7500), (2500, 10000), (5000, 10000),
(1, 1000000000)
]

a, b = pairs[bisect(limits, saldo)]``````

In the mean time changing
to
will work

In the mean time changing
to
will work

Got it. I'm not used to Python. In some other languages the content of the file (1000) would be a numeric value, not a string. Thanks

Edited by Hawkeye Python: n/a

In some other languages the content of the file (1000) would be a numeric value, not a string. Thanks

In many other lanuages you need to declare vaiables.
In python there is no need for this,but off course datatypes is always there.

C++
int age;
age = 30

Python
age = 30

Test in IDLE.

``````>>> age = 30
>>> type(age)
<type 'int'>
>>>``````

So do this if you not sure of datatype.

``````>>> f = open('C:/saldo.txt')
>>> saldo
'100'
>>> print saldo
100
>>> type(saldo)
<type 'str'>

>>> if saldo > 1000:
print True
else:
print False
True
#As you see this is worng
#Because saldo is a string

>>> if int(saldo) > 1000:
print True
else:
print False
False``````