[Python] Python while loop not stopping when element is found in a tuple [closed]

Discussão em 'Python' iniciado por Stack, Novembro 30, 2025.

  1. Stack

    Stack Membro Participativo

    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 == 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?

    Continue reading...

Compartilhe esta Página