नियंत्रण#
परिचय#
पायथन में कंट्रोल आपको रोबोट को यह बताने की सुविधा देता है कि कब प्रतीक्षा करनी है, कब क्रियाओं को दोहराना है, निर्णय कैसे लेने हैं और कब प्रोजेक्ट को समाप्त करना है।
नीचे सभी कंट्रोल्स की सूची दी गई है, जिसमें मेथड्स और कोर पायथन कीवर्ड्स शामिल हैं:
wait– दिए गए समय के लिए प्रोजेक्ट को रोक देता है।for– अनुक्रम में प्रत्येक आइटम के लिए कोड दोहराता है।if– यदि कोई शर्त सही है तो कोड चलाता है।if/else– एक शर्त के आधार पर अलग-अलग कोड चलाता है।if/elif/else– क्रम में कई शर्तों की जाँच करता है।while– जब तक कोई शर्त सही है तब तक कोड दोहराता है।break– लूप से तुरंत बाहर निकलता है।stop_program– चल रहे प्रोग्राम को समाप्त करता है।pass– प्लेसहोल्डर का उपयोग तब किया जाता है जब किसी कार्रवाई की आवश्यकता नहीं होती है।
इंतज़ार#
wait कोड की अगली पंक्ति पर जाने से पहले एक निश्चित समय के लिए रुकता है।
Usage:wait(time, units)
पैरामीटर |
विवरण |
|---|---|
|
प्रतीक्षा करने का समय, धनात्मक पूर्ण संख्या या दशमलव के रूप में। |
|
प्रतीक्षा समय को दर्शाने वाली इकाई: |
# Move right for one second, then stop
robot.move_at(90)
wait(1, SECONDS)
robot.stop_all_movement()
के लिए#
for क्रियाओं के एक सेट को एक विशिष्ट संख्या में दोहराता है। for का उपयोग सूचियों, टुपल्स, शब्दकोशों, सेटों, स्ट्रिंग्स में आइटम के माध्यम से लूप करने के लिए या range के साथ लूप की एक निर्दिष्ट मात्रा के लिए किया जा सकता है।
उपयोग:
for value in expression_list:
pass
अवयव |
विवरण |
|---|---|
|
एक अस्थायी चर जो पुनरावृति में वर्तमान तत्व को संग्रहीत करता है। |
|
लूप में शामिल किए जा रहे तत्वों का संग्रह (जैसे, सूची, स्ट्रिंग, रेंज)। |
# Move in a square path.
for index in range(4):
robot.move_for(50, 0)
robot.turn_for(RIGHT, 90)
# Print each item in the list
colors = ["Red", "Green", "Blue"]
for color in colors:
robot.screen.print(color)
robot.screen.next_row()

अगर#
if इंडेंटेड कोड ब्लॉक चलाता है यदि शर्त True.
उपयोग:
if condition:
pass
अवयव |
विवरण |
|---|---|
|
एक एक्सप्रेशन या वेरिएबल जिसकी जाँच स्टेटमेंट चलने पर की जाती है। यदि यह |
# Kick when the screen is pressed
while True:
if robot.screen.pressing():
robot.kicker.kick(MEDIUM)
wait(0.1, SECONDS)
यदि/अन्यथा#
if और else यह निर्धारित करें कि कोड का कौन सा इंडेंटेड ब्लॉक चलता है, इस आधार पर कि शर्त True है या False।
उपयोग:
if condition:
pass
else:
pass
अवयव |
विवरण |
|---|---|
|
एक एक्सप्रेशन या वेरिएबल जिसकी जाँच स्टेटमेंट चलने पर की जाती है। यदि यह |
# Show one emoji when the screen is pressed,
# and a different emoji when not pressed
while True:
if robot.screen.pressing():
robot.screen.show_emoji(EXCITED)
else:
robot.screen.show_emoji(BORED)
wait(0.1, SECONDS)
यदि/यदि/अन्यथा#
if/elif/else संरचना शर्तों के आधार पर यह चयन करती है कि कोड का कौन सा इंडेंटेड ब्लॉक चलेगा:
ifअपने ब्लॉक को चलाता है यदि शर्तTrue.elifअतिरिक्त शर्तों की जाँच केवल तभी करता है जब पिछली सभी शर्तेंFalse. एकाधिकelifकथनों का उपयोग किया जा सकता है।elseअपने ब्लॉक को तभी चलाता है जब पिछली कोई भी शर्त सही न होTrue.
उपयोग:
if condition:
pass
elif condition:
pass
else:
pass
अवयव |
विवरण |
|---|---|
|
An expression or variable that is checked when the statement runs. The first condition that is |
# Move the robot forward or reverse
# based on the position of the joystick
def when_axis_changed():
position = controller.axis1.position()
if position > 0:
robot.move_at(0)
elif position < 0:
robot.move_at(180)
else:
robot.stop_all_movement()
controller.axis1.changed(when_axis_changed)
जबकि#
while तब तक कोड को बार-बार चलाता है जब तक कि शर्त Trueबनी रहती है। इसे True शर्त के रूप में उपयोग करके “हमेशा” के रूप में भी इस्तेमाल किया जा सकता है, या not शर्त में जोड़कर “तब तक प्रतीक्षा करें” के रूप में भी इस्तेमाल किया जा सकता है, जैसा कि नीचे दिए गए उदाहरणों में दिखाया गया है।
उपयोग:
while condition:
pass
अवयव |
विवरण |
|---|---|
|
एक एक्सप्रेशन या वेरिएबल जिसकी जाँच प्रत्येक पुनरावृति से पहले की जाती है। यदि यह |
# Keep the LEDs green while the robot is moving
robot.move_for(200, 0, wait=False)
while robot.is_move_active():
robot.led.on(ALL_LEDS, GREEN)
wait(50, MSEC)
robot.led.on(ALL_LEDS, BLACK)
while True:
# Continually flash all LEDs red then green.
robot.led.on(ALL_LEDS, RED)
wait(0.5, SECONDS)
robot.led.on(ALL_LEDS, GREEN)
wait(0.5, SECONDS)
# Wait until the screen is pressed before
# turning off the LEDs
while not robot.screen.pressing():
robot.led.on(ALL_LEDS, RED)
wait(50, MSEC)
robot.led.on(ALL_LEDS, BLACK)
तोड़ना#
break लूप से तुरंत बाहर निकल जाता है। break का उपयोग उन लूपों को छोड़ने के लिए किया जा सकता है जो अन्यथा हमेशा चलते रहते।
# Flash LEDs until the screen is pressed
while True:
robot.led.on(ALL_LEDS, RED)
wait(0.5, SECONDS)
robot.led.on(ALL_LEDS, GREEN)
wait(0.5, SECONDS)
if robot.screen.pressing():
break
# Turn the LEDs Off
robot.led.off(ALL_LEDS)
प्रोग्राम बंद करें#
stop_program एक चल रहे प्रोजेक्ट को समाप्त करता है।
Usage:robot.stop_program()
पैरामीटर |
विवरण |
|---|---|
इस विधि के कोई मानदंड नहीं हैं। |
# Stop the project once the screen is pressed
while True:
robot.led.on(ALL_LEDS, RED)
wait(0.5, SECONDS)
robot.led.on(ALL_LEDS, GREEN)
wait(0.5, SECONDS)
if robot.screen.pressing():
robot.stop_program()
उत्तीर्ण#
pass भविष्य के कोड के लिए एक प्लेसहोल्डर है और इसका उपयोग खाली लूप, सशर्त कथनों और फ़ंक्शन में त्रुटियों से बचने के लिए किया जा सकता है।
if condition:
pass
def function():
pass