शुरू करेंमुफ़्त में शुरू करें

Conditional while loops

आपका recipe scaler, रेसिपी को स्केल-अप करने से पहले यह वेरिफ़ाई करना चाहता है कि हर ingredient पर्याप्त मात्रा में है. आप while लूप को conditional logic के साथ मिलाकर ingredient की क्वांटिटी जाँचेंगे और उपयोगी स्टेटस अपडेट देंगे. आपके पास एक वैरिएबल ingredients_checked है जो यह ट्रैक करता है कि आपने कितने ingredients वेरिफ़ाई कर लिए हैं, और total_ingredients जो आपकी टमाटर और बेसिल पास्ता रेसिपी में कुल ingredients की संख्या दर्शाता है.

यह अभ्यास पाठ्यक्रम का हिस्सा है

डेवलपर्स के लिए Python परिचय

पाठ्यक्रम देखें

अभ्यास निर्देश

  • ऐसा while लूप बनाएँ जो तब तक चले जब तक ingredients_checked, total_ingredients से कम हो.
  • हर बार लूप चलने पर ingredients_checked काउंटर को 1 से बढ़ाएँ.
  • Conditional statements का उपयोग करके जाँचें कि क्या 4 से कम ingredients रिव्यू किए गए हैं.
  • अगर नहीं, तो जाँचें कि क्या 6 या उससे कम ingredients रिव्यू किए गए हैं.

इंटरैक्टिव व्यावहारिक अभ्यास

इस अभ्यास को इस नमूना कोड को पूरा करके आज़माएँ।

total_ingredients = 7
ingredients_checked = 0

# Set up the loop
____ ingredients_checked < total_ingredients:
    # Increment the counter
    ____ += 1
    # Check if less than 4 ingredients reviewed
    ____ ingredients_checked < 4:
        print("More than half remaining")
    # Check if 6 or fewer ingredients reviewed
    ____ ingredients_checked <= 6:
        print("Nearly finished checking")
    else:
        print("All ingredients verified!")
कोड संपादित करें और चलाएँ