Keyword Argument Overriding a Positional Default

Easy ⏱ 8 min 79% acceptance ★★★★☆ 4.3
Write a function book_ticket(destination, seat_class="Economy", meal=True) that returns a formatted string "{destination} - {seat_class} - Meal: {meal}". Demonstrate that keyword arguments can be supplied out of order relative to their positions, as long as they are named.

Examples

Example 1
Input
book_ticket("Delhi")
Output
Delhi - Economy - Meal: True
Explanation

Both seat_class and meal use their defaults.

Example 2
Input
book_ticket("Mumbai", meal=False, seat_class="Business")
Output
Mumbai - Business - Meal: False
Explanation

Keyword arguments meal and seat_class are supplied out of their declared order, which is legal.

Constraints

  • destination has no default and must always be supplied.

Topics

Functionskeyword arguments

Companies

TCSInfosysWipro

Hints

Hint 1

Keyword arguments can appear in any order at the call site as long as they are named.

Hint 2

Use an f-string to format the final result.

Loading the Python runtime… Run executes your code and shows printed output; Submit checks your function against this problem's examples.