Number Chain Python Coding Game

In this activity, you will “chain up the numbers.”

Starter file

# Initial variable to track game play

# While we are still playing...

    # Ask the user how many numbers to loop through

    # Loop through the numbers. (Be sure to cast the string into an integer.)

        # Print each number in the range

    # Once complete, as the user if they would like to continue

Instructions

  1. Using a while loop, ask the user “How many numbers?” and then print out a chain of ascending numbers starting at 0.
  2. After the results have printed, ask the user if they would like to continue.
  • If “y”, restart the process, starting at 0 again.
  • If “n”, exit the chain.

Challenge

Rather than just displaying numbers constantly starting at 0, have the numbers begin at the end of the previous chain.

Solution

# Initial variable to track game play
user_play = "y"

# Set start and last number
start_number = 0

# While we are still playing...
while user_play == "y":

    # Ask the user how many numbers to loop through
    user_number = input("How many numbers? ")

    # Loop through the numbers. (Be sure to cast the string into an integer.)
    for x in range(start_number, int(user_number) + start_number):

        # Print each number in the range
        print(x)

    # Set the next start number as the last number of the loop
    start_number = start_number + int(user_number)

    # Once complete...
    user_play = input("Continue the chain: (y)es or (n)o? ")

We will be happy to hear your thoughts

Leave a reply