Forums: » Register « |  Free Tools |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support |

New Free Tools on Dev Shed!

#1
December 10th, 2004, 04:17 PM
 monkeyman23555
Contributing User

Join Date: Nov 2004
Location: There where the rabbits jump
Posts: 556
Time spent in forums: 2 Days 23 h 24 m 55 sec
Reputation Power: 11
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
December 10th, 2004, 06:16 PM
 Schol-R-LEA
Commie Mutant Traitor

Join Date: Jun 2004
Location: Norcross, GA (again)
Posts: 1,798
Time spent in forums: 1 Month 2 Weeks 3 Days 1 h 39 m 19 sec
Reputation Power: 1569
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().

#3
December 10th, 2004, 08:53 PM
 Grim Archon
Mini me.

Join Date: Nov 2003
Location: Cambridge, UK
Posts: 783
Time spent in forums: 3 Days 2 h 15 m 57 sec
Reputation Power: 13
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
__________________
*** Experimental Python Markup CGI V2 ***

#4
December 11th, 2004, 05:26 AM
 monkeyman23555
Contributing User

Join Date: Nov 2004
Location: There where the rabbits jump
Posts: 556
Time spent in forums: 2 Days 23 h 24 m 55 sec
Reputation Power: 11
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

#5
December 11th, 2004, 06:58 AM
 DevCoach
Contributing User

Join Date: Feb 2004
Location: London, England
Posts: 1,585
Time spent in forums: 2 Weeks 4 Days 2 h 58 m 23 sec
Reputation Power: 1372
Quote:
 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

#6
December 11th, 2004, 12:21 PM
 monkeyman23555
Contributing User

Join Date: Nov 2004
Location: There where the rabbits jump
Posts: 556
Time spent in forums: 2 Days 23 h 24 m 55 sec
Reputation Power: 11
okay i get it and the script worked thanks guys

I am learning!!!!

 Viewing: Dev Shed Forums > Programming Languages > Python Programming > Choice problem