On 06/23/2013 02:18 AM, Jack Little wrote:
I am trying to use random.choice for a text based game. I am using windows 7, 
64-bit python. Here is my code:

def lvl2():
     print "COMMANDER: Who should you train with?"
     trn=random.choice(1,2)
     if trn==1:
         lvl2_1()
         print "Squad One!"
     elif trn==2:
         lvl2_2()
         print "Squad Nine!"





Here is my error:

  File "C:\Users\Jack\Desktop\python\skye.py", line 20, in lvl2
     trn=random.choice(1,2)
TypeError: choice() takes exactly 2 arguments (3 given)





You don't say what version of Python you're using, but I'll assume 2.7

Steven's answer is correct, but here's another option:

trn = random.randint(1,2)

Here, the 1 and 2 are separate arguments delimiting a range of integer values. Note that it includes both end points, unlike the xrange function.

Alternatively, you could use

trn = random.randrange(1,3)


To make the above clearer, suppose you wanted an int value between 1 and 20, inclusive. You could do that at least four ways:

trn = random.choice([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20])

trn = random.choice(range(1, 21))

trn = random.randint(1, 20)

trn = random.randrange(1, 21)




--
DaveA
_______________________________________________
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor

Reply via email to