Home > OS >  How to use a condition inside a while loop?
How to use a condition inside a while loop?

Time:12-02

I have a line of codes to check if the entered value exsist in the database and will continue to loop but inside the while loop it also print the else statement which it shouldn't

cottageNotAvailable = False
mycursor.execute("SELECT * FROM reserved")
occupide = 0
name = input("Enter Name: ")
cottage_row = int(input("Select Cottage Row: "))
while cottage_row < 1 or cottage_row > 2:
cottage_row = int(input("Select Cottage Row: "))
cottage = int(input("Select Cottage: "))

##ROW 1 Cottages
if cottage_row == 1:
   subtotals = 1000
   ##Check Cottage if available
   users = mycursor.fetchall()
   for allUser in users:
       if str(cottage) in str(allUser[3]):
           cottageNotAvailable = True

   while cottage < 1 or cottage > 5 or cottageNotAvailable:
                    print("======Cottage is not available=======")
                    cottage = int(input("Select Cottage: "))
                    for allUser2 in users:
                        if(str(cottage) in str(allUser2[3])):
                            print("1")
                        else:
                            print('2')
  1. When Value exsist it print 1 and 2

Output: 1 2

  1. When Value does not exsist it just print. Which works fine 2 inside the while loop if the value exsist the else shouldn't also trigger

CodePudding user response:

What is the value of cottage and users? What does allUser2[3] value return ? Based on the condition check the value of str(cottage) in str(allUser2[3])

  • Related