Skip to content

Instantly share code, notes, and snippets.

@mayankdawar
Created February 19, 2020 17:31
Show Gist options
  • Save mayankdawar/f51adcde0f8c0f85440fc4e88ce7da13 to your computer and use it in GitHub Desktop.
Save mayankdawar/f51adcde0f8c0f85440fc4e88ce7da13 to your computer and use it in GitHub Desktop.
Create an empty list called resps. Using the list percent_rain, for each percent, if it is above 90, add the string ‘Bring an umbrella.’ to resps, otherwise if it is above 80, add the string ‘Good for the flowers?’ to resps, otherwise if it is above 50, add the string ‘Watch out for clouds!’ to resps, otherwise, add the string ‘Nice day!’ to res…
percent_rain = [94.3, 45, 100, 78, 16, 5.3, 79, 86]
resps = []
for i in percent_rain:
if i > 90:
resps.append("Bring an umbrella.")
elif i >80:
resps.append("Good for the flowers?")
elif i > 50:
resps.append("Watch out for clouds!")
else:
resps.append("Nice day!")
@Nurye-Nigus
Copy link

percent_rain = [94.3, 45, 100, 78, 16, 5.3, 79, 86]

resps=[]
for i in percent_rain:
if(i>90):
resps='Bring an umbrella.'

elif(i>80):
    resps='Good for the flowers?'
    
elif(i>50):
    resps='Watch out for clouds!'
    
else:
    resps='Nice day!'

what is the problem of this code

@mayankdawar
Copy link
Author

percent_rain = [94.3, 45, 100, 78, 16, 5.3, 79, 86]

resps=[]
for i in percent_rain:
if(i>90):
resps='Bring an umbrella.'

elif(i>80):
    resps='Good for the flowers?'
    
elif(i>50):
    resps='Watch out for clouds!'
    
else:
    resps='Nice day!'

what is the problem of this code

well in append function the string gets added on the end of the list and in this code, u are assigning the string value to the resp and changing its data type of resp from the list to string I have added some screenshots hope u understand the concept. If u still don't get this then your doubts would be highly appreciated.
image
image

@abdullzz
Copy link

resps = ["Bring an umbrella" if x>90 else "Good for the flowers?" if x>80 else "Watch out for clouds!" if x>50 else "Nice day!" for x in percent_rain] why is this not working?

@rifat01670
Copy link

rifat01670 commented May 25, 2021

percent_rain = [94.3, 45, 100, 78, 16, 5.3, 79, 86]
resps=[]
for i in percent_rain:
if(i>90):
resps=resps+["Bring an umbrella"]

elif(i>80):
     resps=resps+["Good for the flowers"]
  
elif(i>50):
     resps=resps+["Watch out for clouds"]
  
 else:
    resps=resps+["Nice day!"]

what is wrong in this ?

@prawinreddy55
Copy link

percent_rain = [94.3, 45, 100, 78, 16, 5.3, 79, 86]
resps = []
for i in percent_rain:
if i > 90:
resps += ['Bring an umbrella.']
elif i > 80:
resps += ['Good for the flowers?']
elif i > 50:
resps += ['Watch out for clouds!']
else:
resps += ['Nice day!']
print(resps)

@princemaxp
Copy link

percent_rain = [94.3, 45, 100, 78, 16, 5.3, 79, 86]
resps = []
for i in percent_rain:
if i > 90:
resps += ['Bring an umbrella.']
elif i > 80:
resps += ['Good for the flowers?']
elif i > 50:
resps += ['Watch out for clouds!']
else:
resps += ['Nice day!']
print(resps)

this one works. but make sure that your output is same as the expected output. I forgot one fullstop and it show failed.

@asad-debug
Copy link

percent_rain = [94.3, 45, 100, 78, 16, 5.3, 79, 86]
resps = []
for i in percent_rain:
if i > 90:
resps += ['Bring an umbrella.']

elif i > 80:
    resps += ['Good for the flowers?']
    
elif i > 50:
    resps += ['Watch out for clouds!']
    
else:
    resps +=['Nice day!']
    
    print(resps)

@asad-debug
Copy link

percent_rain = [94.3, 45, 100, 78, 16, 5.3, 79, 86]
resps = []
for i in percent_rain:
if i > 90:
resps += ['Bring an umbrella.']

elif i > 80:
    resps += ['Good for the flowers?']
    
elif i > 50:
    resps += ['Watch out for clouds!']
    
else:
    resps +=['Nice day!']
    
    print(resps)

This works 100%

@misspage
Copy link

I tried this but it did not work; not sure what is wrong

percent_rain = [94.3, 45, 100, 78, 16, 5.3, 79, 86]

resp =[]

if percent_rain < 90:
resp = ("bring an umbrella")
output (resp)
elif percent_rain > 80:
output (resp + ("Good for the flowers?"))
elif percent_rain<50:
output (resp +("Wacth out for clouds"))
else :
output (resp +("Nice day!"))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment