Repository navigation
Expand file tree
/
Copy pathsearch_update_insert.py
More file actions
49 lines (39 loc) · 1.59 KB
/
Copy pathsearch_update_insert.py
File metadata and controls
49 lines (39 loc) · 1.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# Problem: Create a menu-driven program to search, update, or insert values in a list of numbers.
user_input = input("Enter number with spaces: ")
numbers = [int(x) for x in user_input.split()]
print("Choose an operation:")
print("1. Search")
print("2. Update")
print("3. Insert")
operator = int(input("Enter choice: "))
if operator == 1:
target = int(input("Enter a target: "))
found = False
for num in numbers:
if num == target:
found = True
break # if nahanap na
print(f"Target {target} found")
if operator == 2:
target = int(input("Enter a target: "))
new_value = int(input("Enter a new value: "))
for num in range(len(numbers) -1, -1, -1):
if numbers[num] == target:
numbers[num] = new_value
print(numbers)
# if operator == 3:
# new_number = int(input("Enter a new list value: "))
# index_position = int(input("Enter an index position: "))
# numbers.insert(index_position, new_number) # insert(index, value)
# approach 2 for insertion;
if operator == 3:
new_number = int(input("Enter a new list value: "))
index_position = int(input("Enter an index position: "))
new_list = [] # create a new empty list muna
for num in range(index_position): # add lahat ng numbers before the index
new_list.append(numbers[num])
new_list.append(new_number)
for num in range(index_position, len(numbers)): # add new number # add all numbers after the index
new_list.append(numbers[num])
numbers = new_list # replace the old list with a new one
print(numbers) # print the new list of numbers