Hello guest, if you read this it means you are not registered. Click here to register in a few simple steps, you will enjoy all features of our Forum.
Bottom Page

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

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:

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
New York City,USA,28.9,-2.8

In [8]:

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

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

city_temp = mean_temp.readline().split(",")
['Beijing', 'China', '30.9', '-8.4\n']

What is the error message?
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(",")
3 city_temp

AttributeError: 'list' object has no attribute 'split'
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.
(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(",")
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.
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


Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  Help With Coding Assignment sarah_mb_sues 4 217 Jul-10-2018, 05:10 PM
Last Post: sarah_mb_sues
  UnboundLocalError: local variable 'a' referenced before assignment fad3r 3 274 Jun-20-2018, 05:43 PM
Last Post: nilamo
  Trouble with edX Python Final sarah_mb_sues 11 668 Jun-19-2018, 10:36 AM
Last Post: cryomick
  Trouble with assigning a string value in conditional statement juliabrushett 1 141 Jun-16-2018, 06:21 PM
Last Post: ichabod801
  why am I getting "local variable 'x' referenced before assignment"? wlsa 6 254 Jun-16-2018, 05:31 PM
Last Post: buran
  Homework Assignment Help pinku018 3 206 Jun-08-2018, 01:09 PM
Last Post: j.crater
  Homework Assignment Help sphedicl 3 232 Jun-08-2018, 12:26 PM
Last Post: pinku018
  Python for Everybody 5.2 assignment baba04201 7 5,210 May-31-2018, 04:22 PM
Last Post: zaid03
  I have an assignment - to create a keylogger MartyXO 9 676 May-30-2018, 03:53 PM
Last Post: MartyXO
  How to hold a program in the turtle (Tic-Tac-Toe game assignment) kmchap 1 189 May-17-2018, 05:39 PM
Last Post: j.crater

Forum Jump:

Users browsing this thread: 1 Guest(s)