-5

I am trying to search for a number in a tuple using a while loop in Python. The program finds the element , but it does not stop and continues to run until the end of the tuple. Here is my code:

numbers=(2,4,6,8,10,12)

x= int(input("Enter a number to search: "))

i=0

while i < len(numbers):

if numbers[i] == x:

print("Number found at index", i)

i+=1

Even when the number is found , the loop keeps running.

What i want:

  1. Stop the loop as soon as the element is found

  2. Also know if there is more pythonic way to do this

    Expected output example: If user enters 6

    Output: Number found at index 2

    How to fix this code and improve it?

New contributor
sneha verma is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.
2
  • 4
    Your code has invalid indentation and you should fix this. Commented 10 hours ago
  • Is it "LPU students making pointless posts to fulfil a class requirement" time of year again? Commented 7 hours ago

4 Answers 4

-1

You never tell the loop to stop. After printing the message you still increment i and the while keeps running until the end of the tuple.

Minimal fix for your code:

numbers = (2, 4, 6, 8, 10, 12)

x = int(input("Enter a number to search: "))

i = 0
while i < len(numbers):
    if numbers[i] == x:
        print("Number found at index", i)
        break          # stop the loop as soon as we find the element
    i += 1
else:
    # this runs only if the loop finished without hitting `break`
    print("Number not found")

More Pythonic approach – use a for loop (or index) instead of managing i yourself:

numbers = (2, 4, 6, 8, 10, 12)

x = int(input("Enter a number to search: "))

for i, value in enumerate(numbers):
    if value == x:
        print("Number found at index", i)
        break
else:
    print("Number not found")

Or just:

numbers = (2, 4, 6, 8, 10, 12)
x = int(input("Enter a number to search: "))

try:
    i = numbers.index(x)
    print("Number found at index", i)
except ValueError:
    print("Number not found")
Sign up to request clarification or add additional context in comments.

Comments

-1

Use break to exit the loop as soon as the element is found

numbers = (2, 4, 6, 8, 10, 12)

x = int(input("Enter a number to search: "))

i = 0

while i < len(numbers):
    if numbers[i] == x:
        print("Number found at index", i)
        break   # stop the loop
    i += 1
New contributor
Mario is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.

Comments

-2

You have to specify in the loop that the element is found you can stop now, to solve the issue use break before incrementing the value of i

New contributor
Shubham Chowdhury is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.

1 Comment

Try to add more information to your answer (e.g. code blocks)
-2

Your loop condition never becomes False -- This will print “Found” but keep looping, because you never told it to stop.

t = (3, 7, 10, 1)
i = 0

while i < len(t):
    if t[i] == 10:
        print("Found")
    i += 1
New contributor
Sudhanshu Mishra is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.