Can anyone tell me why my list is only returning the first if statement and not the rest


#1

Can anyone tell me why my list is only returning the first if statement and not the rest

def smog_risk(intensity, wind_rating):
    answer = int()
    calm = [5, 3, 2, 1]
    wind = [3, 2, 1, 1]
    
    if intensity == ('High'):
        if wind_rating == ('Calm'):
            answer = calm[0]
    elif intensity == ('Moderate'):
        if wind_rating == ('Calm'):
            answer = calm[1]            
    elif intensity == ('Low'):
        if wind_rating == ('Calm'):
            answer = calm[2]            
    elif intensity == ('Very_Low'):
        if wind_rating == ('Calm'):
            answer = calm[3] #end calm     
    elif intensity == ('High'):
        if wind_rating == ('Windy'):
            answer = wind[0]
    elif intensity == ('Moderate'):
        if wind_rating == ('Windy'):
            answer = wind[1]            
    elif intensity == ('Low'):
        if wind_rating == ('Windy'):
            answer = wind[2]            
    elif intensity == ('Very_Low'):
        if wind_rating == ('Windy'):
            answer = wind[3]
            
    return answer

risk = smog_risk('High', 'Calm')
print(risk)
risk = smog_risk('Low', 'Windy')
print(risk)
risk = smog_risk('Moderate', 'Windy')
print(risk)
risk = smog_risk('Very_Low', 'Windy')
print(risk)

#2

lets do this one:

risk = smog_risk('Low', 'Windy')

this means this elif condition is true:

    elif intensity == ('Low'):
        if wind_rating == ('Calm'):
            answer = calm[2]  

I included some more code so you can see which elif i am referring to. Which means the remaining elif conditions aren’t checked or executed.

elif (else if) is literally what is says:

if this condition is true, do something. else if do something else (repeat for each elif statement you have), and finally do something else when none of the if or elif conditions where true.

if that doesn’t help, use visualizer:

http://www.pythontutor.com/visualize.html#mode=edit

paste the code in, and press: visualize execution. Then you can see all the steps your program takes


#3

Okay so i’ve fixed it thank you but its saying that i’ve used 20 statements and im aloud to use a max of 12 anyway i can shorten this to 12?

def smog_risk(intensity, wind_rating):
    '''returns number assisgned to the two values'''
    answer = int()
    calm = [5, 3, 2, 1]
    wind = [3, 2, 1, 1]
    
    if (intensity, wind_rating) == ('High', 'Calm'):
        answer = calm[0]
    elif (intensity, wind_rating) == ('Moderate', 'Calm'):
        answer = calm[1]            
    elif (intensity, wind_rating) == ('Low', 'Calm'):
        answer = calm[2]            
    elif (intensity, wind_rating) == ('Very Low', 'Calm'):
        answer = calm[3] #end calm     
    elif (intensity, wind_rating) == ('High', 'Windy'):
        answer = wind[0]
    elif (intensity, wind_rating) == ('Moderate', 'Windy'):
        answer = wind[1]            
    elif (intensity, wind_rating) == ('Low', 'Windy'):
        answer = wind[2]            
    elif (intensity, wind_rating) == ('Very Low', 'Windy'):
        answer = wind[3]        
    return answer

#4

what does it count as statement? And how does it get to 20?

i don’t know if you can, i can. I see the solution they are aiming for

I am in a bit of a sarcastic mood

In how far is help with these assignments allowed? I don’t want to be responsible for something i was not allowed to do


#5

im not sure how it counts the statement its for a uni assignment , im guessing everytime i assign something with = or == thats a statement ? i’m not exactly sure , i dont want the answer because i love getting the uh ha finally moment when i get it but any hints towards what im suppose to do would be good.


#6

i would normally ask a tutor for help towards the answer but its due this sunday


#7

You could write if and elif statement to check for the various intensities.

then inside the clauses of those statements, nest an if and else which checks for wind_rating

this is precisely 12, which is why i think its the solution aimed for.


#8

could u write an example of what u mean i can get my head around it

def smog_risk(intensity, wind_rating):
    '''returns number assisgned to the two values'''
    answer = int()
    calm = [5, 3, 2, 1]
    wind = [3, 2, 1, 1]
    
    if (intensity) == ('High'):
        if wind_rating == ('Calm'):
            answer = (calm[0])
        else:
            answer = (wind[0])
    elif (intensity) == ('Moderate'):
        if wind_rating == ('Calm'):
            answer = calm[1]
        else:
            answer = (wind[1])
    elif (intensity) == ('Low'):
        if wind_rating == ('Calm'):
            answer = calm[2]
        else:
            answer = (wind[2])
    elif (intensity) == ('Very Low'):
        if wind_rating == ('Calm'):
            answer = calm[3]
        else:
            answer = (wind[3])   
    return answer

#9

This looks right, apart from the massive amount of redundant parentheses.

you can remove this line:

    answer = int()

as well as all the parentheses in this code:

    if (intensity) == ('High'):
        if wind_rating == ('Calm'):
            answer = (calm[0])
        else:
            answer = (wind[0])
    elif (intensity) == ('Moderate'):
        if wind_rating == ('Calm'):
            answer = calm[1]
        else:
            answer = (wind[1])
    elif (intensity) == ('Low'):
        if wind_rating == ('Calm'):
            answer = calm[2]
        else:
            answer = (wind[2])
    elif (intensity) == ('Very Low'):
        if wind_rating == ('Calm'):
            answer = calm[3]
        else:
            answer = (wind[3])   
    return answer

#10

doing all that still left me with 19 statements i need to eliminate some of the = or ==


#11

you need to find out what it counts as statement, otherwise its difficult to tell what to reduce.

I can come up with a far more effective solution, but that is not how its suppose to be


#12

this is all i can tell u about the questions i’m asked

Write a function smog_risk(intensity, wind_rating) that takes an intensity and a wind rating and returns a smog risk rating as an integer.

The intensity will be a string and the only possible values are ‘High’, ‘Moderate’, ‘Low’ and ‘Very Low’, ie, outputs from the traffic_intensity function.

The wind_rating will be a string and the only possible values are ‘Calm’ and ‘Windy’.

The table below outlines the level of smog risk for the various combinations.

Intensity Level Calm Windy
High 5 3
Moderate 3 2
Low 3 1
Very Low 1 1
The smog risk rating is on a scale of 1 to 5, with 5 being the highest risk and 1 being the lowest risk.

Note:

Your function should only use one return. Be sure to use the pre-check before you do the full check.
Your function must have 12 or less statements. The pre-check will let you know if you have too many.
You can assume we will only test your function with valid intensity levels and wind ratings.


#13

its counting ever = and == as a statement


#14

i could think of ways to make this code a lot more effective. But i shouldn’t be the architect of the solution all the time, then you never learn it

How would you go about this?

So far we stayed with the code you posted in the beginning of this topic. Do you still think that is a good idea?


#15

I was bored, i solved it using only a single equal sign within the entire function. So there are certainly ways to improve your code

but i am struggling how i can push you in the right direction without spoiling too much


#16

I’ve litterally been trying to figure this out for 8 hours today , it’s doing my head in haha , I’ve guessed the way I’m doing it is never going to work but with what I’ve been taught so far I can’t figure out how everyone else is getting this question I’ve tried for Bla Bla in Bla Bla but I end up with the same result . Am I ment to be using a list function ? Any clue as to how on earth u did it with only one = would be appreciated :joy: I wish I had your brains


#17

you could use a list, make a list with the wind_ratings, then use .index() to get the index of the wind_rating, this index can then be used to to get integer value from calm or wind list (you need if and elif for this

The solution i created uses a dictionary, which is also handy for this problem

there isn’t that much special about my brain, i have just been programming more and longer


#18

im not sure on what u mean there

so create a list

wind = ['High', 'Moderate', 'Low', 'Very Low']

then how am i ment to use .index() and how am i ment to get a int value from calm or windy and use that in if and elif to get my final result


#19

we can do:

wind.index()

this will return an integer, this integer can be used as index to retrieve from calm or wind depending on wind_rating


#20

I didn’t do it with one = sign but i finally got it !!! 2 days later :joy:

def smog_risk(intensity, wind_rating):
    '''returns smog risk from inputted data'''
    list1 = ("High", 5, 3), ("Moderate", 3, 2), ("Low", 3, 1), ("Very Low", 1, 1)
    answer = str()
    for results in list1:
        if results[0] == (intensity):
            if wind_rating == "Calm":
                answer = str(results[1])
            else:
                answer = str(results[2])
    return str(answer)

now please share how on earth u did it