Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Looping and nested loops
#4
It looks you are using python2. If you were using python3, you will get ValueError, not NameError (line #9). See https://python-forum.io/Thread-Python3-2...-raw-input for more details. If this is the case you are strongly advised to switch to python3, because python2 support ends 1 January 2020.

what you have is close to what you want. You need to store the daily rain data in a list so that you can calculate average later.

all_days = ("Monday", "Tuesday", "Wednesday", "Thursday","Friday", "Saturday", "Sunday")
 
# number of weeks
while True:
    try:
        total_weeks = int(input("Enter number of weeks for which rainfall should be calculated "))
    except ValueError:
        print("Number of weeks must be an interger")
    else:
        if total_weeks < 1:
            print("Number of weeks must be at least 1")
        else:
            break

daily_rain = []
for week in range(1, total_weeks+1):
    for day in all_days:
        rain = int(input("enter the rain for {} in week {}:".format(day, week))) # in 3.6+ this could be rain = int(input(f"enter the rain for {day} in week {week}:"))
        daily_rain.append(rain)
        
# change the code above so that you can handle wron input for rain, i.e. you want number - int/float      
# add code to calculate average
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Messages In This Thread
Looping and nested loops - by albry - Dec-20-2018, 10:52 AM
RE: Looping and nested loops - by buran - Dec-20-2018, 10:56 AM
RE: Looping and nested loops - by albry - Dec-20-2018, 11:16 AM
RE: Looping and nested loops - by buran - Dec-20-2018, 11:31 AM
RE: Looping and nested loops - by albry - Dec-20-2018, 12:02 PM
RE: Looping and nested loops - by buran - Dec-20-2018, 12:10 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  for loops break when I call the list I'm looping through Radical 4 910 Sep-18-2023, 07:52 AM
Last Post: buran
  reduce nested for-loops Phaze90 11 1,937 Mar-16-2023, 06:28 PM
Last Post: ndc85430
  Nested for loops: Iterating over columns of a DataFrame to plot on subplots dm222 0 1,726 Aug-19-2022, 11:07 AM
Last Post: dm222
  Nested for loops - help with iterating a variable outside of the main loop dm222 4 1,605 Aug-17-2022, 10:17 PM
Last Post: deanhystad
  breaking out of nested loops Skaperen 3 1,233 Jul-18-2022, 12:59 AM
Last Post: Skaperen
  Break out of nested loops muzikman 11 3,392 Sep-18-2021, 12:59 PM
Last Post: muzikman
  Looping through nested elements and updating the original list Alex_James 3 2,147 Aug-19-2021, 12:05 PM
Last Post: Alex_James
  How to break out of nested loops pace 11 5,416 Mar-03-2021, 06:25 PM
Last Post: pace
  Nested for Loops sammay 1 8,815 Jan-09-2021, 06:48 PM
Last Post: deanhystad
  How to make this function general to create binary numbers? (many nested for loops) dospina 4 4,448 Jun-24-2020, 04:05 AM
Last Post: deanhystad

Forum Jump:

User Panel Messages

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