🐸

Default Settings

Python Basicspython-architect-02-default
Reward: 100 XP
|

The Wisdom of Laziness

The Archmage has a new task: Summon 50 Goblins to carry cargo.

Hoppy notices that 90% of Goblins are Green, and only a few captains are Red. But the old blueprint requires a color every time, so Hoppy has to write "Green" 45 times.

"Too tedious!" Hoppy complains. "Can't the blueprint just make Green ones by default?"

Factory Settings (Default Arguments)

Yes! We can install a Default Value into the blueprint slot.

def summon_goblin(color="Green"):
  print("Summoned a " + color + " Goblin!")

Now the rules have changed:

  1. Empty: Automatically uses the default "Green".
  2. Filled: Overrides the default with your specific color.

Your Mission

1
Modify Blueprint

Modify the make_potion function. Add a default value "Healing" to the effect parameter.

2
Quick Craft

Call make_potion() without arguments. It should make a Healing potion.

3
Custom Craft

Call make_potion("Poison") with an argument. It should make a Poison potion.

Position Rule

If you have multiple parameters, parameters with defaults must come LAST! Otherwise, Python gets confused about which value goes where.

Suggested Solution
Expand
Solution:
def make_potion(effect="Healing"):
  print("Brewed a potion with effect: " + effect)

make_potion()        # Defaults to Healing
make_potion("Poison") # Overrides to Poison
Advanced Tips
Want more? Click to expand

F. Bridge to Reality

  • Factory Setting = Default Argument.
  • This is common in APIs. For example, print() has a default argument end='\n', which is why it adds a new line automatically. If you write print("Hi", end=""), it won't add a new line!
Loading...
Terminal
Terminal
Ready to run...