Python Forum
Thread Rating:
  • 1 Vote(s) - 5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
WHILE LOOP in my program
#1
!curl https://raw.githubusercontent.com/MicrosoftLearning/intropython/master/world_temp_mean.csv -o mean_temp.txt

the_file = open("mean_temp.txt", "a+")
the_file.write("Rio de Janeiro,Brazil,30.0,18.0")
the_file.seek(0,0)
headings = the_file.readline().strip('\n')
headings = headings.split(',')
city_temp = the_file.readlines()
for line in city_temp:
    line_list = line.split(',')
    print(headings[0].title(),"of",line_list[0],headings[2],"is",line_list[2],"Celsius")
the_file.close()
Output:
% Total % Received % Xferd Average Speed Time Time Time Current Dload Upload Total Spent Left Speed 100 222 100 222 0 0 2349 0 --:--:-- --:--:-- --:--:-- 2387 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
So this is my code above. The output I produced is correct but my requirement is to use a while loop here.
"The Weather: use while loop to print city and highest monthly average temp in celsius". can anyone help me with this? thank you!
Reply
#2
while loops to go through lists generally use the pop method, which removes and returns the last item of the list:

breakfast = ['spam', 'spam', 'eggs', 'spam']
while breakfast:
    print('I ate some {}.'.format(breakfast.pop()))
Two things to note: it goes backwards and destroys the list. To go forward either reverse the list first. To keep the list, make a copy of it and destroy that.
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
  Noob here. Random quiz program. Using a while loop function and alot of conditionals. monkeydesu 6 1,378 Sep-07-2022, 02:01 AM
Last Post: kaega2
  Try-except in while loop: error with closing program Lupin_III 7 2,854 Jul-03-2020, 10:57 AM
Last Post: Lupin_III
  please help me in loop i am trying to run this program in raspberry pi by sending msg saifullahbhutta 2 2,319 Jan-27-2020, 02:32 PM
Last Post: saifullahbhutta
  While loop won't restart program :( TheDovah77 4 4,208 Apr-04-2019, 07:37 PM
Last Post: TheDovah77
  How to rerun the program in a loop with different input? bharaths 3 3,761 Nov-23-2018, 09:36 AM
Last Post: bharaths
  Nested while loop in pyramid program. raj55 2 5,733 May-04-2018, 10:05 AM
Last Post: ThiefOfTime

Forum Jump:

User Panel Messages

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