If you run into a situation where you would like to ask a professor to write you a letter of recommendation, there are several things you should keep in mind
(taken directly from the Economics Department"Student Guide to Navigating the Econ Department"):
It is part of your professors’ job to write these letters for their students, so do not be shy about asking for a recommendation. Ask with plenty of time in
advance (a few weeks), and be prepared to share with your professor your resume and information about what you’re applying for and why you are interested in doing it. Your professor may or may not want to meet to discuss your interest in the position.
The letters matter less (and are easier to write) for low-stakes opportunities ... than for more selective or higher-stakes opportunities, so be particularly careful about choosing your letter-writers for the higher-stakes instances.
Who is a good letter-writer for you? An optimal letter-writer: has had you in at least one class or has supervised you in research ... or other work; their contact with you was relatively recent; you did well in the class or work; and is someone you had contact with them outside of just the classroom (so the prof got to know you a bit beyond your classroom participation).
The professor may say no for any number of reasons, notably: they don’t have time, they don’t feel that they know you well enough, or they’re not confident that they can write you a strong letter for this position. (Some profs will tell you that they can write you a letter but will warn you that it will not be a super enthusiastic letter or that it will acknowledge challenges or limitations; then you get to decide whether you still want this professor to write for you.) If they say no, don’t take it personally, and make sure you have time to move on to the next possible letter-writer.