Control Statements
Follow along: Control Statements
The program launching process along with parameter settings are all simplified and set up on the Jupyter Notebook Environment.
- Create a new *.ipynb file Jupyter Notebook
- Fill in the content below in the newly created file
- Follow and Execute the example codes
(The Jetson Board used for these examples are => Jetson Nano)
Let’s write the following examples.
if Statement
# 1
money = 0
if money:
print('money :', money, ', ",Take a taxi"')
else:
print('money :', money, ', ",Go on foot"')
# 2
money = 500
card = 1
if (money >= 30000 or card == True):
print('"Take a taxi"')
else:
print('"Go on foot"')
# 3
pocket = [500, 'cellphone']
money = pocket[0]
print('pocket: ', pocket)
if (money >= 30000 or 'card' in pocket):
print('"Take a taxi"')
elif 'cellphone' in pocket:
print('"Call mom"')
else:
print('"Go on foot"')
while Statement
# 1
a = 0
while (a < 10):
print('a :', a)
a += 1
# 2
b = 0
while (1):
print('b :', b)
b += 1
if (b == 10):
break
print("I succeeded in running away!")
# 3
c = 0
while True:
print('c :', c)
c += 1
if (c != 10):
continue
print('Can you see my writing?')
if (c == 10):
break
#4
while True:
print('Press "ctrl+c" to exit')
for Statement
# 1
numbers = [1, 2, 3, 4, 5]
for i in numbers:
print(i)
# 2
scores = [97, 56, 33, 78, 12, 84]
number = 1
for score in scores:
if score >= 60:
msg = (f'Student number {number} received'
f'a score of {score} and passed.')
else:
msg = (f'Student number {number} received'
f'a score of {score} and failed.')
print(msg)
number += 1
# 3
scores = [97, 56, 33, 78, 12, 84]
for number in range(len(scores)):
if scores[number] <= 60:
continue
msg = (f'Student number {number} received'
f'a score of {scores[number]} and passed.')
print(msg)
# 4
for i in range(2, 10):
print(i,'times table')
for j in range(1, 10):
print(i,'*',j,'=',i*j)