Follow Techotopia on Twitter

On-line Guides
All Guides
eBook Store
iOS / Android
Linux for Beginners
Office Productivity
Linux Installation
Linux Security
Linux Utilities
Linux Virtualization
Linux Kernel
System/Network Admin
Programming
Scripting Languages
Development Tools
Web Development
GUI Toolkits/Desktop
Databases
Mail Systems
openSolaris
Eclipse Documentation
Techotopia.com
Virtuatopia.com
Answertopia.com

How To Guides
Virtualization
General System Admin
Linux Security
Linux Filesystems
Web Servers
Graphics & Desktop
PC Hardware
Windows
Problem Solutions
Privacy Policy

  




 

 

Function Use

When Python evaluates odd( n ), first it evaluates n ; second, it assigns this argument value to the local parameter of odd (named spin); third, it applies odd, the suite of statements is executed, ending with return 1 or return 0; fourth, this result returned to the calling statement so that it can finish it's execution.

We would use this function like this.

s = random.randrange(37) 
# 0 <= s <= 36, single-0 roulette
if s == 0:
    print "zero"
elif odd(s):
    print s, "odd"
else:
    print s, "even"

We evaluate a function named random.randrange to create a random number, s. The if clause handles the case where s is zero. The first elif clause evaluates our odd function. To do this evaluation, Python must set spin to the value of s and execute the suite of statements that are the body of odd. The suite of statements will return either True or False. Since the if and elif clauses handle zero and odd cases, all that is left is for s to be even.


 
 
  Published under the terms of the Open Publication License Design by Interspire