Skip When a Multiple of X

PythonPythonBeginner
Practice Now

Introduction

In this project, you will learn how to create a function that generates a list of numbers from 1 to 100, skipping any numbers that are multiples of a given number or contain that number.

👀 Preview

Enter a number: 7
>>> [1, 2, 3, 4, 5, 6, 8, 9, 10, 11, 12, 13, 15, 16, 18, 19, 20, 22, 23, 24, 25, 26, 29, 30, 31, 32, 33, 34, 36, 38, 39, 40, 41, 43, 44, 45, 46, 48, 50, 51, 52, 53, 54, 55, 58, 59, 60, 61, 62, 64, 65, 66, 68, 69, 80, 81, 82, 83, 85, 86, 88, 89, 90, 92, 93, 94, 95, 96, 99, 100]

🎯 Tasks

In this project, you will learn:

  • How to implement the jump_x function to generate the desired list of numbers
  • How to take user input and use it in the function
  • How to run the function and observe the output

🏆 Achievements

After completing this project, you will be able to:

  • Understand how to create a function that skips certain numbers based on a given condition
  • Implement user input in a Python program
  • Run a Python script and interpret the output

Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL python(("`Python`")) -.-> python/BasicConceptsGroup(["`Basic Concepts`"]) python(("`Python`")) -.-> python/ControlFlowGroup(["`Control Flow`"]) python(("`Python`")) -.-> python/DataStructuresGroup(["`Data Structures`"]) python(("`Python`")) -.-> python/FunctionsGroup(["`Functions`"]) python(("`Python`")) -.-> python/PythonStandardLibraryGroup(["`Python Standard Library`"]) python/BasicConceptsGroup -.-> python/variables_data_types("`Variables and Data Types`") python/BasicConceptsGroup -.-> python/numeric_types("`Numeric Types`") python/BasicConceptsGroup -.-> python/strings("`Strings`") python/BasicConceptsGroup -.-> python/type_conversion("`Type Conversion`") python/ControlFlowGroup -.-> python/conditional_statements("`Conditional Statements`") python/ControlFlowGroup -.-> python/for_loops("`For Loops`") python/ControlFlowGroup -.-> python/break_continue("`Break and Continue`") python/DataStructuresGroup -.-> python/lists("`Lists`") python/DataStructuresGroup -.-> python/tuples("`Tuples`") python/FunctionsGroup -.-> python/function_definition("`Function Definition`") python/PythonStandardLibraryGroup -.-> python/data_collections("`Data Collections`") python/BasicConceptsGroup -.-> python/python_shell("`Python Shell`") python/FunctionsGroup -.-> python/build_in_functions("`Build-in Functions`") subgraph Lab Skills python/variables_data_types -.-> lab-302762{{"`Skip When a Multiple of X`"}} python/numeric_types -.-> lab-302762{{"`Skip When a Multiple of X`"}} python/strings -.-> lab-302762{{"`Skip When a Multiple of X`"}} python/type_conversion -.-> lab-302762{{"`Skip When a Multiple of X`"}} python/conditional_statements -.-> lab-302762{{"`Skip When a Multiple of X`"}} python/for_loops -.-> lab-302762{{"`Skip When a Multiple of X`"}} python/break_continue -.-> lab-302762{{"`Skip When a Multiple of X`"}} python/lists -.-> lab-302762{{"`Skip When a Multiple of X`"}} python/tuples -.-> lab-302762{{"`Skip When a Multiple of X`"}} python/function_definition -.-> lab-302762{{"`Skip When a Multiple of X`"}} python/data_collections -.-> lab-302762{{"`Skip When a Multiple of X`"}} python/python_shell -.-> lab-302762{{"`Skip When a Multiple of X`"}} python/build_in_functions -.-> lab-302762{{"`Skip When a Multiple of X`"}} end

Implement the jump_x Function

In this step, you will learn how to implement the jump_x function in the jump_x.py file. Follow the steps below to complete this step:

  1. Open the jump_x.py file in your preferred code editor.
  2. Locate the jump_x function definition:
def jump_x() -> list:
    """
    Generate a list from 1 to 100, skipping numbers that are multiples of x or contain x.

    Args:
        x (int): The number to be skipped.

    Returns:
        list: The generated list.
    """
    x = int(input("Enter a number: "))
    result = []
    for num in range(1, 101):
        if num % x == 0 or str(x) in str(num):
            continue
        result.append(num)
    return result
  1. The function takes an input x from the user, which is the number to be skipped.
  2. It then generates a list of numbers from 1 to 100, skipping any numbers that are multiples of x or contain the digit x.
  3. The function returns the generated list.
  4. Save the jump_x.py file.

Run the jump_x Function

In this step, you will learn how to run the jump_x function and see the output.

  1. Open a terminal or command prompt.
  2. Navigate to the directory where the jump_x.py file is located.
  3. Run the following command:
python3 jump_x.py
  1. The program will prompt you to "Enter a number:". Enter a number, for example, 7.
  2. The program will output a list of numbers from 1 to 100 that do not contain the number 7 or multiples of 7.

Your output should look similar to this:

Enter a number: 7
>>> [1, 2, 3, 4, 5, 6, 8, 9, 10, 11, 12, 13, 15, 16, 18, 19, 20, 22, 23, 24, 25, 26, 29, 30, 31, 32, 33, 34, 36, 38, 39, 40, 41, 43, 44, 45, 46, 48, 50, 51, 52, 53, 54, 55, 58, 59, 60, 61, 62, 64, 65, 66, 68, 69, 80, 81, 82, 83, 85, 86, 88, 89, 90, 92, 93, 94, 95, 96, 99, 100]

Congratulations! You have successfully implemented the jump_x function and tested it.

Summary

Congratulations! You have completed this project. You can practice more labs in LabEx to improve your skills.

Other Python Tutorials you may like