-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlist2.py
More file actions
69 lines (59 loc) · 2.42 KB
/
Copy pathlist2.py
File metadata and controls
69 lines (59 loc) · 2.42 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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
def main():
print("Python List Operations")
print("1. Create a new list")
print("2. Add elements to the list")
print("3. Access elements from the list")
print("4. Remove elements from the list")
print("5. Exit")
my_list = None
while True:
try:
choice = int(input("\nEnter your choice: "))
if choice == 1:
# Create a new list
my_list = []
print("A new list has been created.")
elif choice == 2:
# Add elements to the list
if my_list is not None:
elements = input("Enter elements to add (comma-separated): ").split(',')
my_list.extend([element.strip() for element in elements])
print("Updated list:", my_list)
else:
print("Please create a list first!")
elif choice == 3:
# Access elements from the list
if my_list is not None:
if my_list:
index = int(input(f"Enter index (0 to {len(my_list) - 1}): "))
if 0 <= index < len(my_list):
print(f"Element at index {index}: {my_list[index]}")
else:
print("Index out of range.")
else:
print("The list is empty.")
else:
print("Please create a list first!")
elif choice == 4:
# Remove elements from the list
if my_list is not None:
if my_list:
element = input("Enter element to remove: ")
if element in my_list:
my_list.remove(element)
print("Updated list:", my_list)
else:
print("Element not found in the list.")
else:
print("The list is empty.")
else:
print("Please create a list first!")
elif choice == 5:
print("Exiting the program. Goodbye!")
break
else:
print("Invalid choice. Please select a valid option.")
except ValueError:
print("Invalid input. Please enter a number.")
if __name__ == "__main__":
main()