Need to complete this function only using return once

python

#1

Hi i need to write this function and have it return the values but i cant stop it from return none after each one , im only aloud to use return once in the code can anyone help.

def  traffic_intensity(count):
    if count in range(0, 5000):
        print("very low")
    elif count in range(5000, 10000):
        print("low")
    elif count in range(10000, 18000):
        print("moderate")
    elif count in range(18000,1000000):
        print("high")
    else:
        return()
level = traffic_intensity(18000)
print(level)  #high  
level = traffic_intensity(9999)
print(level)
level = traffic_intensity(5000)
print(level)
level = traffic_intensity(1000)
print(level)

#2

None is default value returned

In the different conditions (if and elif) set a variable to the desired value, then after the conditions return this variable. Then you have a single return


#3

is there a way in code u could show me where im going wrong? i dont understand why i cant fix it

def  traffic_intensity(count):
    x = ("very low")
    c = ("low")
    v = ("moderate")
    b = ("high")
    if count in range(0, 5000):
        print(x)
    elif count in range(5000, 10000):
        print(c)
    elif count in range(10000, 18000):
        print(v)
    elif count in range(18000,1000000):
        print(b)
    else:
        return()

#4

that solution could work, if you where allowed to use multiple returns:

    if count in range(0, 5000):
        return x

but given you are not to use allow multiple returns, that won’t work

It should be a single variable, which is defined inside if/elif clause. So that the value of the variable depends on which condition clause executed

then at the end of the function, return this variable


#5

ive tried this but now its saying it cant return int ? when im trying to return a string which i dont understand

def  traffic_intensity(count):
    for num in count:
        if num in range(0, 5000):
            print("very low")
        elif num in range(5000, 10000):
            print("low")
        elif num in range(10000, 17999):
            print("moderate")
        elif num in range(18000, 100000):
            print("high")
        else:
            return()


level = traffic_intensity(18000)
print(level)  #high  
level = traffic_intensity(9999)
print(level)
level = traffic_intensity(5000)
print(level)
level = traffic_intensity(1000)
print(level)

#6

finally got it ! thank you for the help

def  traffic_intensity(count):
    answer = ()
    a = (count)
    if a <= 5000:
        answer = "Very low"
    elif a >= 10000:
        answer = "low"
    elif a >= 18000:
        answer = "moderate"
    elif a <= 18000:
        answer = "high"
    return answer

#7

Good you figured it out :slight_smile:

I on purpose didn’t want to give you the answer, you needed to get there yourself. Where my instructions to vague and to abstract?


#8

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.