Array Lookup - Python Programming Exercise

his Python program asks the user to input a specific number of floating-point numbers and stores them in a list. The program then repeatedly prompts the user to enter a number and checks if that number is present in the previously created list. The process continues until the user enters the word "end," signaling the end of the program. The program utilizes a while loop to repeatedly ask for input and a conditional statement to check if the number exists in the list. The main goal of this exercise is to practice working with lists and user input in Python, as well as using conditional statements to check for membership in a collection. By managing the list dynamically based on user input and checking for membership, the program demonstrates how to work with data structures like lists and how to control program flow based on user interaction. The exercise also emphasizes the importance of handling user input effectively and efficiently, an essential skill for any Python programmer.

 Category

Arrays, Lists, and Strings

 Exercise

Array Lookup

 Objective

Develop a Python program that checks if a value belongs to a previously created list.

The steps to follow are:
- Ask the user how many values they will enter.
- Reserve space for that amount of floating-point numbers.
- Request the values from the user.
- Then, repeat:
* Ask the user for a number (execution ends when they enter "end" instead of a number).
* Indicate if that number is in the list or not.

This must be done in pairs, but you must provide a single source file, containing the names of both programmers in a comment.

 Example Python Exercise

 Copy Python Code
# Program developed by: Programmer 1, Programmer 2

# Ask the user how many values they will enter
num_values = int(input("How many values will you enter? "))

# Reserve space for that amount of floating-point numbers
values_list = []

# Request the values from the user
for i in range(num_values):
    value = float(input(f"Enter value {i+1}: "))
    values_list.append(value)

# Repeat asking for a number and check if it's in the list
while True:
    # Ask the user for a number or 'end' to stop
    user_input = input("Enter a number (or type 'end' to stop): ")

    # Stop the loop if the user types 'end'
    if user_input.lower() == 'end':
        break

    # Convert the input to a float and check if it's in the list
    try:
        number = float(user_input)
        if number in values_list:
            print(f"{number} is in the list.")
        else:
            print(f"{number} is not in the list.")
    except ValueError:
        print("Invalid input, please enter a valid number or 'end' to quit.")

 Output

How many values will you enter? 3
Enter value 1: 3.5
Enter value 2: 7.8
Enter value 3: 5.2
Enter a number (or type 'end' to stop): 7.8
7.8 is in the list.
Enter a number (or type 'end' to stop): 2.0
2.0 is not in the list.
Enter a number (or type 'end' to stop): end

 Share this Python Exercise

 More Python Programming Exercises of Arrays, Lists, and Strings

Explore our set of Python Programming Exercises! Specifically designed for beginners, these exercises will help you develop a solid understanding of the basics of Python. From variables and data types to control structures and simple functions, each exercise is crafted to challenge you incrementally as you build confidence in coding in Python.

  •  Even Number Array

    This Python program prompts the user to enter 10 integer numbers and displays only the even numbers from the list. It utilizes a for loop to iterate through th...

  •  Positive and Negative Number Array

    This Python program prompts the user to enter 10 real numbers and calculates two averages: one for the positive numbers and another for the negative numbers...

  •  Numerous Numbers and Their Sum

    This Python program prompts the user to enter several numbers, calculating their sum as they go. The program continues to prompt the user for numbers until they enter...

  •  Matrix Array

    This Python programming exercise is ideal for practicing the handling of multidimensional arrays. In this case, the goal is to prompt the user for the marks of 20 students, ...

  •  Advanced Statistics

    This Python statistical program is a fantastic exercise for improving data handling and user interaction in programming. The program allows users to add new data...

  •  Namedtuple

    This Python program is an excellent introduction to using NamedTuples for storing structured data. The program allows users to define 2D points with specific f...