0
0
Blockchain / Solidityprogramming~30 mins

Memory allocation in Blockchain / Solidity - Mini Project: Build & Apply

Choose your learning style9 modes available
Memory Allocation in Blockchain Smart Contracts
📖 Scenario: You are building a simple smart contract that stores user balances on a blockchain. Efficient memory allocation is important to keep the contract lightweight and fast.
🎯 Goal: Create a smart contract that initializes user balances, sets a maximum allowed balance, updates balances using a loop, and finally outputs the updated balances.
📋 What You'll Learn
Create a dictionary called balances with exact entries for three users and their initial balances
Create a variable called max_balance and set it to 1000
Use a for loop with variables user and balance to iterate over balances.items() and increase each balance by 100 without exceeding max_balance
Print the final balances dictionary
💡 Why This Matters
🌍 Real World
Smart contracts on blockchains often manage user balances and must allocate memory efficiently to avoid high costs and slow execution.
💼 Career
Understanding memory allocation and safe updates in smart contracts is essential for blockchain developers to write secure and efficient code.
Progress0 / 4 steps
1
DATA SETUP: Create initial user balances
Create a dictionary called balances with these exact entries: 'Alice': 500, 'Bob': 750, 'Charlie': 300
Blockchain / Solidity
Need a hint?

Use curly braces {} to create a dictionary with keys as user names and values as their balances.

2
CONFIGURATION: Set maximum allowed balance
Create a variable called max_balance and set it to 1000
Blockchain / Solidity
Need a hint?

Just assign the number 1000 to the variable max_balance.

3
CORE LOGIC: Increase balances safely
Use a for loop with variables user and balance to iterate over balances.items(). Increase each balance by 100 but do not let it exceed max_balance. Update the balances dictionary accordingly.
Blockchain / Solidity
Need a hint?

Use a loop to go through each user and balance. Add 100, then check if it goes over max_balance. If yes, set it to max_balance.

4
OUTPUT: Display updated balances
Write print(balances) to display the updated balances dictionary
Blockchain / Solidity
Need a hint?

Use the print() function to show the final dictionary.