Python Forum
nested while loop flow help - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Homework (https://python-forum.io/forum-9.html)
+--- Thread: nested while loop flow help (/thread-13837.html)



nested while loop flow help - Ponamis - Nov-02-2018

def car_rental():
    exceeded_period_invalid = True
    while exceeded_period_invalid:
    
        daily_rental_period_requested_is_invalid = True
        while daily_rental_period_requested_is_invalid:          
            print()
            daily_rental_period = int(input("Enter the number of days requested: "))
            print()
            if daily_rental_period < 0:
                print ("Please enter a number greater than 0: ")
            if daily_rental_period > 180:
                print ("The rental period exceed 180 days try a lower amount: ")
            else:
                daily_rental_period_requested_is_invalid = False

        weekly_rental_period_requested_is_invalid = True
        while weekly_rental_period_requested_is_invalid:  
            print()
            weekly_rental_period = int(input("Enter the number of weeks requested: "))
            print()
            if weekly_rental_period < 0:
                print ("Please enter a number greater than 0: ")
            if weekly_rental_period > 25:
                print ("The rental period exceed 180 days try a lower amount: ")
            else:
                weekly_rental_period_requested_is_invalid = False
        
        # Convert weeks in to days
        converted_weekly_rental_to_day = weekly_rental_period // 7                      
        total_daily_rental_period = converted_weekly_rental_to_day + daily_rental_period

        # Add converted days to customer day input and check if total exceed 180 days 
        total_rental_period = total_daily_rental_period + daily_rental_period

    if total_daily_rental_period > 180:
            print ("The rental period exceeded 180 days try a lower amount: ")
            exceeded_period_invalid = False

    #for debugging only        
    print("exceeded period loop",exceeded_period_invalid,)
    print("daily invalid loop",daily_rental_period_requested_is_invalid)
    print("weekly invalid loop",weekly_rental_period_requested_is_invalid)



car_rental()
hi

i got a 1 main while loop, inside of it i have 2 while loops, it adds the result of the two inner and if it exceeds 180 days then the main loops runs again and ask the customer gain.

everything is fine untile you reach:

    if total_daily_rental_period > 180:
            print ("The rental period exceeded 180 days try a lower amount: ")
            exceeded_period_invalid = False


it eihter ignores it or gets an error depending on indentation level

and cant use other python features, this professor is a moron. Big Grin


RE: nested while loop flow help - j.crater - Nov-02-2018

Hello, it gets ignored when the condition isn't met. As for the indentation error, the error traceback message should be pointing you to the spot where the indendation is wrong. Double check alignment of code, it's easy to make an error with indentation. You can also post the full error traceback message (in error tags).


RE: nested while loop flow help - Ponamis - Nov-02-2018

if i input 1 for days and 1 for weeks it adds altogether the loop should stop

if total_daily_rental_period > 180:
            exceeded_period_invalid = False
            print ("The rental period exceeded 180 days try a lower amount: ")



RE: nested while loop flow help - j.crater - Nov-02-2018

This block of code is not inside the main/outer while loop, it's in the same level, which means it's outside.
The code would be much more readable if you condensed parts of it into functions. Such indendation bugs would be spotted much easier too.


RE: nested while loop flow help - Ponamis - Nov-02-2018

i think i know what i did wrong,
thank j.crater i understood more about indentation.