# Having trouble with creating pyramid structure

Question:
So my problem is when I run this code with my text file, it generates some of the correct answer but then goes off track. I’m trying to make it generate a pyramid structure with number words pairs, then ignore all words except the last word in each line of the pyramid. It does the job perfectly for six words, and then loses track of where it’s at and spits out random words after that. I can’t tell if its reading the pyramid wrong, or generating the pyramid wrong.

``````def decode(message_file):
#Read the contents of the file
with open(message_file, 'r') as file:

# Extracting the number and corresponding word from each line
number_word_pairs = {int(line.split()[0]): line.split()[1] for line in lines}

# Building the pyramid structure
numbers = set()
current_line = 1
current_num = 1

while current_num <= max(number_word_pairs.keys()):
if current_num == current_line * (current_line + 1) // 2:
current_line += 1
current_num += 1

# Constructing the decoded message
decoded_message = ' '.join([number_word_pairs[num] for num in sorted(numbers) if num in number_word_pairs])

return decoded_message

# Example usage
decoded_message = decode('message_file.txt')
print(decoded_message)``````

(I’m guessing the numbers in the pairs are basically counting up, but mixed up?)
So, look at the while loop in your code.
It’ll mostly produce the same result (in `numbers`), the only difference being how far it goes. (Not sure why a set is being used here instead of a list.)
So, print out `numbers` and see if it matches what you expect.

Is the `numbers` output you want like this?

``````1 3 6 10 15 21
``````

It also seems like your loop is a convoluted way of calculating the pyramid numbers.

2 Likes

That is the numbers output I want, when I print numbers it shows
1, 3, 6, 136, 10, 15, 276, 21, 153, 28, 36, 171, 300, 45, 55, 190, 66, 78, 210, 91, 231, 105, 120, 253. Which is indeed incorrect, looks like I need to change how it’s generating the pyramid for sure

On further inspection, it looks like when I print numbers, it IS actually returning the correct numbers just mixed up. When constructing the decoded message I am using sorted(numbers). But for some reason when i print decoded_message it’s not only out of order, some of the words don’t even match the numbers??? Highly confused

(Debugging is a very important skill, if not the most important skill in programming.)

First, you should understand your data structures. In python, `set`s are an unordered, unique collection of things. Since you are sorting your `numbers`, it should not be a problem though.

However, in your `message_file`, you must make sure that all of the numbers are unique. This could be the cause of the problem.

If that is not the cause, then do some debugging. Print out your `number_word_pairs` and `sorted(numbers)` and try constructing the message yourself.

I currently cannot see what could be causing the problem in your code, so printing things out would be helpful.
(If you need more help, also provide the input, expected output, and actual output.)

1 Like