Sichanov

deck_of_cards

Oct 24th, 2021
281
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.26 KB | None | 0 0
  1. def is_valid(list, index):
  2.     if 0 <= index < len(list):
  3.         return True
  4.  
  5.  
  6. cards = input().split(', ')
  7.  
  8. number_of_commads = int(input())
  9.  
  10. for each_command in range(number_of_commads):
  11.     command = input().split(', ')
  12.     action = command[0]
  13.     if action == 'Add':
  14.         if command[1] in cards:
  15.             print('Card is already in the deck')
  16.         else:
  17.             cards.append(command[1])
  18.             print('Card successfully added')
  19.     elif action == 'Remove':
  20.         if command[1] in cards:
  21.             cards.remove(command[1])
  22.             print('Card successfully removed')
  23.         else:
  24.             print('Card not found')
  25.     elif action == 'Remove At':
  26.         index = int(command[1])
  27.         if is_valid(cards, index):
  28.             cards.pop(index)
  29.             print('Card successfully removed')
  30.         else:
  31.             print('Index out of range')
  32.     elif action == 'Insert':
  33.         index = int(command[1])
  34.         if is_valid(cards, index):
  35.             if command[2] in cards:
  36.                 print('Card is already added')
  37.             else:
  38.                 cards.insert(index, command[2])
  39.                 print('Card successfully added')
  40.         else:
  41.             print('Index out of range')
  42.  
  43. print(*cards, sep=', ')
  44.  
Advertisement
Add Comment
Please, Sign In to add comment