Python Forum

Full Version: Looping and nested loops
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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
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
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!
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
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!
(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