Python Forum
nested while loop flow help
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
nested while loop flow help
#1
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
Reply
#2
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).
Reply
#3
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: ")
Reply
#4
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.
Reply
#5
i think i know what i did wrong,
thank j.crater i understood more about indentation.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Bug Need help on this nested loop task PP9044 4 4,639 Apr-16-2021, 01:31 PM
Last Post: perfringo
  Nested if stmts in for loop johneven 2 5,867 Oct-19-2019, 04:05 AM
Last Post: xeedon
  Nested for loop issue always using index 0 searching1 2 2,577 Dec-30-2018, 09:17 AM
Last Post: searching1
  Nested loop Tryhard20 3 5,709 Sep-05-2018, 04:57 AM
Last Post: volcano63
  Inflow watertank before outward flow starts orjstrand 6 4,965 May-02-2018, 11:31 AM
Last Post: j.crater
  Nested Loop multiplication table SushiRolz 3 10,204 Feb-28-2018, 04:34 AM
Last Post: Larz60+
  Nested Loop to Generate Triangle Babbare 12 11,686 May-29-2017, 05:00 AM
Last Post: buran

Forum Jump:

User Panel Messages

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