Python Forum
card 21 trick - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Homework (https://python-forum.io/forum-9.html)
+--- Thread: card 21 trick (/thread-4440.html)

Pages: 1 2 3


card 21 trick - SummoningZ - Aug-16-2017

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


RE: I need help with my coding - Python 3 - nilamo - Aug-16-2017

(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.


RE: I need help with my coding - Python 3 - SummoningZ - Aug-16-2017

(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.


RE: I need help with my coding - Python 3 - nilamo - Aug-16-2017

(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())



RE: I need help with my coding - Python 3 - SummoningZ - Aug-16-2017

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.


RE: I need help with my coding - Python 3 - sparkz_alot - Aug-17-2017

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.


RE: I need help with my coding - Python 3 - SummoningZ - Aug-17-2017

(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


RE: I need help with my coding - Python 3 - sparkz_alot - Aug-17-2017

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.


RE: I need help with my coding - Python 3 - SummoningZ - Aug-17-2017

(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.


RE: I need help with my coding - Python 3 - SummoningZ - Aug-18-2017

(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?