Editor

Pick an Item by Number

#569 · Errors and exceptions · Beginner · 35 XP · about 6 min

A program shows a numbered menu, starting at 1, and the user types the number of their choice. Write pick(items, choice) where items is the list on the menu and choice is the text the user typed. Return the chosen item, or a helpful message:

  • If choice is not a whole number, return 'Please type a number'.
  • If no item has that number, return 'There is no item 4', with the number they typed in place of 4.

Spaces around the number are fine. Remember that the menu starts at 1 while list positions start at 0, and that Python reads a negative position from the end of the list, which is not what the user meant.

Examples

menu = ['tea', 'coffee', 'juice']
pick(menu, '2')    # returns 'coffee'
pick(menu, 'abc')  # returns 'Please type a number'
pick(menu, '4')    # returns 'There is no item 4'
pick(menu, '0')    # returns 'There is no item 0'

Fastest solvers

Leaderboard

First solves only, ranked by stars and then time.