The First Step of Magic
Ma Su: “Zhuge, I have been troubled lately. As the team leader, I need to calculate everyone’s performance every month, but manual calculation is too cumbersome. Can programming solve this?”
Zhuge Liang: “Good! Today, I will teach you how to use Cursor, this magical programming assistant, to easily create a performance calculation system.”
Ma Su: “Cursor? What is that?”
Zhuge Liang: “It’s an AI-based intelligent programming tool, like having a little sprite that can write code for you. Let’s install it first. Go to cursor.so to download the installation package, and it will be done in a few minutes.”
Start the Magical Journey
Ma Su: “It’s installed! Wow, the interface is so clean!”
Zhuge Liang: “Let’s create a new project and write a simple performance calculation function. Watch closely:”
def calculate_performance(base_salary, sales_amount, attendance_days):
# Base salary ratio
base_ratio = 0.6
# Sales commission rate
commission_rate = 0.05
# Attendance bonus
attendance_bonus = 500 if attendance_days >= 22 else 0
# Calculate final performance
performance = (base_salary * base_ratio) + (sales_amount * commission_rate) + attendance_bonus
return performance
Ma Su: “I can roughly understand this code, but if I want to add more rules, it feels like it will get complicated…”
Zhuge Liang: “Don’t worry, see the magic of Cursor. Press Ctrl + K
below the code and tell it you want to add a team collaboration scoring rule.”
Ma Su: “Wow! It automatically modified the code for us!”
def calculate_performance(base_salary, sales_amount, attendance_days, team_score):
# Base salary ratio
base_ratio = 0.6
# Sales commission rate
commission_rate = 0.05
# Attendance bonus
attendance_bonus = 500 if attendance_days >= 22 else 0
# Team collaboration score converted to bonus (full score 10 points)
team_bonus = (team_score / 10) * 1000
# Calculate final performance
performance = (base_salary * base_ratio) +
(sales_amount * commission_rate) +
attendance_bonus +
team_bonus
return performance
Enhancing the Magic
Ma Su: “This is amazing! What if we want to add some data validation?”
Zhuge Liang: “Good idea! Press Ctrl + L
to let Cursor help us optimize the code:”
def validate_input(value, min_value, max_value, field_name):
if not isinstance(value, (int, float)) or value < min_value or value > max_value:
raise ValueError(f"{field_name} must be between {min_value} and {max_value}")
def calculate_performance(base_salary, sales_amount, attendance_days, team_score):
# Input validation
validate_input(base_salary, 3000, 50000, "Base Salary")
validate_input(sales_amount, 0, 1000000, "Sales Amount")
validate_input(attendance_days, 0, 31, "Attendance Days")
validate_input(team_score, 0, 10, "Team Score")
# Original calculation logic
base_ratio = 0.6
commission_rate = 0.05
attendance_bonus = 500 if attendance_days >= 22 else 0
team_bonus = (team_score / 10) * 1000
performance = (base_salary * base_ratio) + \
(sales_amount * commission_rate) + \
attendance_bonus + \
team_bonus
return round(performance, 2)
Practical Exercise
Ma Su: “Let’s test this system!”
Zhuge Liang: “Come, I will teach you to write test code. Just enter ‘write a test case’ in Cursor, and it will generate it:”
def test_performance_calculation():
# Test normal case
result = calculate_performance(5000, 100000, 23, 8.5)
print(f"Normal performance calculation result: {result}")
try:
# Test exceptional case
result = calculate_performance(2000, 100000, 32, 11)
except ValueError as e:
print(f"Caught expected error: {e}")
# Run test
test_performance_calculation()
Ma Su: “This is great! Now I can easily manage team performance. By the way, military advisor, are there any advanced features?”
Zhuge Liang: “Of course, for example, you can use Ctrl + /
to let Cursor add comments to the code, and Ctrl + R
to refactor the code. But let’s stop here for today, and you should practice these basic functions first.”
Summary
Ma Su: “I learned a lot today! Cursor is really powerful.”
Zhuge Liang: “That’s right, it can not only help you write code but also optimize and test it. Remember a few key shortcuts:
-
Ctrl + K
Intelligent coding assistant -
Ctrl + L
Code optimization -
Ctrl + /
Add comments -
Ctrl + R
Code refactoring
Alright, now you have mastered the basic usage of Cursor, go back and practice well!”