我正在嘗試制作一個程式,該程式隨機通過一堆卡片來獲取“i”數量的套牌。但是,我的 while 回圈似乎在 1 次運行后停止了?怎么不會回圈?
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
uj5u.com熱心網友回復:
for您可以使用回圈更輕松地做到這一點。這是該range()功能的用途:
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])
uj5u.com熱心網友回復:
我的錯是我沒有將卡片串列復制到cardsTemp中。這樣做時,它完美地回圈了,但是 for 回圈由于某種原因在 25 張牌后停止了。我通過在 while 回圈中創建一個 while 回圈來解決這個問題。
這對我有用。感謝您的回答!
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
uj5u.com熱心網友回復:
嘗試將原始卡片復制到 while 回圈內的卡片 temp 中。
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
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/438461.html
標籤:Python python-3.x 循环 while循环
下一篇:洗掉購物車和本地存盤中的產品
