Dictionary with loop and squared numbers

i’m learning Julia through juliaacademy.com and i’m not getting past exercise 4.2.
4.1 I got it easy, but I ended up stalling right away. I’ve tried everything but it didn’t work at all.
if anyone can help me i will be very grateful.

4.1 Loop over integers between 1 and 100 and print their squares.

4.2 Add to the code above a bit to create a dictionary, squares that holds integers and their squares as key, value pairs such that

squares[10] == 100

What hav eyou tried? What hasn’t worked? Have you read the manual section on Dictionaries?

1 Like

that was as close as I could get …

Please don’t post screenshots - they’re hard to read. Instead, use this guidance to help you format your posts here.


You are creating a new Dict in every loop iteration - so you basically end up with a hundred dictionaries with one entry each.

What you want is to instantiate a Dict and then simply add key-value pairs to the existing single dictionary to end up with one Dict that has a hundred entries.

Have you looked at the manual section I linked above? You might be particularly interested in the line that references creating dictionaries from comprehensions :wink:


Thanks! :wink: :+1: