Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to use a condition inside a while loop?

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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])

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading