നിയന്ത്രണം#

ആമുഖം#

പൈത്തണിലെ നിയന്ത്രണം ഉപയോഗിച്ച് റോബോട്ടിനോട് എപ്പോൾ കാത്തിരിക്കണം, എപ്പോൾ പ്രവർത്തനങ്ങൾ ആവർത്തിക്കണം, എങ്ങനെ തീരുമാനങ്ങൾ എടുക്കണം, എപ്പോൾ പ്രോജക്റ്റ് അവസാനിപ്പിക്കണം എന്നിവ പറയാൻ കഴിയും.

മെത്തേഡുകളും കോർ പൈത്തൺ കീവേഡുകളും ഉൾപ്പെടെ ലഭ്യമായ നിയന്ത്രണങ്ങളുടെ ഒരു ലിസ്റ്റ് ചുവടെയുണ്ട്:

  • wait – ഒരു നിശ്ചിത സമയത്തേക്ക് പ്രോജക്റ്റ് താൽക്കാലികമായി നിർത്തുന്നു.

  • for – Repeats code for each item in a sequence.

  • if – ഒരു വ്യവസ്ഥ ശരിയാണെങ്കിൽ കോഡ് പ്രവർത്തിപ്പിക്കുന്നു.

  • if/else – ഒരു അവസ്ഥയെ ആശ്രയിച്ച് വ്യത്യസ്ത കോഡ് പ്രവർത്തിപ്പിക്കുന്നു.

  • if/elif/else – ഒന്നിലധികം വ്യവസ്ഥകൾ ക്രമത്തിൽ പരിശോധിക്കുന്നു.

  • while – ഒരു വ്യവസ്ഥ ശരിയാണെങ്കിൽ കോഡ് ആവർത്തിക്കുന്നു.

  • break – ഒരു ലൂപ്പിൽ നിന്ന് ഉടൻ പുറത്തുകടക്കുന്നു.

  • stop_project – പ്രവർത്തിക്കുന്ന പ്രോജക്റ്റ് അവസാനിപ്പിക്കുന്നു.

  • pass – ഒരു നടപടിയും ആവശ്യമില്ലാത്തപ്പോൾ ഉപയോഗിക്കുന്ന പ്ലെയ്‌സ്‌ഹോൾഡർ.

കാത്തിരിക്കുക#

wait ഒരു നിശ്ചിത സമയത്തേക്ക് താൽക്കാലികമായി നിർത്തുന്നു.

Usage:
wait(time, units)

പാരാമീറ്ററുകൾ

വിവരണം

time

ഒരു പോസിറ്റീവ് പൂർണ്ണ സംഖ്യയായോ ദശാംശമായോ കാത്തിരിക്കേണ്ട സമയത്തിന്റെ അളവ്.

units

കാത്തിരിപ്പ് സമയത്തെ പ്രതിനിധീകരിക്കുന്ന യൂണിറ്റ്: MSEC (സ്ഥിരസ്ഥിതി) – മില്ലിസെക്കൻഡ്, അല്ലെങ്കിൽ SECONDS

def main():
    # Turn right for one second, then stop
    drivetrain.turn(RIGHT)
    wait(1, SECONDS)
    drivetrain.stop()

# VR threads — Do not delete
vr_thread(main)

വേണ്ടി#

for ഒരു കൂട്ടം പ്രവൃത്തികൾ ഒരു നിശ്ചിത എണ്ണം തവണ ആവർത്തിക്കുന്നു. ലിസ്റ്റുകൾ, ട്യൂപ്പിളുകൾ, നിഘണ്ടുക്കൾ, സെറ്റുകൾ, സ്ട്രിംഗുകൾ എന്നിവയിലെ ഇനങ്ങളിലൂടെ ലൂപ്പ് ചെയ്യാൻ for ഉപയോഗിക്കാം, അല്ലെങ്കിൽ range ഉള്ള ഒരു നിശ്ചിത അളവിലുള്ള ലൂപ്പുകൾക്കായി ഉപയോഗിക്കാം.

ഉപയോഗം:

for value in expression_list:
    pass

ഘടകങ്ങൾ

വിവരണം

value

ആവർത്തനത്തിൽ നിലവിലെ എലമെന്റിനെ സംഭരിക്കുന്ന ഒരു താൽക്കാലിക വേരിയബിൾ.

expression_list

ലൂപ്പ് ചെയ്യപ്പെടുന്ന ഘടകങ്ങളുടെ ശേഖരം (ഉദാ. ലിസ്റ്റ്, സ്ട്രിംഗ്, ശ്രേണി).

def main():
    # Move in a square path
    for index in range(4):
        drivetrain.drive_for(FORWARD, 100, MM)
        drivetrain.turn_for(RIGHT, 90, DEGREES)

# VR threads — Do not delete
vr_thread(main)

def main():
    # Print each item in the list
    colors = ["Red", "Green", "Blue"]

    for color in colors:
        brain.print(color)
        brain.new_line()

# VR threads — Do not delete
vr_thread(main)

എങ്കിൽ#

if കോഡിന്റെ ഇൻഡന്റ് ചെയ്ത ബ്ലോക്ക് പ്രവർത്തിപ്പിക്കുന്ന അവസ്ഥ True.

ഉപയോഗം:

if condition:
    pass

ഘടകങ്ങൾ

വിവരണം

condition

സ്റ്റേറ്റ്മെന്റ് പ്രവർത്തിക്കുമ്പോൾ പരിശോധിക്കുന്ന ഒരു എക്സ്പ്രഷൻ അല്ലെങ്കിൽ വേരിയബിൾ. അത് Trueആണെങ്കിൽ, if ബ്ലോക്ക് എക്സിക്യൂട്ട് ചെയ്യുന്നു; അത് Falseആണെങ്കിൽ, ബ്ലോക്ക് ഒഴിവാക്കപ്പെടും.

def main():
    # Turn right until facing south
    while True:
        if drivetrain.heading(DEGREES) < 180:
            drivetrain.turn_for(RIGHT, 90, DEGREES)
        wait(0.1, SECONDS)

# VR threads — Do not delete
vr_thread(main)

അല്ലെങ്കിൽ/അല്ലെങ്കിൽ#

if ഉം else ഉം ഏത് ഇൻഡന്റ് ചെയ്ത കോഡ് ബ്ലോക്കാണ് പ്രവർത്തിക്കുന്നതെന്ന് നിർണ്ണയിക്കുന്നത്, അവസ്ഥ True അല്ലെങ്കിൽ False ആണോ എന്നതിനെ അടിസ്ഥാനമാക്കിയാണ്.

ഉപയോഗം:

if condition:
    pass
else:
    pass

ഘടകങ്ങൾ

വിവരണം

condition

സ്റ്റേറ്റ്മെന്റ് പ്രവർത്തിക്കുമ്പോൾ പരിശോധിക്കുന്ന ഒരു എക്സ്പ്രഷൻ അല്ലെങ്കിൽ വേരിയബിൾ. Trueആണെങ്കിൽ, if ബ്ലോക്ക് എക്സിക്യൂട്ട് ചെയ്യുന്നു; Falseആണെങ്കിൽ, else ബ്ലോക്കിനുള്ളിലെ കോഡ് പകരം എക്സിക്യൂട്ട് ചെയ്യുന്നു.

def main():
    # Drive after turning around
    while True:
        if drivetrain.rotation(DEGREES) < 180:
            drivetrain.turn_for(RIGHT, 90, DEGREES)
        else:
            drivetrain.drive(FORWARD)
        wait(0.1, SECONDS)

# VR threads — Do not delete
vr_thread(main)

എലിഫ്/അല്ലെങ്കിൽ#

വ്യവസ്ഥകളെ അടിസ്ഥാനമാക്കി ഏത് ഇൻഡന്റ് ചെയ്ത കോഡ് ബ്ലോക്ക് പ്രവർത്തിക്കുന്നുവെന്ന് if/elif/else ഘടന തിരഞ്ഞെടുക്കുന്നു:

  • if , അവസ്ഥ True.

  • മുൻ വ്യവസ്ഥകളെല്ലാം Falseആണെങ്കിൽ മാത്രമേ elif അധിക വ്യവസ്ഥകൾ പരിശോധിക്കൂ. ഒന്നിലധികം elif പ്രസ്താവനകൾ ഉപയോഗിക്കാം.

  • മുമ്പത്തെ അവസ്ഥകളൊന്നും True മാത്രമേ else അതിന്റെ ബ്ലോക്ക് പ്രവർത്തിപ്പിക്കുന്നുള്ളൂ.

ഉപയോഗം:

if condition:
    pass
elif condition:
    pass
else:
    pass

ഘടകങ്ങൾ

വിവരണം

condition

സ്റ്റേറ്റ്മെന്റ് പ്രവർത്തിക്കുമ്പോൾ പരിശോധിക്കുന്ന ഒരു എക്സ്പ്രഷൻ അല്ലെങ്കിൽ വേരിയബിൾ. ആദ്യത്തെ അവസ്ഥയായ True ഏത് ബ്ലോക്ക് എക്സിക്യൂട്ട് ചെയ്യുന്നുവെന്ന് നിർണ്ണയിക്കുന്നു; ഒന്നും Trueഅല്ലെങ്കിൽ, else ബ്ലോക്ക് പ്രവർത്തിക്കുന്നു.

def main():
    # Move depending on the detected color
    while True:
        if front_eye.detect(RED):
            drivetrain.drive(FORWARD)
        elif front_eye.detect(GREEN):
            drivetrain.drive(REVERSE)
        else:
            drivetrain.turn(RIGHT)
        wait(0.1, SECONDS)

# VR threads — Do not delete
vr_thread(main)

അതേസമയം#

while repeatedly runs code as long as the condition is True. It can also be used like a “forever” by using True as the condition, or a “wait until” by adding not to the condition, as shown in the examples below.

ഉപയോഗം:

while condition:
    pass

ഘടകങ്ങൾ

വിവരണം

condition

ഓരോ ആവർത്തനത്തിനും മുമ്പായി പരിശോധിക്കുന്ന ഒരു എക്സ്പ്രഷൻ അല്ലെങ്കിൽ വേരിയബിൾ. അത് Trueആണെങ്കിൽ, ലൂപ്പ് തുടരുന്നു; അത് Falseആണെങ്കിൽ, ലൂപ്പ് നിർത്തുന്നു.

def main():
    while True:
        # Move in a square path forever
        drivetrain.drive_for(FORWARD, 150, MM)
        drivetrain.turn_for(RIGHT, 90, DEGREES)

# VR threads — Do not delete
vr_thread(main)

def main():
    # Wait until an object is detected to stop driving
    drivetrain.drive(FORWARD)
    while not front_distance.found_object():
        wait(50, MSEC)
    drivetrain.stop()

# VR threads — Do not delete
vr_thread(main)

ബ്രേക്ക്#

break ലൂപ്പിൽ നിന്ന് ഉടൻ പുറത്തുകടക്കുന്നു. break ലൂപ്പുകൾ ഉപേക്ഷിക്കാൻ ഉപയോഗിക്കാം, അല്ലാത്തപക്ഷം അവ എന്നെന്നേക്കുമായി ലൂപ്പ് ചെയ്യപ്പെടും.

def main():
    # Stop turning after a button is pressed
    while True:
        drivetrain.turn(RIGHT)
        if front_distance.found_object():
            break
    drivetrain.stop()

# VR threads — Do not delete
vr_thread(main)

സ്റ്റോപ്പ്_പ്രോജക്റ്റ്#

stop_project ഓടിക്കൊണ്ടിരിക്കുന്ന ഒരു പ്രോജക്റ്റ് അവസാനിപ്പിക്കുന്നു.

Usage:
stop_project()

പാരാമീറ്ററുകൾ

വിവരണം

ഈ രീതിക്ക് പാരാമീറ്ററുകൾ ഇല്ല.

def main():
    # Stop the project after a button is pressed
    while True:
        drivetrain.turn(RIGHT)
        if front_distance.found_object():
            stop_program()
        drivetrain.drive(FORWARD)

# VR threads — Do not delete
vr_thread(main)

പാസ്#

pass എന്നത് ഭാവി കോഡിനുള്ള ഒരു പ്ലെയ്‌സ്‌ഹോൾഡറാണ്, കൂടാതെ ശൂന്യമായ ലൂപ്പുകൾ, കണ്ടീഷണലുകൾ, ഫംഗ്‌ഷനുകൾ എന്നിവയിലെ പിശകുകൾ ഒഴിവാക്കാൻ ഇത് ഉപയോഗിക്കാം.

if condition:
    pass

def function():
    pass