നിയന്ത്രണം#
ആമുഖം#
പൈത്തണിലെ നിയന്ത്രണം ഉപയോഗിച്ച് റോബോട്ടിനോട് എപ്പോൾ കാത്തിരിക്കണം, എപ്പോൾ പ്രവർത്തനങ്ങൾ ആവർത്തിക്കണം, എങ്ങനെ തീരുമാനങ്ങൾ എടുക്കണം, എപ്പോൾ പ്രോജക്റ്റ് അവസാനിപ്പിക്കണം എന്നിവ പറയാൻ കഴിയും.
മെത്തേഡുകളും കോർ പൈത്തൺ കീവേഡുകളും ഉൾപ്പെടെ ലഭ്യമായ നിയന്ത്രണങ്ങളുടെ ഒരു ലിസ്റ്റ് ചുവടെയുണ്ട്:
wait– ഒരു നിശ്ചിത സമയത്തേക്ക് പ്രോജക്റ്റ് താൽക്കാലികമായി നിർത്തുന്നു.for– Repeats code for each item in a sequence.if– ഒരു വ്യവസ്ഥ ശരിയാണെങ്കിൽ കോഡ് പ്രവർത്തിപ്പിക്കുന്നു.if/else– ഒരു അവസ്ഥയെ ആശ്രയിച്ച് വ്യത്യസ്ത കോഡ് പ്രവർത്തിപ്പിക്കുന്നു.if/elif/else– ഒന്നിലധികം വ്യവസ്ഥകൾ ക്രമത്തിൽ പരിശോധിക്കുന്നു.while– ഒരു വ്യവസ്ഥ ശരിയാണെങ്കിൽ കോഡ് ആവർത്തിക്കുന്നു.break– ഒരു ലൂപ്പിൽ നിന്ന് ഉടൻ പുറത്തുകടക്കുന്നു.stop_program– പ്രവർത്തിക്കുന്ന പ്രോഗ്രാം അവസാനിപ്പിക്കുന്നു.pass– ഒരു നടപടിയും ആവശ്യമില്ലാത്തപ്പോൾ ഉപയോഗിക്കുന്ന പ്ലെയ്സ്ഹോൾഡർ.
കാത്തിരിക്കുക#
wait ഒരു നിശ്ചിത സമയത്തേക്ക് താൽക്കാലികമായി നിർത്തുന്നു.
Usage:wait(time, units)
പാരാമീറ്ററുകൾ |
വിവരണം |
|---|---|
|
ഒരു പോസിറ്റീവ് പൂർണ്ണ സംഖ്യയായോ ദശാംശമായോ കാത്തിരിക്കേണ്ട സമയത്തിന്റെ അളവ്. |
|
കാത്തിരിപ്പ് സമയത്തെ പ്രതിനിധീകരിക്കുന്ന യൂണിറ്റ്: |
# Build Used: Super Code Base 2.0
def main():
# Turn right for one second
drivetrain.turn(RIGHT)
wait(1, SECONDS)
drivetrain.stop()
# Start threads — Do not delete
start_thread(main)
വേണ്ടി#
for ഒരു കൂട്ടം പ്രവൃത്തികൾ ഒരു നിശ്ചിത എണ്ണം തവണ ആവർത്തിക്കുന്നു. ലിസ്റ്റുകൾ, ട്യൂപ്പിളുകൾ, നിഘണ്ടുക്കൾ, സെറ്റുകൾ, സ്ട്രിംഗുകൾ എന്നിവയിലെ ഇനങ്ങളിലൂടെ ലൂപ്പ് ചെയ്യാൻ for ഉപയോഗിക്കാം, അല്ലെങ്കിൽ range ഉള്ള ഒരു നിശ്ചിത അളവിലുള്ള ലൂപ്പുകൾക്കായി ഉപയോഗിക്കാം.
ഉപയോഗം:
for value in expression_list:
pass
ഘടകങ്ങൾ |
വിവരണം |
|---|---|
|
ആവർത്തനത്തിൽ നിലവിലെ എലമെന്റിനെ സംഭരിക്കുന്ന ഒരു താൽക്കാലിക വേരിയബിൾ. |
|
ലൂപ്പ് ചെയ്യപ്പെടുന്ന ഘടകങ്ങളുടെ ശേഖരം (ഉദാ. ലിസ്റ്റ്, സ്ട്രിംഗ്, ശ്രേണി). |
# Build Used: Super Code Base 2.0
def main():
# Move in a square path
for index in range(4):
drivetrain.drive_for(FORWARD, 100, MM)
drivetrain.turn_for(RIGHT, 90)
# Start threads — Do not delete
start_thread(main)
എങ്കിൽ#
if കോഡിന്റെ ഇൻഡന്റ് ചെയ്ത ബ്ലോക്ക് പ്രവർത്തിപ്പിക്കുന്ന അവസ്ഥ True.
ഉപയോഗം:
if condition:
pass
ഘടകങ്ങൾ |
വിവരണം |
|---|---|
|
സ്റ്റേറ്റ്മെന്റ് പ്രവർത്തിക്കുമ്പോൾ പരിശോധിക്കുന്ന ഒരു എക്സ്പ്രഷൻ അല്ലെങ്കിൽ വേരിയബിൾ. അത് |
# Build Used: Super Code Base 2.0
def main():
# Turn when the bumper is pressed
while True:
if bumper.is_pressed():
drivetrain.turn_for(RIGHT, 90)
wait(0.1, SECONDS)
# Start threads — Do not delete
start_thread(main)
അല്ലെങ്കിൽ/അല്ലെങ്കിൽ#
if ഉം else ഉം ഏത് ഇൻഡന്റ് ചെയ്ത കോഡ് ബ്ലോക്കാണ് പ്രവർത്തിക്കുന്നതെന്ന് നിർണ്ണയിക്കുന്നത്, അവസ്ഥ True അല്ലെങ്കിൽ False ആണോ എന്നതിനെ അടിസ്ഥാനമാക്കിയാണ്.
ഉപയോഗം:
if condition:
pass
else:
pass
ഘടകങ്ങൾ |
വിവരണം |
|---|---|
|
സ്റ്റേറ്റ്മെന്റ് പ്രവർത്തിക്കുമ്പോൾ പരിശോധിക്കുന്ന ഒരു എക്സ്പ്രഷൻ അല്ലെങ്കിൽ വേരിയബിൾ. |
# Build Used: Super Code Base 2.0
def main():
# Turn when the bumper is pressed
while True:
if bumper.is_pressed():
drivetrain.turn(RIGHT)
else:
drivetrain.stop()
wait(0.1, SECONDS)
# Start threads — Do not delete
start_thread(main)
എലിഫ്/അല്ലെങ്കിൽ#
വ്യവസ്ഥകളെ അടിസ്ഥാനമാക്കി ഏത് ഇൻഡന്റ് ചെയ്ത കോഡ് ബ്ലോക്ക് പ്രവർത്തിക്കുന്നുവെന്ന് if/elif/else ഘടന തിരഞ്ഞെടുക്കുന്നു:
if, അവസ്ഥTrue.മുൻ വ്യവസ്ഥകളെല്ലാം
Falseആണെങ്കിൽ മാത്രമേelifഅധിക വ്യവസ്ഥകൾ പരിശോധിക്കൂ. ഒന്നിലധികംelifപ്രസ്താവനകൾ ഉപയോഗിക്കാം.മുമ്പത്തെ അവസ്ഥകളൊന്നും
Trueമാത്രമേelseഅതിന്റെ ബ്ലോക്ക് പ്രവർത്തിപ്പിക്കുന്നുള്ളൂ.
ഉപയോഗം:
if condition:
pass
elif condition:
pass
else:
pass
ഘടകങ്ങൾ |
വിവരണം |
|---|---|
|
സ്റ്റേറ്റ്മെന്റ് പ്രവർത്തിക്കുമ്പോൾ പരിശോധിക്കുന്ന ഒരു എക്സ്പ്രഷൻ അല്ലെങ്കിൽ വേരിയബിൾ. ആദ്യത്തെ അവസ്ഥയായ |
# Build Used: Super Code Base 2.0
def main():
# Change the LED while turning
drivetrain.turn(RIGHT)
while True:
if drivetrain.get_heading() < 120:
bumper.set_color(OFF)
elif drivetrain.get_heading() < 240:
bumper.set_color(RED)
else:
bumper.set_color(GREEN)
wait(0.2, SECONDS)
# Start threads — Do not delete
start_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
ഘടകങ്ങൾ |
വിവരണം |
|---|---|
|
ഓരോ ആവർത്തനത്തിനും മുമ്പായി പരിശോധിക്കുന്ന ഒരു എക്സ്പ്രഷൻ അല്ലെങ്കിൽ വേരിയബിൾ. അത് |
# Build Used: Super Code Base 2.0
def main():
# Turn when the bumper is pressed
while True:
if bumper.is_pressed():
drivetrain.turn(RIGHT)
else:
drivetrain.stop()
wait(0.1, SECONDS)
# Start threads — Do not delete
start_thread(main)
# Build Used: Super Code Base 2.0
def main():
# Turn the other direction when the bumper is pressed
drivetrain.turn(LEFT)
while not bumper.is_pressed():
wait(0.2, SECONDS)
drivetrain.turn(RIGHT)
# Start threads — Do not delete
start_thread(main)
ബ്രേക്ക്#
break ലൂപ്പിൽ നിന്ന് ഉടൻ പുറത്തുകടക്കുന്നു. break ലൂപ്പുകൾ ഉപേക്ഷിക്കാൻ ഉപയോഗിക്കാം, അല്ലാത്തപക്ഷം അവ എന്നെന്നേക്കുമായി ലൂപ്പ് ചെയ്യപ്പെടും.
ഉപയോഗം:
break
പാരാമീറ്ററുകൾ |
വിവരണം |
|---|---|
ഈ കീവേഡിന് പാരാമീറ്ററുകൾ ഇല്ല. |
# Build Used: Super Code Base 2.0
def main():
# Stop turning after pressing the bumper
while True:
drivetrain.turn(RIGHT)
if bumper.is_pressed():
break
wait(0.2, SECONDS)
drivetrain.stop()
bumper.set_color(RED)
# Start threads — Do not delete
start_thread(main)
സ്റ്റോപ്പ്_പ്രോഗ്രാം#
stop_program ഓടിക്കൊണ്ടിരിക്കുന്ന ഒരു പ്രോജക്റ്റ് അവസാനിപ്പിക്കുന്നു.
Usage:stop_program()
പാരാമീറ്ററുകൾ |
വിവരണം |
|---|---|
ഈ രീതിക്ക് പാരാമീറ്ററുകൾ ഇല്ല. |
# Build Used: Super Code Base 2.0
def main():
# Stop the project after pressing the bumper
while True:
drivetrain.turn(RIGHT)
if bumper.is_pressed():
break
wait(0.2, SECONDS)
stop_program()
# Start threads — Do not delete
start_thread(main)
പാസ്#
pass എന്നത് ഭാവി കോഡിനുള്ള ഒരു പ്ലെയ്സ്ഹോൾഡറാണ്, കൂടാതെ ശൂന്യമായ ലൂപ്പുകൾ, കണ്ടീഷണലുകൾ, ഫംഗ്ഷനുകൾ എന്നിവയിലെ പിശകുകൾ ഒഴിവാക്കാൻ ഇത് ഉപയോഗിക്കാം.
ഉപയോഗം:
pass
പാരാമീറ്ററുകൾ |
വിവരണം |
|---|---|
ഈ കീവേഡിന് പാരാമീറ്ററുകൾ ഇല്ല. |
if condition:
pass
def function():
pass