Bottom Page

• 0 Vote(s) - 0 Average
• 1
• 2
• 3
• 4
• 5
 Trouble with "Weather Program" Assignment sarah_mb_sues Programmer named Tim Posts: 13 Threads: 4 Joined: Jun 2018 Reputation: 0 Likes received: 0 #1 Aug-05-2018, 04:29 PM 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'] ``` heras Silly Frenchman Posts: 21 Threads: 3 Joined: Jul 2018 Reputation: 1 Likes received: 1 #2 Aug-05-2018, 05:24 PM What is the error message? sarah_mb_sues Programmer named Tim Posts: 13 Threads: 4 Joined: Jun 2018 Reputation: 0 Likes received: 0 #3 Aug-05-2018, 05:32 PM (This post was last modified: Aug-05-2018, 05:33 PM by sarah_mb_sues. Edited 1 time in total.) 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' heras Silly Frenchman Posts: 21 Threads: 3 Joined: Jul 2018 Reputation: 1 Likes received: 1 #4 Aug-06-2018, 03:16 PM 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. Vysero Wafer-Thin Wafer Posts: 84 Threads: 11 Joined: May 2018 Reputation: 1 Likes received: 4 #5 Aug-09-2018, 09:45 PM (This post was last modified: Aug-09-2018, 09:45 PM by Vysero. Edited 1 time in total.) (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 , 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. ichabod801 Bunny Rabbit Posts: 1,971 Threads: 48 Joined: Sep 2016 Reputation: 93 Likes received: 692 #6 Aug-10-2018, 02:29 AM 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 « Next Oldest | Next Newest »

Top Page

 Possibly Related Threads... Thread Author Replies Views Last Post Need help on assignment sstonne 1 138 Nov-01-2018, 02:48 PM Last Post: sstonne Dictionary based assignment: Need help RoyceJ 6 361 Aug-29-2018, 05:30 AM Last Post: perfringo Help With Coding Assignment sarah_mb_sues 4 411 Jul-10-2018, 05:10 PM Last Post: sarah_mb_sues UnboundLocalError: local variable 'a' referenced before assignment fad3r 3 1,372 Jun-20-2018, 05:43 PM Last Post: nilamo Trouble with edX Python Final sarah_mb_sues 11 1,314 Jun-19-2018, 10:36 AM Last Post: cryomick Trouble with assigning a string value in conditional statement juliabrushett 1 271 Jun-16-2018, 06:21 PM Last Post: ichabod801 why am I getting "local variable 'x' referenced before assignment"? wlsa 6 563 Jun-16-2018, 05:31 PM Last Post: buran Homework Assignment Help pinku018 3 364 Jun-08-2018, 01:09 PM Last Post: j.crater Homework Assignment Help sphedicl 3 372 Jun-08-2018, 12:26 PM Last Post: pinku018 Python for Everybody 5.2 assignment baba04201 7 8,198 May-31-2018, 04:22 PM Last Post: zaid03

Forum Jump:

Users browsing this thread: 1 Guest(s)