I have this computer program that’s supposed to draw a Hand of Mahjong Tiles from a deck:
# imports random
import itertools, random
# make a deck of tiles
deck = list(itertools.product(range(1, 5), range(1,10), range(0,3)))
deckWinds=(list(itertools.product(range(1, 5), range(10,14), range(3,4))))
deckDragons=(list(itertools.product(range(1, 5), range(14,17), range(4,5))))
deck=deck+deckWinds+deckDragons
# shuffles the deck
random.shuffle(deck)
#have a dictionary of suit names and number names
numbers={10: "E", 11: "S", 12: "W", 13: "N", 14: "White", 15: "Green", 16: "Red"}
suits={0: "Wan", 1: "So", 2: "Pin", 3: "Wind", 4: "Dragon"}
#which card is to be drawn
cardNumber=1
# deal myHand
myHand=[]
for i in range(14):
drawn=deck[cardNumber]
cardNumber=cardNumber+1
myHand.append(drawn)
myHand.sort(key=lambda t: (t[2], t[1]))
print(myHand)
visibleHand=[]
# make readable UI version
for i in range(len(myHand)):
tile, number, suit = myHand[i] # unpack the tuple to have cleaner code
visibleHand.append(f"{numbers.get(number, number)} {suits.get(suit, suit)}")
print(visibleHand)
On Online Python it works fine, but on Trinket it gives me a series of error messages, starting with SyntaxError: bad input on line 33 in main.py
, and when I make that line a comment to see if there are any other problems, it reads AttributeError: '<invalid type>' object has no attribute 'product' on line 5 in main.py
. Are there any differences between the two that might cause this?
Anonymous is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
4
I bit the bullet and registered on Trinket.io. What they’re running as a Python interpreter is nowhere near standard Python, as running
import itertools
print(itertools)
print(dir(itertools))
prints out
<unknown>
['__doc__', '__name__', '__package__', 'permutations']
IOW, itertools
is an <unknown>
object, and really only contains permutations
(not the rest of what we’d expect itertools
to in regular Python world).
import sys
print(sys.copyright)
prints out
Copyright 2009-2010 Scott Graham.
All Rights Reserved.
which is a hint they’re running Skulpt, but with the sys.version
and sys.version_info
values removed so you can’t tell more clearly.
So – to answer “Are there any differences between the two that might cause this?”: yes, apparently. Are there other differences we can’t know about? Very likely.
3