Python Forum
Thread Rating:
  • 2 Vote(s) - 3 Average
  • 1
  • 2
  • 3
  • 4
  • 5
card 21 trick
#1
Hi, I have a coding project for school which involves me making a program to do the 21 card trick in python.
I have started to make the program but am stuck on why it is not running, here is what I have at the moment.


first = ("JS","4C","KC","3H","5H","AD","10H")
second = ("9H","7S","JD","6D","JC","2D","QC")
third = ("5C","JH","4S","QS","8D","9D","4H")

def packet(first,second,third):
  for i in range(0,21,3):
    first.insert(0, packet[i])
    second.insert(0, packet[i+1])
    third.insert(0, packet[i+2])

print("1: " + " ".join([str(card) for card in first]))
print("2: " + " ".join([str(card) for card in second]))
print("3: " + " ".join([str(card) for card in third]))

selection = int(input("> "))
if selection == 1:
  packet.extend(second)
  packet.extend(first)
  packet.extend(third)
elif selection == 2:
  packet.extend(first)
  packet.extend(second)
  packet.extend(third)
elif selection == 3:
  packet.extend(first)
  packet.extend(third)  
  packet.extend(second)

print("You're thinking of the %s" % packet[10])
If someone can help me fix this so it does the card trick it will be greatly appreciated.
Thanks
#2
(Aug-16-2017, 05:50 PM)SummoningZ Wrote: but am stuck on why it is not running

What does that mean?  Is there an error?  What is the error?

Can I guess that the error is that there's no indentation?  Also, packet.extend() doesn't make sense, since packet is a function.
#3
(Aug-16-2017, 05:50 PM)SummoningZ Wrote:  

The error is "AttributeError: 'function' object has no attribute 'extend'
The site wouldnt let me put indents in thats why its not showing.
#4
(Aug-16-2017, 08:48 PM)SummoningZ Wrote: The error is "AttributeError: 'function' object has no attribute 'extend'"
Which is what I said :)

As for pasting code, stick it in [ python] tags, and either right click and "paste without formatting" or ctrl+shift+v to do the same.  The forum 100% allows indentation in code blocks:
def spam():
    return "eggs"

for _ in range(5):
    print(spam())
#5
How do I fix this problem, please help I need it by next week I have been trying for at least 2 weeks.

(Aug-16-2017, 09:08 PM)nilamo Wrote:
(Aug-16-2017, 08:48 PM)SummoningZ Wrote: The error is "AttributeError: 'function' object has no attribute 'extend'"
Which is what I said :)

As for pasting code, stick it in [ python] tags, and either right click and "paste without formatting" or ctrl+shift+v to do the same.  The forum 100% allows indentation in code blocks:
def spam():
    return "eggs"

for _ in range(5):
    print(spam())


first = ("JS","4C","KC","3H","5H","AD","10H")
second = ("9H","7S","JD","6D","JC","2D","QC")
third = ("5C","JH","4S","QS","8D","9D","4H")

def packet(first,second,third):
  for i in range(0,21,3):
    first.insert(0, packet[i])
    second.insert(0, packet[i+1])
    third.insert(0, packet[i+2])

print("1: " + " ".join([str(card) for card in first]))
print("2: " + " ".join([str(card) for card in second]))
print("3: " + " ".join([str(card) for card in third]))

selection = int(input("> "))
if selection == 1:
  packet.extend(second)
  packet.extend(first)
  packet.extend(third)
elif selection == 2:
  packet.extend(first)
  packet.extend(second)
  packet.extend(third)
elif selection == 3:
  packet.extend(first)
  packet.extend(third)  
  packet.extend(second)

print("You're thinking of the %s" % packet[10])
Thats the code.
#6
As @nilamo pointed out, you've defined "packet" as a function and as such has no attribute ".extend", lists, however do. Have no idea what you are trying to do with "packet" within your for loop. Your function has no "return", that may turn out to be helpful.
If it ain't broke, I just haven't gotten to it yet.
OS: Windows 10, openSuse 42.3, freeBSD 11, Raspian "Stretch"
Python 3.6.5, IDE: PyCharm 2018 Community Edition
#7
(Aug-17-2017, 12:38 AM)sparkz_alot Wrote: As @nilamo pointed out, you've defined "packet" as a function and as such has no attribute ".extend", lists, however do. Have no idea what you are trying to do with "packet" within your for loop. Your function has no "return", that may turn out to be helpful.

So how do I add the attribute ".extend" into the code and where would I put the return?

Thanks
#8
You use the ".extend" by using lists. So for example, if "first", "second" and "third" were lists rather than tuples, you could use ".extend" with them, ie "first.extend(iterable)". The "return" is placed at the end of the function and in general returns the results of what happens within the function.

def mult(x, y):
    result = x * y
    return result

no_1 = 2
no_2 = 4
product = mult(no_1, no_2)
print(product)
In your situation, it's not just that you know how the trick is played, but also the math on which the trick is based. Here is a pretty good explanation of that math: Math behind 21 Card Trick.

If it were me, I would start with a main list of all 21 cards, and first, second, third start as empty lists which could be filled at a later time (within a function), you need to deal the cards, display the cards, ask for user choice, collect the cards (each step 3 times), finally reveal the eleventh card.

To be honest, I can't help but feel that either your answer or perhaps some snippets from the assignment are based on the program by dylanfw on GH.
If it ain't broke, I just haven't gotten to it yet.
OS: Windows 10, openSuse 42.3, freeBSD 11, Raspian "Stretch"
Python 3.6.5, IDE: PyCharm 2018 Community Edition
#9
(Aug-17-2017, 12:52 PM)sparkz_alot Wrote: You use the ".extend" by using lists. So for example, if "first", "second" and "third" were lists rather than tuples, you could use ".extend" with them, ie "first.extend(iterable)". The "return" is placed at the end of the function and in general returns the results of what happens within the function.

def mult(x, y):
    result = x * y
    return result

no_1 = 2
no_2 = 4
product = mult(no_1, no_2)
print(product)
In your situation, it's not just that you know how the trick is played, but also the math on which the trick is based. Here is a pretty good explanation of that math: Math behind 21 Card Trick.

If it were me, I would start with a main list of all 21 cards, and first, second, third start as empty lists which could be filled at a later time (within a function), you need to deal the cards, display the cards, ask for user choice, collect the cards (each step 3 times), finally reveal the eleventh card.

To be honest, I can't help but feel that either your answer or perhaps some snippets from the assignment are based on the program by dylanfw on GH.
Yes it is based on it because I have no clue on what to do at all and every course I have taken doesnt help either and now I still dont get it so I dont know what to do anymore to be honest.
#10
(Aug-17-2017, 12:52 PM)sparkz_alot Wrote: You use the ".extend" by using lists. So for example, if "first", "second" and "third" were lists rather than tuples, you could use ".extend" with them, ie "first.extend(iterable)". The "return" is placed at the end of the function and in general returns the results of what happens within the function.

def mult(x, y):
    result = x * y
    return result

no_1 = 2
no_2 = 4
product = mult(no_1, no_2)
print(product)
In your situation, it's not just that you know how the trick is played, but also the math on which the trick is based. Here is a pretty good explanation of that math: Math behind 21 Card Trick.

If it were me, I would start with a main list of all 21 cards, and first, second, third start as empty lists which could be filled at a later time (within a function), you need to deal the cards, display the cards, ask for user choice, collect the cards (each step 3 times), finally reveal the eleventh card.

To be honest, I can't help but feel that either your answer or perhaps some snippets from the assignment are based on the program by dylanfw on GH.

Can anyone show me what this would look like?


Possibly Related Threads…
Thread Author Replies Views Last Post
  Turtle Graphics Card Values in a List Muzz 0 2,346 Apr-11-2019, 12:55 PM
Last Post: Muzz
  card dealer school project kalle1234 5 10,927 Jan-05-2019, 09:21 PM
Last Post: ichabod801
  Playing Card Sorting ness828 4 75,722 Feb-05-2018, 09:01 PM
Last Post: sparkz_alot

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020