Application in CodeCraft
With input()
, we can design some functions in CodeCraft to build structures based on the users' input information.
We can use input()
in CodeCraft asking the user for a number. Keep in mind, the number entered here is returned as string
type, to use it as a number
type, we can do type conversion like this:
int( input('enter a number:' ) )
Examples:
Ask the user to input the number of how many blocks to build.
First block locates at (x,y,z), blocks are along a line and separated from each other.
# n blocks along x axis
def n_blocks_x(x,y,z):
n = int(input('How many blocks along x axis:'))
for t in range(n):
block(x,y,z, 1)
x += 2
n_blocks_x(0,3,-10) # brick
# n blocks along y axis
def n_blocks_y(x,y,z):
n = int(input('How many blocks along y axis:'))
for t in range(n):
block(x,y,z, 2)
y += 2
n_blocks_y(0,3,-10) # cobblestone
# n blocks along xy diagonal
def n_blocks_xy(x,y,z):
n = int(input('How many blocks along xy:'))
for t in range(n):
block(x,y,z, 3)
x += 2
y += 2
n_blocks_xy(0,3,-10) # diamond
(pic?)
User input how many blocks to build and the material choice:
def n_blocks_m(x,y,z):
n = int(input('How many blocks along x axis:'))
m = int(input("What's the material number?"))
for t in range(n):
block(x,y,z,m)
x += 2
Call to build some colorful block lines:
n_blocks_m(0, 2, -10) # enter n:4, m:76
n_blocks_m(-10, 3, -10) # enter n:6, m:73
n_blocks_m(4, 5, -10) # enter n:8, m:62
(( delete below??
# 4-04-4 n blocks along xyz
def n_blocks_xyz(x,y,z,material):
n = int(input('How many blocks along xyz:'))
for i in range(n):
block(x,y,z,material)
x += 2
y += 2
z += 2
n_blocks_xyz(0,3,-10,'obsidian')
# In screenshot 4-04-4, there are 10 x brick, 8 y cobblestone, 6 xy dirt, 10 xyz obsidian
# 4-04-5 nm_blocks_x,
n blue blocks along x, separated by s blocks (user input n,s).
def ns_blocks_x(x,y,z):
n = int(input('How many blocks to put:'))
s = int(input('separated blocks:)) # s means step
for i in range(n):
block(x,y,z,'wool_cyan')
x += s+1
nm_blocks_x(0,4,-10)
))
Build a column with height based on user's input
Column locates at ground location (x, z)
# user input column's height, diamond(3)
def column_h(x,z)
h = int(input('How tall is the column:'))
column(x,z,h, 3)
# call to build a few diamond columns
column_h(10, -20) # input h:12
column_h(4, -22) # input h:8
column_h(6, -18) # input h:4
Build a column with specific material based on user input
# 10 blocks tall column, user input material
def column_m(x,z):
m = int( input('Please enter material number of your choice: ') )
column(x,z,10, m)
# call to build some color columns
column_m(0,-20) # input m:66
column_m(3,-18) # input m:67
column_m(-2,-20) # input m:68
Check the image, the diamond columns are for user input height, the colorful ones are for user input materials
Exercise:
You can try for yourself to define a function to build a column with height
and material
all depend on user input.