What's new

Help Bot paano ko kaya ma loop ito

Yayann

Honorary Poster
Established
print(end="Enter the Size: ")
arrSize = int(input())
print("Enter " +str(arrSize)+ " Elements: ")
arr = []
for i in range(arrSize):
arr.append(input())
print("Enter an Element to Search: ")
elem = input()
chk = 0
for i in range(arrSize):
if elem==arr:
index = i
chk = 1
break
if chk==1:
print("\nElement Found at Index Number: " + str(index))
else:
print("\nElement doesn't found!")
 
Solution
Sure! Here's an example of how to loop this code:

Code:
while True:
  print("Enter the Size: ")
  arrSize = int(input())
  print("Enter " +str(arrSize)+ " Elements: ")
  arr = []
  for i in range(arrSize):
    arr.append(input())
  print("Enter an Element to Search: ")
  elem = input()
  chk = 0
  for i in range(arrSize):
    if elem==arr[i]:
      index = i
      chk = 1
      break
  if chk==1:
    print("\nElement Found at Index Number: " + str(index))
  else:
    print("\nElement doesn't found!")
  
  print("\nDo you want to search again? (Y/N): ")
  choice = input()
  if choice.lower() == 'n':
    break

This will put the existing code inside a while loop that will keep running until the user chooses to stop searching by...
Hello!

It looks like this code prompts the user to input a size for an array, then allows the user to input elements for that array. After the user inputs the elements, the code prompts the user to input an element to search for in the array.

The code then checks if the user-inputted element is in the array, and if it is, it prints out the index position of that element in the array. If the element is not found in the array, the code prints out a message saying so.

Overall, this code is a simple implementation of linear search, which is a basic algorithm for finding a target element in an array.
 
Hello!

It looks like this code prompts the user to input a size for an array, then allows the user to input elements for that array. After the user inputs the elements, the code prompts the user to input an element to search for in the array.

The code then checks if the user-inputted element is in the array, and if it is, it prints out the index position of that element in the array. If the element is not found in the array, the code prints out a message saying so.

Overall, this code is a simple implementation of linear search, which is a basic algorithm for finding a target element in an array.
Patulong po kung pano po siya mag loop
 
Sure! Here's an example of how to loop this code:

Code:
while True:
  print("Enter the Size: ")
  arrSize = int(input())
  print("Enter " +str(arrSize)+ " Elements: ")
  arr = []
  for i in range(arrSize):
    arr.append(input())
  print("Enter an Element to Search: ")
  elem = input()
  chk = 0
  for i in range(arrSize):
    if elem==arr[i]:
      index = i
      chk = 1
      break
  if chk==1:
    print("\nElement Found at Index Number: " + str(index))
  else:
    print("\nElement doesn't found!")
  
  print("\nDo you want to search again? (Y/N): ")
  choice = input()
  if choice.lower() == 'n':
    break

This will put the existing code inside a while loop that will keep running until the user chooses to stop searching by entering "N". Within the loop, the user will be prompted to enter the size of the array, input the elements, and search for an element. After the search results are displayed, the user will be prompted to search again or not.

Patulong po kung pano po siya mag loop
Sure, here's an example of how you can loop this code using a while loop:

Code:
while True:
    print("Enter the size: ", end="")
    arr_size = int(input())
    arr = []
    for i in range(arr_size):
        print("Enter element #{}: ".format(i+1), end="")
        arr.append(input())
    print("Enter an element to search: ", end="")
    elem = input()
    index = -1
    for i in range(arr_size):
        if arr[i] == elem:
            index = i
            break
    if index == -1:
        print("Element not found!")
    else:
        print("Element found at index {}!".format(index))
    print("Do you want to search again? (Y/N): ", end="")
    choice = input()
    if choice.lower() == "n":
        break

This code will keep prompting the user to search for an element until they choose to stop by entering "N" when prompted. The initial input of the array size and the elements are inside a for loop, so that it will repeat for every search.
 
Solution

Similar threads

Back
Top