-1

So, I'm just starting to program Python and I wanted to make a very simple script that will say something like "Gabe- Hello, my name is Gabe (Just an example of a sentence" + "Jerry- Hello Gabe, I'm Jerry" OR "Gabe- Goodbye, Jerry" + "Jerry- Goodbye, Gabe". Here's pretty much what I wrote.

 answers1 = [
"James-Hello, my name is James!"
]
answers2 = [
    "Jerry-Hello James, my name is Jerry!"
    ]
answers3 = [
    "Gabe-Goodbye, Samuel."
        ]
 answers4 = [
    "Samuel-Goodbye, Gabe"
    ]
Jack1 = (answers1 + answers2)
Jack2 = (answers3 + answers4)
Jacks = ([Jack1,Jack2])
import random
for x in range(2):
    a = random.randint(0,2)
    print (random.sample([Jacks, a]))

I'm quite sure it's a very simple fix, but as I have just started Python (Like, literally 2-3 days ago) I don't quite know what the problem would be. Here's my error message

Traceback (most recent call last):
File "C:/Users/Owner/Documents/Test Python 3.py", line 19, in <module>
print (random.sample([Jacks, a]))
TypeError: sample() missing 1 required positional argument: 'k'

If anyone could help me with this, I would very much appreciate it! Other than that, I shall be searching on ways that may be relevant to fixing this.

Florian
  • 24,425
  • 4
  • 49
  • 80
  • I posted this at 3:34 AM Central, so I'll see any comment that may appear later. But I will reply, unless it's completely unrelated, like "Whats your great great great grandmothers Maiden name?" – Midnight Rider Jan 27 '18 at 08:36
  • 1
    You've inserted a list as the argument to random.sample. Just use `random.sample(Jacks, a)` – Silver Jan 27 '18 at 08:40
  • Check [the doc for random.sample](https://docs.python.org/3/library/random.html#random.sample). – Thierry Lathuille Jan 27 '18 at 08:41

3 Answers3

0

The problem is that sample requires a parameter k that indicates how many random samples you want to take. However in this case it looks like you do not need sample, since you already have the random integer. Note that that integer should be in the range [0,1], because the list Jack has only two elements.

    a = random.randint(0,1)
    print (Jacks[a])

or the same behavior with sample, see here for an explanation.

   print (random.sample(Jacks,1))

Hope this helps!

Florian
  • 24,425
  • 4
  • 49
  • 80
0
random.sample([Jacks, a])

This sample method should looks like

random.sample(Jacks, a)

However, I am concerted you also have no idea how lists are working. Can you explain why do you using lists of strings and then adding values in them? I am losing you here.

If you going to pick a pair or strings, use method described by Florian (requesting data by index value.)

jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
Oleg Butuzov
  • 4,795
  • 2
  • 24
  • 33
-2

k parameter tell random.sample function that how many sample you need, you should write:

 print (random.sample([Jacks, a], 3))

which means you need 3 sample from your list. the output will be something like:

[1, jacks, 0]