Last active
September 19, 2022 12:46
-
-
Save cibofdevs/95f7f1751cc28538585cd96db8554f2f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# week_temps_f is a string with a list of fahrenheit temperatures separated by the , sign. | |
# Write code that uses the accumulation pattern to compute the average (sum divided by number of items) and assigns it to avg_temp. | |
# Do not hard code your answer (i.e., make your code compute both the sum or the number of items in week_temps_f) | |
# (You should use the .split(",") function to split by "," and float() to cast to a float). | |
week_temps_f = "75.1,77.7,83.2,82.5,81.0,79.5,85.7" | |
avg_temp = 0.0 | |
for i in week_temps_f: | |
avg_temp = sum(map(float,week_temps_f.split(","))) / 7 | |
print(avg_temp) |
easy step to solve this problem
week_temps_f = "75.1,77.7,83.2,82.5,81.0,79.5,85.7"
sp = week_temps_f.split(',')
sumof = 0.0
for i in sp:
sumof = sumof + float(i)
item = len(sp)
avg_temp = sumof/item
week_temps_f = "75.1,77.7,83.2,82.5,81.0,79.5,85.7"
sun = week_temps_f.split(",")
accum = 0
for i in sun:
accum += float(i)
avg_temp = accum / 7
print(avg_temp)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
yes, we can also do like this
week_temps_f = "75.1,77.7,83.2,82.5,81.0,79.5,85.7"
t=week_temps_f.split(",")
sum1=0
for i in t:
sum1=sum1+float(i)
avg_temp=sum1/len(t)
print(avg_temp)