Thread: Choice problem

    #1
  1. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Nov 2004
    Location
    There where the rabbits jump
    Posts
    556
    Rep Power
    12

    Exclamation Choice problem


    I have a question. "no really hehe"

    Okay when I do:

    from random import *

    choice('Hi','Bye','How are you')

    if 'Hi':
    print "Hi"

    if 'Bye':
    print "Bye"

    if 'How are you':
    print "How are you"


    the problem is that does not work if I run it, it gives me an error of 3 arguments given (1 needed).

    Could someone help me.

    I want to make a unlogical program which has all types of choices over and over (UNLOGICAL)
  2. #2
  3. Commie Mutant Traitor
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Jun 2004
    Location
    Alpharetta, GA
    Posts
    1,806
    Rep Power
    1570
    You're going to have to explain better just what you mean. The description you give is itself 'unlogical', and I can figure out what you are looking for.

    If I understand correctly, you want to be able to randomly select one of several choices from a set of alternatives. Is that right? If so, then the best approach would probably be a lookup table of some kind. There happens to be a very useful idiom in Python, described here, that should be useful for this purpose. As an example of how to use this for what you want, try this:

    Code:
    import random;
    
    def choose():
    	random.seed();  # use the current system time as a random seed
    	chance = random.randint(0, 5);
    	
    	result = {0: lambda x: "zeroth",
    	 1: lambda x: "first",
    	 2: lambda x: "second",
    	 3: lambda x: "third",
    	 4: lambda x: "fourth",
    	 5: lambda x: "fifth"}[chance](1);
    	print result;
    	
    choose();
    This has been tested, and randomly prints either "zeroth", "first", "second", "third", "fourth", "fifth", depending on the result of randint().
  4. #3
  5. Mini me.
    Devshed Novice (500 - 999 posts)

    Join Date
    Nov 2003
    Location
    Cambridge, UK
    Posts
    783
    Rep Power
    14
    You've got the basic idea but have missed out a few things

    choice returns a value and you want to test that value in several places in your code.

    Also - all of your if statements will be executed because thay all evaluate to a none zero result. You need to do a test.

    So assign the result of calling the choice function to a variable like this and test the variable:

    Code:
    import random
    
    choices = ['hi','bye','hello','so long']
    
    mychoice = random.choice(choices)
    
    if mychoice == 'hi':
       #do the hio code
        print 'Hi'
    elif mychoice == 'bye':
      #do the bye code
    elif mychoice == 'so long':
       .....
    Don't use import * it is a bad habit to start.

    grimey
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Nov 2004
    Location
    There where the rabbits jump
    Posts
    556
    Rep Power
    12
    Why is import * bad habit i mean u have to type less!

    Then i'll try that thing with an other object or what ever u wanna call it
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Feb 2004
    Location
    London, England
    Posts
    1,585
    Rep Power
    1373
    Originally Posted by monkeyman23555
    Why is import * bad habit i mean u have to type less!
    Using the equivalent of import * is common in the Java community, but it makes it extremely difficult to navigate round large programs.

    For example, imagine you have a large project with code like this:

    Code:
    from foo import *
    from bar import *
    from wibble import *
    #and so on for another 20 import statements...
    
    #
    # lots of code here...
    #
    
    def somefunction(stuff):
        #blah blah blah
        veryImportantFunction(stuff)
    Now you want to see what veryImportantFunction does. It is not defined in this file, so it must have been imported from somewhere. But where? You now have to search through numerous modules to find the function you want. If you imported the function explicitly with

    from somemodule import veryImportantFunction

    or just imported the module and referred to the function as

    somemodule.veryImportantFunction

    then you would know exactly it you came from. In fact some Python IDEs are able to use this knowledge to show you useful information about the function, or to open it in the editor with a single click.

    The problems with 'import *' do not stop there. What if there is another module that has a function with the same name? In Java this will generate a compiler error, but in Python one version of the function will silently overwrite the other in your namespace, and which function you are calling will depend on the order that the modules are imported. This can lead to very strange and hard to track down bugs.

    Dave - The Developers' Coach
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Nov 2004
    Location
    There where the rabbits jump
    Posts
    556
    Rep Power
    12
    okay i get it and the script worked thanks guys

    I am learning!!!!

IMN logo majestic logo threadwatch logo seochat tools logo