[Tutor] random.choice() problem

2013-06-23 Thread Jack Little
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)
 



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


Re: [Tutor] random.choice() problem

2013-06-23 Thread Steven D'Aprano

On 23/06/13 16:18, 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)

[...]


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)



Alas, here you have stumbled over one of those corners of Python where things do not work 
*quite* as well as they ought to. Even though random.choice looks like a function, it is 
actually a method, and methods have an extra argument, automatically generated by Python, 
called self.

So when you call random.choice(1, 2) Python provides three, not two, arguments:

self, 1, 2

hence the error message about 3 given. Since random.choice takes two 
arguments, and Python provides one of them, that only leaves one argument for you to 
provide. What should that be? Well, if you read the documentation, it tells you. At the 
interactive interpreter, you can enter

help(random.choice)


which will give you:

Help on method choice in module random:

choice(self, seq) method of random.Random instance
Choose a random element from a non-empty sequence.


Ignoring self, you have to give a *sequence* of values. A list is a good 
sequence to use:

# Incorrect:
random.choice(1, 2)


# Correct:
random.choice([1, 2])



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


Re: [Tutor] random.choice() problem

2013-06-23 Thread Dave Angel

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


Re: [Tutor] random.choice() problem

2013-06-23 Thread Peter Otten
Dave Angel wrote:

 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)

 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.

Here's yet another option: if you move the print statements into the 
lvl2_...() functions you can simplify your code by choosing the function 
directly:

 import random
 def level2_1():
... # ...
... print Squad One!
... 
 def level2_2():
... # ...
... print Squad Nine!
... 
 def level2():
... print COMMANDER: Who should you train with?
... level2_x = random.choice([level2_1, level2_2])
... level2_x()
... 
 level2()
COMMANDER: Who should you train with?
Squad One!
 level2()
COMMANDER: Who should you train with?
Squad Nine!
 level2()
COMMANDER: Who should you train with?
Squad Nine!


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