Skip to content
Snippets Groups Projects
Commit 82b50372 authored by Daniel Moix's avatar Daniel Moix
Browse files

Adding list_viewer.py

parent 6f18a435
Branches
No related tags found
No related merge requests found
print("Hello CS 171")
\ No newline at end of file
import random
def generate_random_list(min_val, max_val, length):
random_list = []
for _ in range(length):
random_list.append(random.randint(min_val, max_val))
return random_list
if __name__ == "__main__":
min_val = int(input("Enter the minimum value: "))
max_val = int(input("Enter the maximum value: "))
length = int(input("Enter the length of the list: "))
sort_list = input("Do you want the list to be sorted? (y/n): ").strip().lower()
random_list = generate_random_list(min_val, max_val, length)
if sort_list == 'y':
random_list.sort()
views = 0
while True:
try:
index = int(input("Enter the index you want to view: "))
if index < 0 or index >= length:
raise IndexError
print(f"Value at index {index}: {random_list[index]}")
views += 1
except IndexError:
print("Invalid index entered.")
print(f"Full list: {random_list}")
print(f"Number of views: {views}")
break
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment