Flow of control
if/elif-statement
is used for making complex decisions.
Let's input a choice, then see the visual result of flow of control in CodeCraft world:
Example: choice
We have 3 white columns, depending on your choice, one of them will be highlighted with a specific color:
# 5 white columns
pool = [3,2,4,8,1]
for i in range(5):
column(i, -20, pool[i], 'quartz')
Run the see the original 5 white columns, then go on with the following code:
choice = int( input('Enter your choice of number from 1 to 5: ') )
if choice == 1:
print( '1, blue' )
column((choice-1), -20, pool[choice-1], 62)
elif choice == 2:
print( '2, red' )
column(choice-1, -20, pool[choice-1], 73)
elif choice == 3:
print( '3, yellow' )
column(choice-1, -20, pool[choice-1], 76)
elif choice == 4:
print( '4, pink' )
column(choice-1, -20, pool[choice-1], 71)
elif choice == 5:
print( '5, purple' )
column(choice-1, -20, pool[choice-1], 72)
else:
print( 'You entered an invalid choice. No column is chosen')
Run the whole program a few times, each time enter a different number to see the chosen column is highlighted with a color.
Such as: 4, pink
Example, EvenOdd()
Enter a number and then build column based on it's even or odd.
Even number: white column at x=n, height n, two white blocks on top of the column means it's even; Odd number: black column at x=n, height n, with one black block at the top means it's odd.
(Remember function with default parameter values?)
def EvenOdd(E='shulker_blue', O='shulker_yellow'):
print('even: blue, odd: yellow')
n = int(input('Enter an integer less than 20:'))
if n % 2 == 0:
column(n,-20,n, E)
else:
column(n,-20,n, O)
# call the function
EvenOdd()
Run to call the function a few times, each time enter a different number(0-20), check out the result image:
even: blue, odd: yellow
== to here
Example:
In next example, we will ask the user to enter a number, then base on the number, if smaller than 10, build a column with height of that number; if between 10 and 20, put a blue block at y=10; if reach 20, put a red block at y=20.
ans = int(input('Enter a number less than 30: ')
if ans < 10 :
print('OK to build a column')
column(2, -20, ans, 'quartz');
elif ans<20 :
print('between 10 to 20, blue 10')
block(3, 10, -20, 'wool_blue');
else:
print('larger than 20, red 20')
block(4, 20, -20, 'wool_red');
Each time you want to try a number, you need to refresh the screen and run the program again. Next let's design a similar application, but the user is asked 5 times to enter a number, each time(indicated by m), similar to the above example, the program will decide what to do based on each number.
(( did I talk about while loop before??))
user input, if statements and while loop
# while loop, if statements, prompt
m = 1
while m <6 :
ans = int(input('Enter a number less than 30: ')
if ans < 10 :
print('OK to build a column')
column(m,-20, ans, 'quartz');
elif ans<20 :
print('between 10 to 20, blue 10')
block(m,-20, 10, 'wool_blue');
else:
print('larger than 20, red 20')
block(m,-20, 10, 'wool_red');
m += 1
Run the above application, you will see very clearly which value range the 5 numbers fall into. This app is suitable for number analysis.
Also you will see CodeCraft makes it very clear to see how while
loop and if/elif/ else
statements change the flow of control.
This app only includes a few lines, but it uses many Python basics such as while
loop, if
statements, input()
function, type conversion int()
, Boolean expressions, as well as variable declaration, increment operator(+=), and of cause, calling on block()
and column()
functions.
(Did I talk about while
loop before? If not, then let's make it up)