Nov-28-2016, 04:00 AM
Hello again lol. As I continue my journey through learning the wonderful language of Python, I've made it to the point where I've started to learn classes. To practice and demonstrate my knowledge and learning of classes, I decided to make a random playing card generator, mostly because a friend I have who is learning Java made one. Below I've listed my code, and below the code I've listed the error statement my IDE prints out when I attempt to run it.
import random suits = ['Spade', 'Heart', 'Club', 'Diamond'] values = [2, 3, 4, 5, 6, 7, 8, 9, 10, 'Jack', 'Queen', 'King', 'Ace'] class card: 'Common base class for all cards' def __init__(self, suit, value): self.suit = suit self.value = value def drawnCard(): 'Common base function for drawn card' suitname = suits[random.choice(0, len(suits))] # here is where I think the error in the code is valuename = values[random.choice(0, len(values))] # here as well cardDrawn = card(valuename, suitname) print cardDrawn drawnCard()
Error:/System/Library/Frameworks/Python.framework/Versions/2.6/bin/python2.6 "/Users/Milli/PycharmProjects/untitled1/Classy Card Generator.py"
Traceback (most recent call last):
File "/Users/Milli/PycharmProjects/untitled1/Classy Card Generator.py", line 19, in <module>
drawncard()
File "/Users/Milli/PycharmProjects/untitled1/Classy Card Generator.py", line 14, in drawncard
suitname = suits[random.choice(0, len(suits))] # works like typename[position], but instead of you setting the positon, random.choice chooses the position for you
TypeError: choice() takes exactly 2 arguments (3 given)
Process finished with exit code 1
The second to last line in the error statement is the one I have trouble understanding, and struggle to find the problem and solve it. Error: TypeError: choice() takes exactly 2 arguments (3 given)
Last time I checked my code, I only inputted 2 arguments to both of my uses of random.choice(), unless len() turns out to be two arguments and not one. Any help that you can give is much appreciated.