Help: Tisbury Treasure Hunt

I am unable to solve this exercise. Can someone please help?

Hi @shreyaspandey12 :wave:

Please start a new topic and describe what you’ve tried, and what you are confused by.

Please also post your code and 1-2 failed tests in code blocks, so we can help troubleshoot:

To wrap code in a code block, highlight it and use the code block button in the UI:

grafik

Many thanks!

1 Like

Thanks @BethanyG

I tried below:

    for record in combined_record_group:
        report = (f"{(record[0],record[2],record[3],record[4])}\n")
        report += report
    return report```
  1. Please share a complete function, (if not your complete file).
  2. What do the failing tests say (using a codeblock)?
def clean_up(combined_record_group):
    for record in combined_record_group:
        report = str((f"{(record[0],record[2],record[3],record[4])}\n"))
        report += report
    return report
AssertionError: "('Silver Seahorse', 'Hidden Spring (Island o[112 chars]')\n" != "('Scrimshawed Whale Tooth', 'Deserted Docks'[872 chars]')\n"
Diff is 1015 characters long. Set self.maxDiff to None to see it.

Thanks for your help

In really simple steps, what do the two statements inside the loop do?

I am trying to loop through each tuple element, pull everything but record[1], convert to string and keep appending the string

Yes. But forget what you are trying to do for a moment and try to explain what that code actually does.

1 Like

Let’s carefully trace the code to see how report updates within the loop. For a very simple input (combined_records), it might be helpful to write down on paper how report changes. Hint: Check the actual output in the test failure result—the string that should be on the last line is appearing at the beginning!

1 Like

Thank you all for the leads. I understand report += report is not going to work.

I would like to know still how i can achieve the solution using the loop above. However, with some online help I changed my approach and now I have a minor challenge with newline “\n”.

def clean_up(combined_record_group):
    report = "\n".join(str(f"{(record[0],record[2],record[3],record[4])}") for record in combined_record_group)
    return report
AssertionError: "('Sc[854 chars], 'Hidden Spring (Island of Mystery)', ('4', 'E'), 'Yellow')" != "('Sc[854 chars], 'Hidden Spring (Island of Mystery)', ('4', 'E'), 'Yellow')\n"
Diff is 1096 characters long. Set self.maxDiff to None to see it.

  1. The issue isn’t the +=.
  2. Could you explain what your current challenge is?

My current code is not printing \n at the end of each line. However each string is printed on a new line.

The join() is adding newlines between each record but doesn’t add a newline at the end of the string. The tests expect a newline between each record (which your code does) plus a newline at the end (which your code does not do).

Yeah, I understand. Also that I missing something minute. I tried to concatenate \n to the end of each string, but that adds a 2nd \n to the first string.
Could you please tell me what’s wrong?

It finally worked with below code, but I couldn’t understand why. I understood "\n".join() was not needed though.

    report = "".join(str(f"{(record[0],record[2],record[3],record[4])}"+"\n") for record in combined_record_group)
    return report

"|".join(elements) puts a | between elements. "|".join("abc") gives "a|b|c".

"".join(f"{element}|" for element in elements) puts a | after each elements then concatenates them. "".join(f"{element}|" for element in "abc") gives "a|b|c|".

The exercise requires a trailing newline. "\n".join(elements) will not put a trailing "\n".

Does that help?