P: 3
|
Please don't critique my algorithm as I'm a rank newbie (or go ahead, I can take it :) ). But I can't figure the following out. -
def shuffle():
-
""" Return a list of indices from 0-51 with no repeats."""
-
index = range (0,52)
-
shuffled_list = list() # or shuffled_list = [] does the same
-
while index:
-
card = random.choice(index)
-
index.remove(card)
-
shuffled_list.append(card)
-
# print shuffled_list
-
return shuffled_list
-
shuffled_list always seems to be defined as () when I look in the debugger, so I continually get
File "C:\Python25\cards.py", line 29, in shuffle
while index:
AttributeError: 'tuple' object has no attribute 'append'
I have no idea whats wrong here.
| |
Share this Question
P: 21
|
Hi
I am also new to python.
I tried your code, it works fine for me.
(I use Python 2.5).
Regards,
Prasannaa
| |
P: 3
|
Hi
I am also new to python.
I tried your code, it works fine for me.
(I use Python 2.5).
Regards,
Prasannaa
Well, interestingly, I exited Winpython, restarted it and it works for me too now. I fixed one problem in preceeding code at the same time. My C background had intruded and I had
Does anyone know what that causes? I got no errors but perhaps it messed something up in the intepreter and it couldn't recover?
Thanks!
| | Expert Mod 2.5K+
P: 2,851
|
Well, interestingly, I exited Winpython, restarted it and it works for me too now. I fixed one problem in preceeding code at the same time. My C background had intruded and I had
Does anyone know what that causes? I got no errors but perhaps it messed something up in the intepreter and it couldn't recover?
Thanks!
Since 'list()' returns '[]', I cannot explain why you had the tuple problem. Your algorithm looks fine. For the record, the random module has other functions that do basically the same thing: - >>> deck = range(0,52)
-
>>> random.shuffle(deck)
-
>>> deck
-
[41, 10, 23, 46, 14, 25, 5, 37, 9, 11, 3, 47, 1, 49, 22, 20, 29, 15, 51, 8, 4, 18, 17, 26, 36, 43, 34, 0, 48, 2, 50, 6, 16, 33, 28, 24, 38, 44, 35, 27, 42, 31, 7, 45, 40, 12, 13, 21, 19, 39, 32, 30]
-
>>>
OR - >>> random.sample(range(0,52), 52)
-
[10, 26, 7, 45, 46, 48, 16, 1, 34, 32, 6, 35, 30, 18, 0, 21, 49, 8, 40, 29, 4, 36, 11, 15, 42, 28, 5, 3, 25, 27, 20, 9, 43, 17, 24, 39, 22, 33, 38, 2, 47, 51, 50, 13, 12, 44, 19, 31, 37, 14, 41, 23]
-
>>>
| |
P: 3
|
Since 'list()' returns '[]', I cannot explain why you had the tuple problem. Your algorithm looks fine. For the record, the random module has other functions that do basically the same thing: - >>> deck = range(0,52)
-
>>> random.shuffle(deck)
-
>>> deck
-
[41, 10, 23, 46, 14, 25, 5, 37, 9, 11, 3, 47, 1, 49, 22, 20, 29, 15, 51, 8, 4, 18, 17, 26, 36, 43, 34, 0, 48, 2, 50, 6, 16, 33, 28, 24, 38, 44, 35, 27, 42, 31, 7, 45, 40, 12, 13, 21, 19, 39, 32, 30]
-
>>>
OR - >>> random.sample(range(0,52), 52)
-
[10, 26, 7, 45, 46, 48, 16, 1, 34, 32, 6, 35, 30, 18, 0, 21, 49, 8, 40, 29, 4, 36, 11, 15, 42, 28, 5, 3, 25, 27, 20, 9, 43, 17, 24, 39, 22, 33, 38, 2, 47, 51, 50, 13, 12, 44, 19, 31, 37, 14, 41, 23]
-
>>>
EXCELLENT, thanks!
| | Expert 5K+
P: 6,596
|
Well, interestingly, I exited Winpython, restarted it and it works for me too now.
I've heard of this type of old vs new namespace problem in PythonWin (never experienced it since I only used that IDE for a day or two). There are issues with import vs reload (I never use those either) IDLE's Run Module has always worked for me. Perhaps you weren't using PythonWin's Run command???
I fixed one problem in preceeding code at the same time. My C background had intruded and I had
Does anyone know what that causes? I got no errors but perhaps it messed something up in the intepreter and it couldn't recover?
Thanks!
Python will create an empty list. But then, if you
(or something) Python simply re-assigns the name to the new object and if that was the only reference to the empty list, it is marked for Garbage Collection.
| | | | Question stats - viewed: 1854
- replies: 5
- date asked: May 4 '07
|