liveBook · Manning

Range

As explained in A Pythonic Adventure: From Python basics to a working web app

The Python range function produces an arithmetic sequence of integers represented by a range object.

Need menu choices that grow with any list? Python’s range() creates a sequence of integers for a loop. With range(1, len(choices) + 1), the numbers begin at 1 and stop before the final boundary, so adding 1 includes the menu’s length. section 4.0

Build the allowed answers

For three drinks, range(1, len(choices) + 1) produces 1, 2, and 3. A for loop converts each number to text and appends it to allowed_answers, letting the menu validate user input. section 4.0

. . .
    while True:
        choice = input(prompt)
        allowed_answers = []                  #1
        for a in range(1, len(choices)+1):    #2
            allowed_answers.append(str(a))    #3
        if choice not in allowed_answers:
            answer = choices[int(choice) - 1]
            break
        else:
            print("Enter number 1, 2, or 3!")
            answer = ''
. . .

See range() used inside a menu loop to create valid numeric answers from the list length. section 4.0

How A Pythonic Adventure: From Python basics to a working web app covers it

How deep it goes
Explains loop construction and the exclusive upper boundary, stopping short of deeper range-object internals.
Examples
Uses drink and flavor menus to generate numeric answer strings for lists of different lengths.
What it assumes
Assumes basic Python lists and loops while teaching the exclusive-stop boundary through a concrete menu.
What you can do after
Build dynamic numeric choices for a menu by combining range() with len(), a for loop, and append().
How it connects
Connects range() with len(), for loops, lists, string conversion, append(), and input validation.

Also explained in

Compare how 8 books explain range →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of A Pythonic Adventure: From Python basics to a working web app. The section links point to the parts of the book it draws on.