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
Show 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