A general thread for help with coding.

To start off: I'm trying to write a parser for statistical distributions, however I have run into a minor problem. When I use the parser to find the expected value of the distribution, it gives me the variance. I honestly cannot see my fault, especially seeing as how just using my modules for any distribution works like a charm.

`"""Parser for Statistical Distributions"""`

from PoiDist import * #Imports Poisson functions

from BinDist import * #Imports Binomial functions

from UniDist import * #Imports Uniform Continuous functions

global DistDict #Dictionary to store distributions

DistDict = {} #Sets up dictionary

class Parser():

def __init__(self):

pass

def parse(self, string): #Parse function for parser

if "~" in string: #If input contains a tilde key, denoting a new distribution

listA = string.split("~") #Creates a list split on the tilde, allowing separation of name

name = listA[0] #Declares variable name

listB = listA[1].split("(") #Splits ListA[1] to allow the type of distribution to be found

type = str(listB[0]) #Declares variable type

if type == "B": #If a binomial distribution

listC = listB[1].split(",") #Splits listC to show number of trials and probability of success

n = str(listC[0]) #Sets n to number of trials

n = int(n) #Converts n to integer

p = str(listC[1][:-1]) #Sets p to probability of success

p = float(p) #Converts p to float

DistDict[name] = B(n, p) #Adds distribution to the dictionary

if type == "U": #If a continuous uniform distribution

listC = listB[1].split(",") #Splits listC to show upper and lower bounds

a = str(listC[0]) #Sets a to lower bound

a = float(a) #Converts a to float

b = str(listC[1][:-1]) #Sets b to upper bound

b = float(b) #Converts b to float

DistDict[name] = U(a, b) #Adds distribution to the dictionary

if type == "Po": #If a poisson distribution

l = str(listB[1][0:-1]) #Sets l to average/variance

l = float(l) #Converts l to float

DistDict[name] = Po(l) #Adds distribution to dictionary

elif "Var" or "var" in string: #If asking for a variance

listA = string.split("(") #Splits to find distribution wanted

print(DistDict[listA[1][:-1]].Var()) #Prints the variance of the requested distribution

elif "E" or "e" in string: #If asking for an expected value

listA = string.split("(") #Splits to find distribution wanted

print(DistDict[listA[1][:-1]].E()) #Prints the expected value of the requested distribution