Reputation: 91
I've been trying to create a program that deals 21 cards into 3 piles. The user is then asked to think of a card and tell the program in which pile is their card. this step is repeated 4 more times until the card is found exactly in the middle of the 21 cards. The program the is supposed to go to the end()
function where it prints the users card, the problem is, everything works fine but it prints the statement in the end()
function 5 times. I know it is probably something really stupid but I can't think of a solution. Thanks in advance.
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()
Upvotes: 0
Views: 1190
Reputation: 94
By the time you get to the elif count == 4 line, count will always be 4. That's why. I got a hunch it could work if you changed the order:
...
if count == 4:
end()
break
elif count >= 0 and count <=3:
...
However, it would be even nicer if you could write it without global variables. Instead of global variables you have local ones that are passed over to the next function as arguments. Like this:
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()
Upvotes: 1
Reputation: 91
I have found a solution:
It was just a matter of an ident,
instead of:
elif count == 4:
end()
break
I put the break statement on the same line as elif:
elif count == 4:
end()
break
which seems to solve it
Upvotes: 0
Reputation: 66
You've got recursive calls. split() calls choice() which then calls split() again, which can calls main() which again calls split().
Upvotes: 2