This question was on a problem set regarding $L^p$ spaces in an undergraduate-level real analysis course. I actually used an answer on StackExchange to help me provide an example, but I couldn't provide adequate justification for why it works. The answer I used is linked here. I modified this function a little to make it so that it only converges if $p = 2$, namely I defined my function as follows: $$f(x)=\frac{1}{x^{2/p} \ln^2(x^{2/p})}$$
But I can't really explain why it works (and to be honest, I'm not really sure it does work). My professor hinted in class that there is a solution that involves a continuous piecewise function agreeing at $x=1$ that is basically a generalization of the observation that for the following functions: $$g(x)=\begin{cases} \frac{1}{\sqrt{x}} & 0<x<1 \\ 0 & \text{otherwise}\end{cases}$$ $$h(x)=\begin{cases} \frac{1}{x} & x>1 \\ 0 & \text{otherwise}\end{cases}$$ we can easily show that $g \in L^1(\mathbb{R}), \, g \not\in L^2(\mathbb{R})$ and $h \in L^2(\mathbb{R}), \, h \not\in L^1(\mathbb{R})$. So I would imagine that the solution based on this hint would involve some sort of clever manipulation of powers to incorporate all $p \in [1, 2) \cup (2, \infty]$ somehow, but I have no idea how to do that. I'm pretty lost with this problem. What is the best way to go about it?