Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import random
- # function to generate random one-digit numbers
- def generate_list(n):
- return [random.randint(0, 9) for _ in range(n)]
- # function to check if 7 is in the list
- def check_seven(lst):
- if 7 in lst:
- return "List contains the number 7"
- else:
- return "List does not contain the number 7"
- # function to swap the 4th and 5th elements of the list
- def swap_elements(lst):
- lst[3], lst[4] = lst[4], lst[3]
- return lst
- # function to add X, Y, or Z to each element of the list
- def add_letters(lst):
- letters = ["X", "Y", "Z"]
- return [str(i) + random.choice(letters) for i in lst]
- # prompt the user for the number of elements in the list
- num_elements = int(input("Enter the number of elements in the list: "))
- # generate the list of random one-digit numbers
- lst = generate_list(num_elements)
- print("Original list:", lst)
- # check if 7 is in the list
- print(check_seven(lst))
- # swap the 4th and 5th elements of the list
- swapped_lst = swap_elements(lst)
- print("List after swapping 4th and 5th elements:", swapped_lst)
- # add X, Y, or Z to each element of the list
- new_lst = add_letters(swapped_lst)
- print("List after adding letters:", new_lst)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement