2

I am trying to append a set of objects combined into one as a single object on the end of a list. Is there any way I could achieve this?

I've tried using multiple arguments for .append and tried searching for other functions but I haven't found any so far.

yourCards = []
cards =["Ace","Two","Three","Four","Five","Six","Seven","Eight","Nine","Ten","Jack","Queen","King"]
suits = ["Hearts","Diamonds","Clubs","Spades"]

yourCards.append(cards[random.randint(0,12)],"of",suits[random.randint(0,3)])

I expected the list to have a new element simply as "Two of Hearts" etc. but instead I recieve this error:

TypeError: append() takes exactly one argument (3 given)
David Latimer
  • 65
  • 2
  • 9
  • 1
    You supplied three arguments to `append`, but `append` only accepts one argument. Notice the commas in your argument list? – Robert Harvey Jan 18 '19 at 21:32
  • Do you mean you're trying to build the string `"Two of Hearts"`? How would passing separate arguments achieve that? Do that *before* you pass it to append. Also note the existence of `random.choice`, and that there's nothing to stop you having multiple copies of the same card. – jonrsharpe Jan 18 '19 at 21:32
  • You can use `extend` instead: `yourCards.append((cards[random.randint(0,12)],"of",suits[random.randint(0,3)]))`. – a_guest Jan 18 '19 at 21:33
  • @a_guest I think they want to add a single value that combines the three parts. – jonrsharpe Jan 18 '19 at 21:33
  • 2
    Maybe you got this idea from the `print` function? It joins arguments you pass to it with spaces, but that’s not how function calls work in general; that’s just a convenience for printing. When you have two string objects, you can concatenate them with the `+` operator: `x + " of " + y`. – Ry- Jan 18 '19 at 21:35
  • 1
    why not just add them with a `+`? like `yourCards.append(cards[random.randint(0,12)]+'of'+suits[random.randint(0,3)])`? – anishtain4 Jan 18 '19 at 21:35
  • How would I concate strings (sorry I'm new) – David Latimer Jan 18 '19 at 21:36
  • Just like the comments above describe. Add them with + signs. – Robert Harvey Jan 18 '19 at 21:36

2 Answers2

4

You are sending append() multiple arguments not a string. Format the argument as a string as such. Also, random.choice() is a better approach than random.randint() here as stated by: @JaSON below.

3.6+ using f-strings

yourCards.append(f"{random.choice(cards)} of {random.choice(suites)}")

Using .format()

yourCards.append("{} of {}".format(random.choice(cards), random.choice(suites)))

string concatenation

yourCards.append(str(random.choice(cards)) + " of " + str(random.choice(suites)))
#You likely don't need the str() but it's just a precaution

Improving on Alex's join() approch

' of '.join([random.choice(cards), random.choice(suites)])
Jab
  • 26,853
  • 21
  • 75
  • 114
1
yourCards.append(' '.join([random.choice(cards), "of", random.choice(suits)]))
Alex F
  • 756
  • 2
  • 9
  • 24
  • 1
    I like this one too. but could be improved using `' of '.join([cards[random.randint(0, 12)], suits[random.randint(0, 3)]])` – Jab Jan 18 '19 at 21:41
  • Thanks! I like keeping the "of" in order so the code also forms a sentence. – Alex F Jan 19 '19 at 13:31