I basicly need to be able to restrict the items that can be appended in a list. Just like in a solitaire game, i need an item to be added to a list if it is meant to follow that item alphabetically and if it doesnt then an error is returned. for instance; letter d can be appended in front of letter c, if its a different letter or in the wrong order then the card wont be moved.
the code below is the best i can do so far.
a = ['a','c','e','j','h']
b = ['b','d','f','i','g']
def list_a():
loop = 0
choice = 0
while loop == 1:
print('''Choose from one of the following options:
1. Move item from list b to list a
2. add item to list a!''')
choice = int(input("What would you like to do?: "))
if choice == 1:
move_item()
elif choice == 2:
add_item()
return choice
print('List a: ',a)
print('List b: ',b)
def move_item():
loop = 0
choice = 0
while loop == 1:
print('''Choose from one of the following options:
1. Move item 1
2. move item 2
3. Move item 3
4. move item 4
5. Move item 5!''')
choice = int(input("What would you like to do?: "))
if choice == 1:
a.append(b.pop(0))
elif choice == 2:
a.append(b.pop(1))
elif choice == 3:
a.append(b.pop(2))
elif choice == 4:
a.append(b.pop(3))
elif choice == 5:
a.append(b.pop())
return choice
def add_item():
print()
list_a()
Create a subclass of list and override setitem, insert, and/or whatever else you need to to store your business logic.
Somebody else could probably tell us if setitem is sufficient, or if you need to worry about append and insert, also. Experimentation would work, too, but let me know what you find. 🙂