Advertisement
dsuveges

Untitled

Jan 22nd, 2023
984
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.20 KB | None | 0 0
  1. import random
  2.  
  3. # function to generate random one-digit numbers
  4. def generate_list(n):
  5.     return [random.randint(0, 9) for _ in range(n)]
  6.  
  7. # function to check if 7 is in the list
  8. def check_seven(lst):
  9.     if 7 in lst:
  10.         return "List contains the number 7"
  11.     else:
  12.         return "List does not contain the number 7"
  13.  
  14. # function to swap the 4th and 5th elements of the list
  15. def swap_elements(lst):
  16.     lst[3], lst[4] = lst[4], lst[3]
  17.     return lst
  18.  
  19. # function to add X, Y, or Z to each element of the list
  20. def add_letters(lst):
  21.     letters = ["X", "Y", "Z"]
  22.     return [str(i) + random.choice(letters) for i in lst]
  23.  
  24. # prompt the user for the number of elements in the list
  25. num_elements = int(input("Enter the number of elements in the list: "))
  26.  
  27. # generate the list of random one-digit numbers
  28. lst = generate_list(num_elements)
  29. print("Original list:", lst)
  30.  
  31. # check if 7 is in the list
  32. print(check_seven(lst))
  33.  
  34. # swap the 4th and 5th elements of the list
  35. swapped_lst = swap_elements(lst)
  36. print("List after swapping 4th and 5th elements:", swapped_lst)
  37.  
  38. # add X, Y, or Z to each element of the list
  39. new_lst = add_letters(swapped_lst)
  40. print("List after adding letters:", new_lst)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement