2017-12-02 142 views
0

我一直在試圖創建一個程序,將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() 
+0

你可以請加一些'cards.txt'文件,並添加一些評論? – Yonlif

回答

0

當你到達elif count == 4行時,count總是4.這就是爲什麼。我有一種預感,如果你的順序改變了它可以工作:

... 
if count == 4: 
    end() 
    break 
elif count >= 0 and count <=3: 
    ... 

但是,它會更好,如果你可以把它寫不全局變量。而不是全局變量你有地方的那些作爲參數傳遞給下一個函數。就像這樣:

import random 

def end(cards): 
    print("Your card is: ", cards[10]) 

def choice(count,pile_1,pile_2,pile_3): 
    while True: 
     user = input("Think of a card. Now to which pile does your card belong to?: ") 
     if user == "1": 
      cards = (pile_2 + pile_1 + pile_3) 
      print(cards) 
      split(count+1, cards) 
      break 
     elif user == "2": 
      cards = (pile_1 + pile_2 + pile_3) 
      print(cards) 
      split(count+1, cards) 
      break 
     elif user == "3": 
      cards = (pile_1 + pile_3 + pile_2) 
      print(cards) 
      split(count+1, cards) 
      break 
     else: 
      print("Invalid input") 

def split(count,cards): 
    if count == 4: 
     end(cards) 
     return 
    pile_1 = [] 
    pile_2 = [] 
    pile_3 = [] 
    for i in range(0,21,3): 
     pile_1.append(cards[i]) 
     pile_2.append(cards[i+1]) 
     pile_3.append(cards[i+2]) 
    print() 
    print("first pile: ", pile_1) 
    print("second pile: ", pile_2) 
    print("third pile: ", pile_3) 
    choice(count,pile_1,pile_2,pile_3) 

def main(): 
    cards = [] 
    file = open('cards.txt.', 'r') 
    for line in file: 
    cards.append(line.strip()) 
    file.close 
    random.shuffle(cards) 
    print(cards) 
    split(0, cards) 

main() 
+0

對不起,我不完全明白你寫的沒有全局變量的意思,用什麼方式?對不起,我的知識很模糊。 – SzczureX

+0

我改變了我的答案,使其更清晰。股份公司漂亮的卡牌戲法b.t.w. – SchEma

2

你有遞歸調用。 split()調用choice(),然後再調用split(),它可以調用main(),它再次調用split()。

0

我已經找到了解決辦法:

這僅僅是一個的ident的事,

代替:

elif count == 4: 
     end() 
     break 

我把break語句上與elif相同的行:

elif count == 4: 
    end() 
break 

這似乎解決了它