-
-
Save mayankdawar/f51adcde0f8c0f85440fc4e88ce7da13 to your computer and use it in GitHub Desktop.
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!") |
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?
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 ?
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)
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.
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)
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%
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!"))
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.