0
0
Blockchain / Solidityprogramming~30 mins

Using for directive in Blockchain / Solidity - Mini Project: Build & Apply

Choose your learning style9 modes available
Using for directive in blockchain smart contract
📖 Scenario: You are creating a simple blockchain smart contract that manages a list of registered users. You want to loop through the list of users to check their status.
🎯 Goal: Build a smart contract that stores a list of users and uses a for directive to iterate over them to count how many are active.
📋 What You'll Learn
Create a list variable called users with exact entries: 'Alice', 'Bob', 'Charlie'
Create a dictionary called status with exact entries: 'Alice': True, 'Bob': False, 'Charlie': True
Use a for directive with variable user to iterate over users
Count how many users have True status in status
Print the count with the exact text: Active users: X where X is the count
💡 Why This Matters
🌍 Real World
Smart contracts often need to process lists of users or transactions. Using loops helps automate checks and calculations.
💼 Career
Blockchain developers use loops and data structures to manage user states, balances, and permissions efficiently in smart contracts.
Progress0 / 4 steps
1
Create the list of users
Create a list called users with these exact entries: 'Alice', 'Bob', 'Charlie'
Blockchain / Solidity
Need a hint?

Use square brackets [] to create a list and separate names with commas.

2
Create the status dictionary
Create a dictionary called status with these exact entries: 'Alice': True, 'Bob': False, 'Charlie': True
Blockchain / Solidity
Need a hint?

Use curly braces {} to create a dictionary with keys and boolean values.

3
Use for directive to count active users
Create a variable called active_count and set it to 0. Then use a for directive with variable user to iterate over users. Inside the loop, increase active_count by 1 if status[user] is True.
Blockchain / Solidity
Need a hint?

Use for user in users: to loop, and check if status[user]: to count active users.

4
Print the number of active users
Write a print statement to display the text Active users: X where X is the value of active_count.
Blockchain / Solidity
Need a hint?

Use print(f"Active users: {active_count}") to show the result.