Python Forum
Trouble with "Weather Program" Assignment
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Trouble with "Weather Program" Assignment
#1
Hello,

My Module 4 Midterm for Python Fundamentals on edX involves the following instructions:

***************************************************

Create a program that:

imports and opens a file
appends additional data to a file
reads from the file to displays each city name and month average high temperature in Celsius
Output: The output should resemble the following

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
all of the above text output is generated from the file
the only strings are hard coded:

"is"
"of"
"Celsius"
import the file into the Jupyter Notebook environment

use !curl to download https://raw.githubusercontent.com/Micros...p_mean.csv as mean_temp.txt
# [ ] The Weather: import world_mean_team.csv as mean_temp.txt into the Jupyter notebook


Add the weather for Rio

Open the file in append plus mode ('a+')
Write a new line for Rio de Janeiro "Rio de Janeiro,Brazil,30.0,18.0\n"
Grab the column headings

use .seek() to move the pointer to the beginning of the file
read the first line of text into a variable called: headings
convert headings to a list using .split(',') which splits on each comma
# [ ] The Weather: open file, read/print first line, convert line to list (splitting on comma)


Read the remaining lines from the file using a while loop

assign remaining lines to a city_temp variable
convert the city_temp to a list using .split(',') for each .readline() in the loop
print each city & the highest monthly average temperature
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(',')
# [ ] The Weather: use while loop to print city and highest monthly average temp in celsius

*****************************************************

I have most of the code written and functioning up until the part involving the while loop with .readlines() and assigning the file to the city_temp variable (second to last paragraph in the instructions). I do not know how to set up the while loop, for city_temp I can only get it to read the second line of the file and to have it use the .split(",") properly. When I try to use city_temp = mean_temp.readlines().split(",") I constantly get an error message. Is there a way to overcome this issue by setting up the required while loop a certain way? Any help would be greatly appreciated, thank you!

*****************************************************

Here is my code so far:


In [1]:

# The Weather: import world_mean_temp.csv as mean_temp.txt into the Jupyter Notebook
​
! curl https://raw.githubusercontent.com/MicrosoftLearning/intropython/master/world_temp_mean.csv -o mean_temp.txt
    
mean_temp = open("mean_temp.txt", "r")
print("\n\n" + mean_temp.read())
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100   222  100   222    0     0    125      0  0:00:01  0:00:01 --:--:--   125


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

In [8]:

mean_temp = open("mean_temp.txt", "a+")
​
mean_temp.write("Rio de Janeiro,Brazil,30.0,18.0\n")
Out[8]:
32
In [9]:

mean_temp.seek(0)
​
headings = mean_temp.readline().split(",")
​
print(headings)
['city', 'country', 'month ave: highest high', 'month ave: lowest low\n']
In [10]:

city_temp = mean_temp.readline().split(",")
​
city_temp
Out[10]:
['Beijing', 'China', '30.9', '-8.4\n']
Reply
#2
What is the error message?
Reply
#3
I included the code line itself because I don't have it in the code from the original post

---------------------------------------------------------------------------
Traceback (most recent call last)

----> 1 city_temp = mean_temp.readlines().split(",")
2
3 city_temp

AttributeError: 'list' object has no attribute 'split'
Reply
#4
I would do two things, especially when things aren't working.
- Use separate program lines to do things. One to read the line and the next to split the result.
- Put print statements/functions after each to see what you get.
Reply
#5
(Aug-05-2018, 05:32 PM)sarah_mb_sues Wrote: I included the code line itself because I don't have it in the code from the original post

---------------------------------------------------------------------------
Traceback (most recent call last)

----> 1 city_temp = mean_temp.readlines().split(",")
2
3 city_temp

AttributeError: 'list' object has no attribute 'split'

Hey man Dance , please post the code with your attempt at the while loop still intact. The error you posted is suggesting that a portion of the data your looping over is not the data type you think it is.
Reply
#6
In your code it says 'readline', but in your error it says 'readlines'. These are two different things. 'readline' will return a string, which can be split. 'readlines' will return a list of strings, and cause the error you got.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Trouble with a turtle movement assignment. JosephSondgeroth 1 1,361 Jan-27-2022, 11:56 PM
Last Post: JosephSondgeroth
  Displaying selected information from XML file in the Spyder console(weather forecast) JohnN_pl 6 3,348 Jun-20-2020, 01:36 PM
Last Post: JohnN_pl
  Applied Data Science with Python - homework 2.2 (Weather plotting) eyavuz21 4 3,326 Jun-03-2020, 07:09 PM
Last Post: eyavuz21
  Trouble with Loops in lab assignment jonstryder 6 3,311 Jul-28-2019, 06:40 PM
Last Post: ThomasL
  How to hold a program in the turtle (Tic-Tac-Toe game assignment) kmchap 1 4,518 May-17-2018, 05:39 PM
Last Post: j.crater
  I'm having trouble with my assignment for creating a math quiz thewrongnarwhal 7 7,912 Nov-14-2016, 08:06 AM
Last Post: Ofnuts

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020