15.8k views
0 votes
Program 3: Write a program check whether a number entered by the user is present in the list using 'in' operator with 'while' loop.​

1 Answer

1 vote

Answer:

Here's an example program that checks whether a number entered by the user is present in a list using the 'in' operator with a 'while' loop:

copynumbers = [10, 20, 30, 40, 50]

# Ask the user for a number to check

num = int(input("Enter a number: "))

# Define a variable to keep track of whether the number is found or not

found = False

# Use a while loop to iterate through the list and check if the number is present

i = 0

while i < len(numbers):

if numbers[i] == num:

found = True

break

i += 1

# Print the result based on whether the number is found or not

if found:

print("Number is present in the list.")

else:

print("Number is not present in the list.")

Step-by-step explanation:


In this program, we first define a list of numbers. Then, we ask the user to enter a number to check. We initialize a variable `found` to keep track of whether the number is found or not.

We use a 'while' loop to iterate through the list using an index variable `i`. Inside the loop, we check if the number at the current index `i` is equal to the number entered by the user. If a match is found, we set `found` to True and break out of the loop.

After the loop, we check the value of `found` and print the appropriate message to indicate whether the number is present in the list or not.

User BugBurger
by
8.6k points