0

I created a program that keeps popping up with an error message that says

Traceback (most recent call last):
  File "/Users/salvas/Downloads/Salvas6/program6_2.py", line 23, in <module>
    main()
  File "/Users/salvas/Downloads/Salvas6/program6_2.py", line 12, in main
    reg_price = float(prices_file.readline())
ValueError: could not convert string to float: ''

The program is supposed to use information from a file and display it in a table. I was able to get it to print the information correctly, however, now it comes up with this error. The data includes a destination, the regular price of traveling to this destination, and the discounted percentage which gets calculated into a discounted price in this program. This is the data that I imported from a .txt file that was created using a different program:

San Francisco
550
10
New Orleans
330
10
Atlanta
295
20
def main():
    prices_file = open('prices.txt', 'r')
    destination = prices_file.readline()
    column1 = 'DESTINATION'
    column2 = 'REG.PRICE'
    column3 = 'REDUCED'
    column4 = 'SALE PRICE'
    print(f'{column1:<}\t{column2:}\t{column3:}\t\t{column4:}')
    while destination != '':
        reg_price = float(prices_file.readline())
        reduc_percent = float(prices_file.readline())
        percentage = reduc_percent / 100
        reduced = percentage * reg_price
        sale_price = reg_price - reduced
        destination = destination.rstrip('\n')
        print(f'{destination:<}\t${reg_price:.2f}\t\t${reduced:.2f}\t\t${sale_price:.2f}')
        destination = prices_file.readline()
    prices_file.close()
       
if __name__ == '__main__':
    main()

This is the program that I created that keeps coming up with an error message.

bob
  • 1
  • 1
  • 1
    What is in your input file precisely? If you actually see the \n in there in notepad or vi, you'll want to remove that. If it's an actual second line break, then that is going to throw off your parsing of the line too. – Jmc Mar 29 '23 at 18:22
  • BTW, welcome to Stack Overflow! Check out the [tour], and [ask] if you want tips. – wjandrea Mar 29 '23 at 18:23
  • I just included them to show that they were new lines in the .txt file but I will get rid of them. – bob Mar 29 '23 at 18:26
  • Sidenote: best practice for opening files is using `with` like `with open('prices.txt') as prices_file:`. It's covered in the official tutorial [here](https://docs.python.org/3/tutorial/inputoutput.html#reading-and-writing-files). – wjandrea Mar 29 '23 at 18:26

1 Answers1

1

I suspect the problem is that there are extra blank lines at the end of the file, so the last destination is just '\n'. You could fix that by stripping it earlier, for example:

while destination := prices_file.readline().rstrip('\n'):
wjandrea
  • 28,235
  • 9
  • 60
  • 81
  • Why are there two ':' in the while statement? – bob Mar 29 '23 at 18:35
  • @bob `:=` is an assignment. See [What are assignment expressions (using the "walrus" or ":=" operator)? Why was this syntax added?](https://stackoverflow.com/q/50297704/4518341) – wjandrea Mar 29 '23 at 18:36
  • Oh I see.. so I should put that line in where my original while statement is then correct? – bob Mar 29 '23 at 18:37
  • @bob Yes, but you'll also need to adapt your code to account for it, that is, remove the other assignments to `destination`. – wjandrea Mar 29 '23 at 18:44