Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Looping and nested loops
#1
all_days = ["Monday", Tuesday", "Wednesday", "Thursday","Friday", "Saturday", "Sunday")
total_rain = 0 
total_weeks = 0 

# number of weeks
while True:
    try:
        total_weeks = int(input("Enter number of weeks for which rainfall should be calculated "))
    except NameError:
        print("Number of weeks must be an interger")
    else:
        if total_weeks < 1:
            print("Number of weeks must be at least 1")
        else:
            break
## rain by days of the week 

for weeks in range(total_weeks):
    for days in all_days:
        print("enter the rain for:",(days(0)) + 1 ), "in" (range(total_weeks))
I've just started an into to programming course and its all online and finding it SO hard to get support for the uni, so if someone would be able to point me in the direction of what I'm trying to do!

I need to ask a user to tell me how many weeks they want to know the average rainfall for (which I've gotten to work)
the system then needs to ask them for everyday of the week, of all weeks requested how many mm of rain fall on that day and then add up the value of each day to find averages. I cannot for the life of me make the day and weeks loop through :(

Please assist Cry Cry Cry
Reply
#2
Please, fix the identation of your code, as well as missing double-quote for Tuesday.
Also state what is your goal and what is your question
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
#3
Sorry, I accidentally submitted it too early!

I need help on how to make the day and week print. I know that I need the program to run through each day and say "enter the amount of rain for Monday of week one: ", "Enter the amount of rain for Tuesday of week One" and so on but I can't figure out how to make the value actually appear in my print output.

I know that I will also need to use a counter for all the values to add up and use that to work out the averages, but I can hopefully fix that on my own!

I'm just not sure on the way I need to write it to make it run through each day and appear in the input question for the user.

Thanks!
Reply
#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
#5
Thank you! The above was so much help! I will be okay to do all the averaging coding myself. I just COULDN'T figure out how to do the loop.

I struggle with loop so just so I understand, with the first {} in the line that is calling from the day in my total_days list? and the second {} is calling from my week in the total_weeks?

And every time a new value for 'rain' in input by the user, it will add a new element into the list 'daily_rain'?

Thanks again!
Reply
#6
(Dec-20-2018, 12:02 PM)albry Wrote: so I understand, with the first {} in the line that is calling from the day in my total_days list? and the second {} is calling from my week in the total_weeks
No, it's not calling... It's string formatting. {} is a place-holder. Actual value comes from arguments of .format(). With f-strings in python 3.6+ it's even more convenient and natural.

Check

https://docs.python.org/3/library/stdtyp...str.format

and

https://docs.python.org/3/library/string...ing-syntax

(Dec-20-2018, 12:02 PM)albry Wrote: And every time a new value for 'rain' in input by the user, it will add a new element into the list 'daily_rain'?

yes, that is correct
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


Possibly Related Threads…
Thread Author Replies Views Last Post
  for loops break when I call the list I'm looping through Radical 4 824 Sep-18-2023, 07:52 AM
Last Post: buran
  reduce nested for-loops Phaze90 11 1,757 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,638 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,532 Aug-17-2022, 10:17 PM
Last Post: deanhystad
  breaking out of nested loops Skaperen 3 1,174 Jul-18-2022, 12:59 AM
Last Post: Skaperen
  Break out of nested loops muzikman 11 3,239 Sep-18-2021, 12:59 PM
Last Post: muzikman
  Looping through nested elements and updating the original list Alex_James 3 2,069 Aug-19-2021, 12:05 PM
Last Post: Alex_James
  How to break out of nested loops pace 11 5,263 Mar-03-2021, 06:25 PM
Last Post: pace
  Nested for Loops sammay 1 7,287 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,331 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