നിയന്ത്രണം#
ആമുഖം#
പൈത്തണിലെ നിയന്ത്രണം ഉപയോഗിച്ച് റോബോട്ടിനോട് എപ്പോൾ കാത്തിരിക്കണം, എപ്പോൾ പ്രവർത്തനങ്ങൾ ആവർത്തിക്കണം, എങ്ങനെ തീരുമാനങ്ങൾ എടുക്കണം, എപ്പോൾ പ്രോജക്റ്റ് അവസാനിപ്പിക്കണം എന്നിവ പറയാൻ കഴിയും.
മെത്തേഡുകളും കോർ പൈത്തൺ കീവേഡുകളും ഉൾപ്പെടെ ലഭ്യമായ നിയന്ത്രണങ്ങളുടെ ഒരു ലിസ്റ്റ് ചുവടെയുണ്ട്:
wait– ഒരു നിശ്ചിത സമയത്തേക്ക് പ്രോജക്റ്റ് താൽക്കാലികമായി നിർത്തുന്നു.for– Repeats code for each item in a sequence.if– ഒരു വ്യവസ്ഥ ശരിയാണെങ്കിൽ കോഡ് പ്രവർത്തിപ്പിക്കുന്നു.if/else– ഒരു അവസ്ഥയെ ആശ്രയിച്ച് വ്യത്യസ്ത കോഡ് പ്രവർത്തിപ്പിക്കുന്നു.if/elif/else– ഒന്നിലധികം വ്യവസ്ഥകൾ ക്രമത്തിൽ പരിശോധിക്കുന്നു.while– ഒരു വ്യവസ്ഥ ശരിയാണെങ്കിൽ കോഡ് ആവർത്തിക്കുന്നു.break– ഒരു ലൂപ്പിൽ നിന്ന് ഉടൻ പുറത്തുകടക്കുന്നു.program_stop– റൺ ചെയ്യുന്ന പ്രോഗ്രാം അവസാനിപ്പിക്കുന്നു.pass– ഒരു നടപടിയും ആവശ്യമില്ലാത്തപ്പോൾ ഉപയോഗിക്കുന്ന പ്ലെയ്സ്ഹോൾഡർ.
കാത്തിരിക്കുക#
wait ഒരു നിശ്ചിത സമയത്തേക്ക് താൽക്കാലികമായി നിർത്തുന്നു.
Usage:wait(time, units)
പാരാമീറ്ററുകൾ |
വിവരണം |
|---|---|
|
ഒരു പോസിറ്റീവ് പൂർണ്ണ സംഖ്യയായോ ദശാംശമായോ കാത്തിരിക്കേണ്ട സമയത്തിന്റെ അളവ്. |
|
കാത്തിരിപ്പ് സമയത്തെ പ്രതിനിധീകരിക്കുന്ന യൂണിറ്റ്: |
# Move the arm to the desired position and then blink the signal tower
arm.move_to(40, 140, 210, False)
wait(1, SECONDS)
signal_tower.set_color(SignalTower.BLUE, SignalTower.BLINK)
വേണ്ടി#
for ഒരു കൂട്ടം പ്രവൃത്തികൾ ഒരു നിശ്ചിത എണ്ണം തവണ ആവർത്തിക്കുന്നു. ലിസ്റ്റുകൾ, ട്യൂപ്പിളുകൾ, നിഘണ്ടുക്കൾ, സെറ്റുകൾ, സ്ട്രിംഗുകൾ എന്നിവയിലെ ഇനങ്ങളിലൂടെ ലൂപ്പ് ചെയ്യാൻ for ഉപയോഗിക്കാം, അല്ലെങ്കിൽ range ഉള്ള ഒരു നിശ്ചിത അളവിലുള്ള ലൂപ്പുകൾക്കായി ഉപയോഗിക്കാം.
ഉപയോഗം:
for value in expression_list:
pass
ഘടകങ്ങൾ |
വിവരണം |
|---|---|
|
ആവർത്തനത്തിൽ നിലവിലെ എലമെന്റിനെ സംഭരിക്കുന്ന ഒരു താൽക്കാലിക വേരിയബിൾ. |
|
ലൂപ്പ് ചെയ്യപ്പെടുന്ന ഘടകങ്ങളുടെ ശേഖരം (ഉദാ. ലിസ്റ്റ്, സ്ട്രിംഗ്, ശ്രേണി). |
# Move the arm 4 times by 100 millimeters on the y axis
for index in range(4):
arm.move_inc(0, 100, 0)
എങ്കിൽ#
if കോഡിന്റെ ഇൻഡന്റ് ചെയ്ത ബ്ലോക്ക് പ്രവർത്തിപ്പിക്കുന്ന അവസ്ഥ True.
ഉപയോഗം:
if condition:
pass
ഘടകങ്ങൾ |
വിവരണം |
|---|---|
|
സ്റ്റേറ്റ്മെന്റ് പ്രവർത്തിക്കുമ്പോൾ പരിശോധിക്കുന്ന ഒരു എക്സ്പ്രഷൻ അല്ലെങ്കിൽ വേരിയബിൾ. അത് |
# Display the time until the move is completed
arm.move_to(-100, 200, 100, False)
while True:
print("\033[2J")
print(brain.timer_time(SECONDS))
wait(50, MSEC)
if arm.is_done():
brain.timer_reset()
അല്ലെങ്കിൽ/അല്ലെങ്കിൽ#
if ഉം else ഉം ഏത് ഇൻഡന്റ് ചെയ്ത കോഡ് ബ്ലോക്കാണ് പ്രവർത്തിക്കുന്നതെന്ന് നിർണ്ണയിക്കുന്നത്, അവസ്ഥ True അല്ലെങ്കിൽ False ആണോ എന്നതിനെ അടിസ്ഥാനമാക്കിയാണ്.
ഉപയോഗം:
if condition:
pass
else:
pass
ഘടകങ്ങൾ |
വിവരണം |
|---|---|
|
സ്റ്റേറ്റ്മെന്റ് പ്രവർത്തിക്കുമ്പോൾ പരിശോധിക്കുന്ന ഒരു എക്സ്പ്രഷൻ അല്ലെങ്കിൽ വേരിയബിൾ. |
# Display the time until the move is completed
arm.move_to(-100, 200, 100, False)
while True:
if arm.is_done():
brain.timer_reset()
else:
print("\033[2J")
print(brain.timer_time(SECONDS))
wait(50, MSEC)
എലിഫ്/അല്ലെങ്കിൽ#
വ്യവസ്ഥകളെ അടിസ്ഥാനമാക്കി ഏത് ഇൻഡന്റ് ചെയ്ത കോഡ് ബ്ലോക്ക് പ്രവർത്തിക്കുന്നുവെന്ന് if/elif/else ഘടന തിരഞ്ഞെടുക്കുന്നു:
if, അവസ്ഥTrue.മുൻ വ്യവസ്ഥകളെല്ലാം
Falseആണെങ്കിൽ മാത്രമേelifഅധിക വ്യവസ്ഥകൾ പരിശോധിക്കൂ. ഒന്നിലധികംelifപ്രസ്താവനകൾ ഉപയോഗിക്കാം.മുമ്പത്തെ അവസ്ഥകളൊന്നും
Trueമാത്രമേelseഅതിന്റെ ബ്ലോക്ക് പ്രവർത്തിപ്പിക്കുന്നുള്ളൂ.
ഉപയോഗം:
if condition:
pass
elif condition:
pass
else:
pass
ഘടകങ്ങൾ |
വിവരണം |
|---|---|
|
സ്റ്റേറ്റ്മെന്റ് പ്രവർത്തിക്കുമ്പോൾ പരിശോധിക്കുന്ന ഒരു എക്സ്പ്രഷൻ അല്ലെങ്കിൽ വേരിയബിൾ. ആദ്യത്തെ അവസ്ഥയായ |
# Change the signal tower light if arm can reach to the desired position
while True:
if arm.can_arm_reach_to(200, 200, 200):
signal_tower.set_color(SignalTower.BLUE, SignalTower.BLINK)
elif arm.can_arm_reach_to(300, 300, 300):
signal_tower.set_color(SignalTower.GREEN, SignalTower.BLINK)
else:
signal_tower.set_color(SignalTower.ALL, SignalTower.OFF)
wait(50, MSEC)
അതേസമയം#
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
ഘടകങ്ങൾ |
വിവരണം |
|---|---|
|
ഓരോ ആവർത്തനത്തിനും മുമ്പായി പരിശോധിക്കുന്ന ഒരു എക്സ്പ്രഷൻ അല്ലെങ്കിൽ വേരിയബിൾ. അത് |
# Display the time until the move is completed
arm.move_to(-100, 200, 100, False)
while True:
if arm.is_done():
break
else:
print("\033[2J")
print(brain.timer_time(SECONDS))
wait(50, MSEC)
# Blink blue light until the move is completed
signal_tower.set_color(SignalTower.BLUE, SignalTower.BLINK)
arm.move_to(-100, 200, 100, False)
while not arm.is_done():
wait(0.2, SECONDS)
signal_tower.set_color(SignalTower.BLUE, SignalTower.ON)
ബ്രേക്ക്#
break ലൂപ്പിൽ നിന്ന് ഉടൻ പുറത്തുകടക്കുന്നു. break ലൂപ്പുകൾ ഉപേക്ഷിക്കാൻ ഉപയോഗിക്കാം, അല്ലാത്തപക്ഷം അവ എന്നെന്നേക്കുമായി ലൂപ്പ് ചെയ്യപ്പെടും.
# Display the time until the move is completed
arm.move_to(-100, 200, 100, False)
while True:
if arm.is_done():
break
else:
print("\033[2J")
print(brain.timer_time(SECONDS))
wait(50, MSEC)
പ്രോഗ്രാം_സ്റ്റോപ്പ്#
program_stop ഓടിക്കൊണ്ടിരിക്കുന്ന ഒരു പ്രോജക്റ്റ് അവസാനിപ്പിക്കുന്നു.
Usage:brain.program_stop()
പാരാമീറ്ററുകൾ |
വിവരണം |
|---|---|
ഈ രീതിക്ക് പാരാമീറ്ററുകൾ ഇല്ല. |
# Stop the project until the move is completed
arm.move_to(-100, 200, 100, False)
while True:
if arm.is_done():
break
else:
print("\033[2J")
print(brain.timer_time(SECONDS))
wait(50, MSEC)
brain.program_stop()
പാസ്#
pass എന്നത് ഭാവി കോഡിനുള്ള ഒരു പ്ലെയ്സ്ഹോൾഡറാണ്, കൂടാതെ ശൂന്യമായ ലൂപ്പുകൾ, കണ്ടീഷണലുകൾ, ഫംഗ്ഷനുകൾ എന്നിവയിലെ പിശകുകൾ ഒഴിവാക്കാൻ ഇത് ഉപയോഗിക്കാം.
if condition:
pass
def function():
pass