因此,我浏览了很多有关从python列表中删除重复项的文章,但是它们都使用相同的东西set()。我不想使用它,因为我希望可以从列表中完全删除两个重复的值。下面是代码(通过这种方式,我为姐姐制作了一个老女仆游戏): 编辑:我看着标记的重复问题,答案不是我想要的,因为标记的答案正在使用set(),这就是我希望不使用的。
import random
from random import randint
def main():
# Choosing card
computerHandLen = str(len(computerHand))
print('You have ' + computerHandLen + ' cards to choose from.')
cardChoice = int(input('Choose a card (0-' + str(len(computerHand)) + '):
'))
takenCard = computerHand[cardChoice]
print(takenCard)
# Giving taken card to player
lenPlayerHand = len(playerHand)
randPlayerHand = randint(0, lenPlayerHand)
playerHand.insert(randPlayerHand, takenCard)
# Computer choosing card from player
computerChoiceNum = randint(0, lenPlayerHand)
computerChoice = computerHand[computerChoiceNum]
print('The computer chose ' + computerChoice)
print(computerChoiceNum)
# Repeat
viewCards = input('Do you wish to view your cards?y or n: ').lower()
if viewCards == 'y':
print(playerHand)
else:
main()
cardList = ['GuitarGary', 'ClownClyde', 'LibbyLovegreen', 'CowhandCal',
'CookieConnie', 'HoseHaulerHarry', 'MotorcycleMike', 'MailmanMorris',
'BackhandBetty', 'MissAlphabet', 'WaitressWendy', 'NurseNell',
'FishermanFred', 'TutuTerri', 'MoonwalkerWally', 'QuarterbackJack',
'ArtistAnnie','GuitarGary', 'ClownClyde', 'LibbyLovegreen', 'CowhandCal',
'CookieConnie', 'HoseHaulerHarry', 'MotorcycleMike', 'MailmanMorris',
'BackhandBetty', 'MissAlphabet', 'WaitressWendy', 'NurseNell',
'FishermanFred', 'TutuTerri', 'MoonwalkerWally', 'QuarterbackJack',
'ArtistAnnie']
print(cardList)
cardDispensor = random.choice(cardList)
cardSeperater = playerHand.append(cardDispensor)
print('Note: This is a singleplayer game!')
startGame = input('Do you want to learn the rules of Old Maid? y or n:
').lower()
if startGame == 'y':
print(' ')
print('Old Maid is a card game where your goal is to get rid of all of
your cards before the other player(s). You also do not want to be the last
person with the Old Maid. YOu want the other player to pick the Old Maid
from your hand if you have it. To get rid of cards you must get 2 of the
same card. If you have any pairs in your deck when the game starts, they
will automatically be removed (Same for the computer).')
else:
print(' ')
print('Ok then, lets get into the game!')
for i in range(17):
cardDispensor = random.choice(cardList)
cardSeperater = playerHand.append(cardDispensor)
cardList.remove(cardDispensor)
computerHand = cardList
# Need to figure out how to remove duplicates from list without set()
# Giving Old Maid to random player
chooseOldMaid = randint(1,2)
if chooseOldMaid == 1:
playerHand.append('OldMaid')
if chooseOldMaid == 2:
computerHand.append('OldMaid')
print(' ')
print(computerHand)
main()