Hi everyone,

I'm new to posting, but I've been coming here to search for help for some time now. I'm working on some homework and have come to an impasse. I'm not here for straight-up solutions, just a little help, as I've already got the program 99% complete. In the program I'm trying to scale a list of numbers, but my method for identifying the maximum element is obviously flawed. Could someone help me spot why this program does not think that 100 is greater than 95?

f=open('list.txt','r')

numlist=f.readlines()

len = len(numlist)

max = (numlist[0])
min = (numlist[0])

print "original list: "
for n in range(1,len):
        print numlist[n]
        if numlist[n]>max:
                max=(numlist[n])
        if numlist[n]<min:
                min=(numlist[n])

newlist = []

for n in range(0,len):
        x = float((int(numlist[n]) - float(min))/(int(max)-float(min)))
        newlist.append(x)
print "min: "
print min
print "max: "
print max
print "scaled list: "
print newlist

The "list.txt" file is just:

80
0
60
25
50
70
75
100
90
95

Thanks in advance!

Dani AI

Generated

had the right idea but a few other things in the script make it fragile even after the comparison issue is fixed. correctly pointed to the immediate cause; the following recommendations harden the code and avoid common gotchas when reading numeric data from a text file.

  • Read and convert once: open the file with a context manager and convert lines to numbers as you read them (use strip() to remove newlines and skip blank lines).
  • Do not shadow built-ins: avoid variable names like len, min, or max — use count, min_val, max_val instead. Shadowing hides the standard functions and causes confusing errors.
  • Use the built-in min() and max() instead of a manual loop. That is clearer and less error-prone.
  • Protect against edge cases: handle empty files and the case max_val == min_val (division by zero when scaling).
  • Iteration: iterate directly over the list instead of indexing with range(1, len) (that skips the first element in the original code).

A minimal, robust approach (Python 3) looks like this:

with open('list.txt') as f:
    nums = [int(line.strip()) for line in f if line.strip()]

if not nums:
    raise SystemExit('no numbers found')

min_val, max_val = min(nums), max(nums)

if max_val == min_val:
    scaled = [0.0 for _ in nums]
else:
    scaled = [(x - min_val) / float(max_val - min_val) for x in nums]

print('min:', min_val)
print('max:', max_val)
print('scaled list:', scaled)

For reference on file reading, list comprehensions and the built-in functions used above, see the Python docs: Reading and writing files, List comprehensions, and the min() and max() built-ins.

Recommended Answers

All 2 Replies

The numbers come from the file as strings, not integers. Strings sort left to right so
100 --> 1 & (00) is less than
95 --> 9 & (5)
Hopefully this bit of code will help

test_int_list = [
80,
0,
60,
25,
50,
70,
75,
100,
90,
95 ]

test_str_list = [str(num) for num in test_int_list]
test_str_list.sort()
print "As strings", test_str_list
test_int_list.sort()
print "As integers", test_int_list

Thanks! I was so confused, and now it seems so obvious!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.