-2

is my first post, sorry for the format

Problem: I have a error in the output, the data output is correct but, the loop is out of range and I don't know how to fix it.

Code:

exam_file=open('mean_text.txt', 'a+')
exam_file.write("Rio de Janeiro,Brazil,30.0,18.0\n")

exam_file.seek(0)
headings=exam_file.readline().split(",")

exam_file.seek(0,1)
city_temp=exam_file.readline().split(",")

while city_temp:
    print(headings[0].capitalize()+" of "+city_temp[0]+" "+ headings[2]+" is "+city_temp[2]+" Celsius")
    city_temp = exam_file.readline().split(",")

exam_file.close()

Output:

City of Beijing month ave: highest high is 30.9 Celsius
Traceback (most recent call last):
City of Cairo month ave: highest high is 34.7 Celsius
City of London month ave: highest high is 23.5 Celsius
  File "./Module4task.py", line 11, in <module>
City of Nairobi month ave: highest high is 26.3 Celsius
City of New York City month ave: highest high is 28.9 Celsius
    print(headings[0].capitalize()+" of "+city_temp[0]+" "+ headings[2]+" is "+city_temp[2]+" Celsius")
City of Sydney month ave: highest high is 26.5 Celsius
IndexError: list index out of range
City of Tokyo month ave: highest high is 30.8 Celsius
City of Rio de Janeiro month ave: highest high is 30.0 Celsius

Content of .txt file:

city,country,month ave: highest high,month ave: lowest low
Beijing,China,30.9,-8.4
Cairo,Egypt,34.7,1.2
London,UK,23.5,2.1
Nairobi,Kenya,26.3,10.5
New York City,USA,28.9,-2.8
Sydney,Australia,26.5,8.7
Tokyo,Japan,30.8,0.9

Expect:

City of Beijing month ave: highest high is 30.9 Celsius
City of Cairo month ave: highest high is 34.7 Celsius
City of London month ave: highest high is 23.5 Celsius
City of Nairobi month ave: highest high is 26.3 Celsius
City of New York City month ave: highest high is 28.9 Celsius
City of Sydney month ave: highest high is 26.5 Celsius
City of Tokyo month ave: highest high is 30.8 Celsius
City of Rio De Janeiro month ave: highest high is 30.0 Celsius

My Task:

Add the weather for Rio

  1. Open the file in append plus mode ('a+')

  2. Write a new line for Rio de Janeiro "Rio de Janeiro,Brazil,30.0,18.0\n" Grab the column headings

  3. use .seek() to move the pointer to the beginning of the file
  4. read the first line of text into a variable called: headings
  5. convert headings to a list using .split(',') which splits on each comma

Read the remaining lines from the file using a while loop

  1. assign remaining lines to a city_temp variable
  2. convert the city_temp to a list using .split(',') for each .readline() in the loop
  3. print each city & the highest monthly average temperature
  4. close mean_temps

Tips & Hints:

• print headings to determine indexes to use for the final output (what is in headings[0], [1], [2]..?)

• the city_temp data follows the order of the headings (city_temp[0] is described by headings[0])

• The output should look like: "month ave: highest high" for Beijing is 30.9 Celsius

• convert city_temp to lists with .split(',')

  • 1
    You might start by changing your while loop to a for loop which is the idiomatic way to iterate over lines in a file: `for city_temp in exam_file: city_temp = city_temp.split(','); .....` https://docs.python.org/3/tutorial/inputoutput.html#reading-and-writing-files – wwii Feb 13 '18 at 21:18
  • 1
    Something is really strange because of the way the error message is intermixed with the output. How are you running the script? – Barmar Feb 13 '18 at 21:19
  • one of the requirements is that I must use while loop. I running script in PyCharm – Vitaliy K Feb 13 '18 at 21:27
  • Your output is not compatible with your script: the text is different from what you're printing. Please update your post with the exact code you're running. As for your problem, my guess would be that you have a blank line at the end of your file. In that case, `exam_file.readline()` will give `\n`, which, when split, gives `['\n']`, which is still True as far as the while loop is concerned. – Nathan Vērzemnieks Feb 13 '18 at 22:34
  • If that's the case, you can fix (more or less) it by using `while len(city_temp) == len(headings):`. – Nathan Vērzemnieks Feb 13 '18 at 22:36
  • thank you so much Nathan, this instruction helped me. There is a difference when I run in Pycharm and when I run it in Jupyter – Vitaliy K Feb 13 '18 at 23:03
  • Possible duplicate of [What is a debugger and how can it help me diagnose problems?](https://stackoverflow.com/questions/25385173/what-is-a-debugger-and-how-can-it-help-me-diagnose-problems) – Raedwald Feb 16 '18 at 18:15

1 Answers1

0

Try changing the while loop so you have an endpoint

Like try adding a break statement after you reached the end of your file (using len to find how long it is)

Or you can add a condition to your while statement to check if the file is at the end

mgracer
  • 175
  • 1
  • 11