Skip to content

E. Using Calculations


🎯 Learning Goals

By the end of this lesson, you'll be able to:

  • iBe able to use it in calculations
  • ○ Be able to create growing/shrinking effects
  • ○ Be able to understand the calculation steps

Core Concepts

Using calculations

🔢 Analogy: a magic calculator

Variable iPut it in a calculation, and you get a different result each time!

for i in range(8):
    Entity(model='cube', scale=1 + i * 0.2)

Calculation steps

Formula: scale = 1 + i * 0.2

i Calculation Result
0 1 + 0 × 0.2 1.0
1 1 + 1 × 0.2 1.2
2 1 + 2 × 0.2 1.4
3 1 + 3 × 0.2 1.6
... ... ...
7 1 + 7 × 0.2 2.4

Result: the size grows steadily from 1.0 to 2.4!

Order of operations

1 + i * 0.2
  1. i * 0.2 first (multiplication)
  2. 1 + the result later (addition)

Example Code

from ursina import *

app = Ursina()

base_cube = Entity(model='cube', color=color.yellow)

# Cubes that grow steadily
for i in range(8):
    Entity(
        parent=base_cube,
        model='cube',
        color=color.orange,
        scale=1 + i * 0.2,  # A calculation!
        alpha=0.1
    )

EditorCamera()
app.run()

What Happens

Volumetric cubes that grow steadily!


Examples of various calculations

# Calculate position
position=(i * 2, 0, 0)  # 0, 2, 4, 6, ...

# Calculate size
scale=1 + i * 0.5  # 1.0, 1.5, 2.0, 2.5, ...

# Calculate color (learned later)
color=color.hsv(i * 30, 1, 1)  # Rainbow colors!

Quiz

Quiz: calculation result
i = 3When it is 1 + i * 0.2What is the result of it?
Show Answer

Answer: C) 1.6

Order of operations: 1 + 3 * 0.2 = 1 + 0.6 = 1.6

multiplication (*) is calculated before addition (+)!

Calculation practice:

  • When i=2, i * 3the result:
  • When i=4, 1 + i * 0.5the result:
  • multiplication is calculated before

Try Coding It Yourself

Take what you learned with the blocks and write it yourself in Python code!

Putting calculations to use

1 + i * 0.2 Use a calculation like this to make a steadily growing value.

Example Answer
for i in range(5):
    value = 1 + i * 0.5
    print(f'i={i}, value={value}')

Result: 1.0, 1.5, 2.0, 2.5, 3.0 are printed!

Experimenting with various calculations

Combine multiplication, addition, and division to make various patterns!


🎯 Fill-in-the-Blank Mission

Fill in the blanks to complete the code!

Check answers
for i in range(5):
    result = i * 2
    print(f'{i} x 2 = {result}')
Check answers
for i in range(5):
    value = 10 + i * 5
    print(value)
Check answers
for i in range(5):
    x = i * 3
    print(f'Position: ({x}, 0)')

🧩 Key Takeaways

What you learned in this lesson:

Concept Description
Using calculations iPut it in a calculation to generate a different value each time
Order of operations Multiplication/division → addition/subtraction
Use cases Gradual changes in size, position, color, etc.

Review Checklist

  • [ ] iCan you use it in calculations?
  • [ ] 1 + i * 0.2 Can you predict the result of a calculation like this?
  • [ ] Do you understand the order of operations (multiplication first)?

Challenge

Challenge 1: ⭐ Easy

scale = 2 + i * 0.3 Use a calculation and check the results.

Hint: How does it change if the start value is 2?

Challenge 2: ⭐ Easy

range(10)Use this to increase the number of repetitions.

Hint: more cubes appear!

Challenge 3: ⭐⭐ Medium

Change the y-axis position based on ito make a staircase shape.

Hint: position=(0, i * 0.5, 0)Make the y value with a calculation like this.

Challenge 4: ⭐⭐ Medium

Use it for both the x-axis and y-axis to iplace them diagonally.

Hint: position=(i, i, 0)what pattern does it make?

Challenge 5: ⭐⭐⭐ Hard

Make a shrinking effect (as i grows, the size decreases).

Hint: scale = 3 - i * 0.2 Use a subtraction calculation like this.

Challenge 6: ⭐⭐⭐ Hard

Make a spiral pattern.

Hint: position=(i * 0.5, i * 0.3, 0)and scale=0.5 + i * 0.1Try combining them.


Congratulations!

You've mastered loops!

What you can do now: - for Using loops - range()Specifying the number of repetitions with this - The variable i using it - Correct indentation - Making various effects with calculations