Python Forum

Full Version: In need help to put this code in order
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
write a python functionn GOS_erlang(chanels,capacity) that computes the grade of service for a give number of (interger) channels and (floating point) capacity using the Erlang B formula. write a second function E_erlang(GOS,chanels) that computes the capacity in erlangs for a give grade of service and number of chanels. this function should use GOS_erlang and perform a binary search for the capacity. (e.g. start with a=0E and b=channels as guesses for the umber of erlangs, compute the average c of a and b. computer the gos for a,b,ad c and then replace either a with c or b with c depending on whether the desired GOS is greater or less than that computed for c. repeat the process and terminate when abs(a-b)<.001.. write a pytho script which uses E-erlag to compute a table for 7,15,22,30,37,45,52,60 channels and a GOS of 0.01 and GOS0 of 0.05
def Gos_Erlang(A,C):
    term=1
    sum=1
    fact=1
    num=1
for k in range (1,C+1):
    fact=fact*k
    num=A
    term=num/fact
    sum +=term
    Gos= term/sum

 def (gos, C):
    a=0
    b=c
    d=(a+b)/2
    gos d= Gos_Erlang(a)
Hello, for starters
def Gos_Erlang(A,C):
This function does not return any value, which is ok, but the problem would be line #17 - gos d (which by the way is not allowed variable name due to whitespace).
And function def (gos, C): needs a name.
(Apr-10-2019, 06:37 AM)j.crater Wrote: [ -> ]Hello, for starters
def Gos_Erlang(A,C):
This function does not return any value, which is ok, but the problem would be line #17 - gos d (which by the way is not allowed variable name due to whitespace).
And function def (gos, C): needs a name.

I am having issues with the last define function, its telling me that the return is undefined

import math

def fact(n):
    ans = 1
    for i in range(2 , n + 1):
        ans*=i
    return ans
    
def Gos_erlang(A0,n):
    numera = math.pow(A0 , n) / fact(n)
    denomi = 0
    for k in range(0, n+1):
        denomi += math.pow( A0 , k ) / fact(k)
    Gos = numera / denomi
    return Gos
Gos = float(input('Enter Gos : '))
n = int(input('Enter n : '))

def E_erlang(Gos, A0):
    a=0
    b=n
    c=0
    while abs(a-b)>0.000001:
        c=(a+b)/2
        Gos_c=Gos_erlang(c,n)
        if Gos_c<Gos:
            a=c
        else:
            b=c
    return Gos_c
print('Capacity :', c)
def E_erlang(Gos, A0):
    a=0
    b=n
    c=0
    while abs(a-b)>0.000001:
        # unrelated code removed
    return Gos_c
You're returning something which isn't always defined.
The last function should read

def E_erlang(Gos, A0):
    a = 0
    b = n
    c = 0
    Gos_c = Gos_erlang(c, n)
    while abs(a - b) > 0.000001:
        c = (a + b) / 2
        if Gos_c < Gos:
            a = c
        else:
            b = c
    return Gos_c
Also when you do this:
print('Capacity :', c)
c isn't defined anywhere.