简体   繁体   中英

Why won't this while loop, loop?

I'm trying to make a program, that goes randomly through a stack of cards for 'i' amount of decks. However my while loop seems to stop after 1 run? How come it won't loop?

cards = ['Two of Hearts', 'Two of Diamonds', 'Two of Spades', 'Two of Clubs', 'Three of Hearts', 'Three of Diamonds', 'Three of Spades', 'Three of Clubs', 'Four of Hearts', 'Four of Diamonds', 'Four of Spades', 'Four of Clubs', 'Five of Hearts', 'Five of Diamonds', 'Five of Spades', 'Five of Clubs', 'Six of Hearts', 'Six of Diamonds', 'Six of Spades', 'Six of Clubs', 'Seven of Hearts', 'Seven of Diamonds', 'Seven of Spades', 'Seven of Clubs', 'Eight of Hearts', 'Eight of Diamonds', 'Eight of Spades', 'Eight of Clubs', 'Nine of Hearts', 'Nine of Diamonds', 'Nine of Spades', 'Nine of Clubs', 'Ten of Hearts', 'Ten of Diamonds', 'Ten of Spades', 'Ten of Clubs', 'Jack of Hearts', 'Jack of Diamonds', 'Jack of Spades', 'Jack of Clubs', 'Queen of Hearts', 'Queen of Diamonds', 'Queen of Spades', 'Queen of Clubs', 'King of Hearts', 'King of Diamonds', 'King of Spades', 'King of Clubs', 'Ace of Hearts', 'Ace of Diamonds', 'Ace of Spades', 'Ace of Clubs']

i = 0

while i <= 5:
    cardsTemp = cards
    for n in cardsTemp:
        card = random.randint(1,len(cardsTemp)) - 1
        print(cardsTemp[card])
        print('cardsTempt len: ' + str(len(cardsTemp)))
        cardsTemp.remove(cardsTemp[card])
    i += 1

You can do this much more easily using for loops. This is what the range() function is for:

import random

cards = ['Two of Hearts', 'Two of Diamonds', 'Two of Spades', 'Two of Clubs', 'Three of Hearts', 'Three of Diamonds', 'Three of Spades', 'Three of Clubs', 'Four of Hearts', 'Four of Diamonds', 'Four of Spades', 'Four of Clubs', 'Five of Hearts', 'Five of Diamonds', 'Five of Spades', 'Five of Clubs', 'Six of Hearts', 'Six of Diamonds', 'Six of Spades', 'Six of Clubs', 'Seven of Hearts', 'Seven of Diamonds', 'Seven of Spades', 'Seven of Clubs', 'Eight of Hearts', 'Eight of Diamonds', 'Eight of Spades', 'Eight of Clubs', 'Nine of Hearts', 'Nine of Diamonds', 'Nine of Spades', 'Nine of Clubs', 'Ten of Hearts', 'Ten of Diamonds', 'Ten of Spades', 'Ten of Clubs', 'Jack of Hearts', 'Jack of Diamonds', 'Jack of Spades', 'Jack of Clubs', 'Queen of Hearts', 'Queen of Diamonds', 'Queen of Spades', 'Queen of Clubs', 'King of Hearts', 'King of Diamonds', 'King of Spades', 'King of Clubs', 'Ace of Hearts', 'Ace of Diamonds', 'Ace of Spades', 'Ace of Clubs']

for i in range(5):
    print('\n   LOOP {}'.format(i))

    cardsTemp = cards[:]
    for n in range(len(cards)):
        card = random.randint(1,len(cardsTemp)) - 1
        print(cardsTemp[card])
        print('cardsTempt len: ' + str(len(cardsTemp)))
        cardsTemp.remove(cardsTemp[card])

Try copying the orignal cards into cards temp inside the while loop.

while i <= 5:
    cardsTemp = cards.copy()
    for n in cardsTemp.copy():
        card = random.randint(1,len(cardsTemp)) - 1
        print(cardsTemp[card])
        print('cardsTempt len: ' + str(len(cardsTemp)))
        cardsTemp.remove(cardsTemp[card])
    i += 1

My fault was that I didn't copy the cards list into the cardsTemp. When doing this, it perfectly looped, however the for loop stopped after 25 cards for some reason. I fixed this by making a while loop within the while loop.

This worked for me. Thanks for the answers!

cards = ['Two of Hearts', 'Two of Diamonds', 'Two of Spades', 'Two of Clubs', 'Three of Hearts', 'Three of Diamonds', 'Three of Spades', 'Three of Clubs', 'Four of Hearts', 'Four of Diamonds', 'Four of Spades', 'Four of Clubs', 'Five of Hearts', 'Five of Diamonds', 'Five of Spades', 'Five of Clubs', 'Six of Hearts', 'Six of Diamonds', 'Six of Spades', 'Six of Clubs', 'Seven of Hearts', 'Seven of Diamonds', 'Seven of Spades', 'Seven of Clubs', 'Eight of Hearts', 'Eight of Diamonds', 'Eight of Spades', 'Eight of Clubs', 'Nine of Hearts', 'Nine of Diamonds', 'Nine of Spades', 'Nine of Clubs', 'Ten of Hearts', 'Ten of Diamonds', 'Ten of Spades', 'Ten of Clubs', 'Jack of Hearts', 'Jack of Diamonds', 'Jack of Spades', 'Jack of Clubs', 'Queen of Hearts', 'Queen of Diamonds', 'Queen of Spades', 'Queen of Clubs', 'King of Hearts', 'King of Diamonds', 'King of Spades', 'King of Clubs', 'Ace of Hearts', 'Ace of Diamonds', 'Ace of Spades', 'Ace of Clubs']

i = 0

while i <= 1:
    print('\n   LOOP {}'.format(i))

    cardsTemp = cards.copy()
    n = 0

    while n < len(cardsTemp):
        card = random.randint(1,len(cardsTemp)) - 1
        print(cardsTemp[card])
        print('cardsTempt len: ' + str(len(cardsTemp)))
        cardsTemp.remove(cardsTemp[card])

    i += 1

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM