2

I have a text file consisting of some stocks and their prices and what not, I am trying to print out the stock which has the lowest value along with the name of the company here is my code.

stocks = open("P:\COM661\stocks.txt")

name_lowest = ""
price_lowest = 0

for line in stocks:
    rows = line.split("\t")
    price = float(rows[2])
    if price>price_lowest:
        price_lowest = price
        name_lowest = rows[1]
print(name_lowest + "\t" + str(price_lowest))

I'm trying to go through the file and compare each numeric value to the one before it to see if it is higher or lower and then at the end it should have saved the lowest price and print it along with the name of the company. Instead it prints the value of the last company in the file along with its name.

How can I fix this?

Maroun
  • 94,125
  • 30
  • 188
  • 241
tom feore
  • 11
  • 1
  • 2
    Please post a couple lines from the text file, it's hard to help you without seeing the data you're working with. – Jared Smith Oct 16 '18 at 11:00
  • 3
    Shouldn't `if price>price_lowest:` be a `<` instead? Using more precise names like `current_price` instead of `price` might help you in the future. – Maroun Oct 16 '18 at 11:01
  • You are checking if the price from the file is `>` than your current minimum. So this could should print the largest value from the file. – Moberg Oct 16 '18 at 11:02
  • Sounds like a job for `min()`, `max()` and `sort()`? Also `sorted()` with `itemgetter`: https://stackoverflow.com/questions/613183/how-do-i-sort-a-dictionary-by-value – Torxed Oct 16 '18 at 11:02
  • Shouldn't the `print` be inside of the `if`? – Chris Oct 16 '18 at 11:03
  • 1
    @Chris No. OP wants to print the lowest, in the whole file. – Maroun Oct 16 '18 at 11:03
  • @Maroun My bad :P. The posted answer should solve the problem – Chris Oct 16 '18 at 11:05

5 Answers5

2

You made 2 mistakes. First is initialised the initial value to 0 You should initialise the initial value to the max available number in python float.

import sys
price_lowest  = sys.float_info.max

Or else you could initialise it to the first element

Second your should if statement should be

if price<price_lowest:
Arghya Saha
  • 5,599
  • 4
  • 26
  • 48
1

Initialize:

price_lowest = 999999  # start with absurdly high value, or take first one  

Plus your if check is the opposite.
Should be:

if price < price_lowest
Vikrant Sharma
  • 419
  • 3
  • 6
1

Others already suggested a solution that fixes your current code. However, using Python you can have a shorter solution:

with open('file') as f:
    print min(
        [(i.split('\t')[0], float(i.split('\t')[1])) for i in f.readlines()],
        key=lambda t: t[1]
    )
Maroun
  • 94,125
  • 30
  • 188
  • 241
0

Your "if" logic is backwards, it should be price<lowest_pre.

0

Just make a little adjustment start your price_lowest at None then set it to your first encounter and compare from there on

stocks = open("P:\COM661\stocks.txt")

name_lowest = ""
price_lowest = None

for line in stocks:
    rows = line.split("\t")
    price = float(rows[2])
    if price_lowest = None:
        price = price_lowest
        name_lowest = rows[1]
    elif price < price_lowest:
        price_lowest = price
        name_lowest = rows[1]
print(name_lowest + "\t" + str(price_lowest))
vash_the_stampede
  • 4,590
  • 1
  • 8
  • 20