Skip to content

Instantly share code, notes, and snippets.

@cibofdevs
Last active September 19, 2022 12:46
Show Gist options
  • Save cibofdevs/95f7f1751cc28538585cd96db8554f2f to your computer and use it in GitHub Desktop.
Save cibofdevs/95f7f1751cc28538585cd96db8554f2f to your computer and use it in GitHub Desktop.
# 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)
@kalahan2001
Copy link

why don't we use /len(week_temps_f).
why we are doing hard coding /7.

@arora9889
Copy link

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)

@shahabEsaqib
Copy link

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

@Alexpeain
Copy link

Alexpeain commented Feb 19, 2021

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