0
0
Pythonprogramming~10 mins

Default arguments in Python - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to define a function with a default argument.

Python
def greet(name=[1]):
    print(f"Hello, {name}!")
Drag options to blanks, or click blank then click option'
A"Guest"
BGuest
Cname
DNone
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Forgetting quotes around string default values.
Using variable names without quotes as default.
2fill in blank
medium

Which value should be passed to greet() so it prints 'Hello, Guest!'?

Python
def greet(name="Guest"):
    print(f"Hello, {name}!")

result = greet([1])
Drag options to blanks, or click blank then click option'
A"User"
BNone
C"Guest"
Dname
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Passing None which would print 'Hello, None!'.
Using an unquoted variable name instead of a string value.
3fill in blank
hard

Fix the error in the function definition by completing the default argument correctly.

Python
def multiply(x, y=[1]):
    return x * y
Drag options to blanks, or click blank then click option'
A1
B"1"
C0
DNone
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Using a string "1" instead of number 1.
Using None which causes errors in multiplication.
4fill in blank
hard

Fill both blanks to create a function with two default arguments and call it with one argument.

Python
def order(item, quantity=[1], price=[2]):
    total = quantity * price
    print(f"Order: {quantity} {item}(s) for ${total}")

order("apple")
Drag options to blanks, or click blank then click option'
A1
B0
C0.5
D2
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Setting quantity to 0 which results in zero total.
Using integer price when decimal is expected.
5fill in blank
hard

Fill all three blanks to create a function with default arguments and call it with two arguments.

Python
def describe_pet(name=[1], species=[2], age=[3]):
    print(f"{name} is a {age}-year-old {species}.")

describe_pet("Buddy", "dog")
Drag options to blanks, or click blank then click option'
A"Unknown"
B"dog"
C3
D"Buddy"
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Using wrong default types like numbers for strings.
Not matching the order of arguments and defaults.