thecodezone.
The Code Zone
Short steps, at your pace · Free lessons and practice · Member video walkthroughs
Save your next small win.

Checking your account…

05.04 · Functions, defaults and scope / Practice · 7 min

Repeat text twice unless the caller chooses a count

The custom call works, but the call without a count repeats only once. The fallback is wrong.

Activity 4 of 6 in Functions, defaults and scope

All courses

Python 2: Logic, Loops & Functions / 05.04

Practice default arguments

Not started

Repair the default while preserving an explicitly supplied repetition count.

More options

Your lesson progress

Walkthrough: not startedPractice: not started

Empty: not started. Yellow: in progress. Green: complete. Marking the whole activity complete does not replace your test results. These circles save in this browser for this pilot.

Rob uses a default count of 2 for bye to get byebye and an explicit count of 3 for hi to get hihihi.
Rob’s comic strip · Functions, defaults and scope

Your task: repair the default, not the callers

Keep repeat_string(string, times) as the function’s parameter names, keep the string * times return expression, and set the default for times to 2. Keep custom_result assigned from repeat_string("hi", 3) and default_result from repeat_string("bye").

The two supplied print calls must display hihihi and byebye in that order. The explicit count of 3 must still override the default. Do not hardcode a repetition count in the return expression.

Try an explicit count of zero

The fixed-input checks inspect both stored strings and the complete output for the two calls. They do not prove every argument or the syntax used. After passing, try repeat_string("ha", 0): it should return an empty string. repeat_string("ha") should return haha, and repeat_string("ha", 1) should return ha. Restore the starter calls before checking again.

Build it one step at a time

Prefer learning by typing? Work through short instructions, run the Python code and check each change before continuing. Your guided project saves separately from the challenge below.

The mini-lesson opens in a new tab. Your own code carries forward; each step has a focused check and an optional hint.

Start mini-lesson(opens in a new tab)

3 steps · Saved work · Instant feedback

YOUR TURN

Choose the fallback repetition count

Keep repeat_string, the string * times return expression, both supplied calls and both print calls. Set times to default to 2 while keeping an explicit count of 3 effective for the custom call.

Expected output
hihihi
byebye

Keep the starting inputs. Print these lines in this order, without extra output.

  • Keep custom_result as hihihi
  • Store byebye in default_result
  • Print the custom repetition first
  • Print the default repetition second

Edit Python, then run it. Check challenge checks your output and the listed Python requirements. Your code saves on this browser after a short pause. Sign in and choose Save account draft to keep it across devices. Word wrap is enabled.

Ready to checkRun the checks to see your results here.

Need a nudge?

The fallback belongs in the signature

The caller that supplies 3 already works. Change the default in the parameter definition instead of forcing every call to repeat twice.

Replace your editor code?

Your current work will be replaced. Choose Cancel to keep it.

Ready to try it?

Practice in your browser and see your results.

Progress details

Have a question?

Ask the Code Zone community.

Ask a question in The Code Zone (opens in a new tab)

Continue learning

ONE STEP FORWARD

The default and explicit-argument paths now both work. Next, follow local names and returned results across a function boundary.