我一直在试图创建一个程序,将21张卡交易成3堆。然后要求用户考虑一张卡片并告诉程序他们的卡片是哪一堆。这一步再重复4次,直到在21张卡的正中找到卡。该程序应该去end()
功能打印用户卡,问题是,一切工作正常,但它打印在end()
函数声明5次。我知道这可能是一件非常愚蠢的事情,但我想不出一个解决方案。提前致谢。卡技巧程序
import random
cards = []
count = 0
def end():
print("Your card is: ", cards[10])
def split():
def choice():
global count
while True:
if count >=0 and count <= 3:
global cards
user = input("Think of a card. Now to which pile does your card belong to?: ")
count = count + 1
if user == "1":
del cards[:]
cards = (pile_2 + pile_1 + pile_3)
print(cards)
split()
elif user == "2":
del cards[:]
cards = (pile_1 + pile_2 + pile_3)
print(cards)
split()
elif user == "3":
del cards[:]
cards = (pile_1 + pile_3 + pile_2)
print(cards)
split()
else:
print("Invalid input")
main()
elif count == 4:
end()
break
pile_1 = []
pile_2 = []
pile_3 = []
counter = 0
sub_counter = 0
while True:
if sub_counter >= 0 and sub_counter <= 20:
for item in cards:
if counter == 0:
pile_1.append(item)
counter = counter + 1
elif counter == 1:
pile_2.append(item)
counter = counter + 1
elif counter == 2:
pile_3.append(item)
counter = 0
sub_counter = sub_counter + 1
elif sub_counter == 21:
False
break
print()
print("first pile: ", pile_1)
print("second pile: ", pile_2)
print("third pile: ", pile_3)
choice()
def main():
file = open('cards.txt.', 'r')
for line in file:
cards.append(line)
file.close
random.shuffle(cards)
print(cards)
split()
main()
你可以请加一些'cards.txt'文件,并添加一些评论? – Yonlif