Home > Software design >  Won't let me find average of the dictionary
Won't let me find average of the dictionary

Time:02-12

Ok guys so basically my issue is that I need to have a dictionary created by what the user inputs. I then need to allow them to input a name and time length which I have managed successfully. I also think I got it to tell me the shortest and longest length correctly. However for some reason it wont let me calculate an average. I have spent hours researching it on youtube and trying different things, so my hypothesis is that something is incorrect at the top of my code which isn't allowing the calculation of an average. Please help this project is due soon! Code is below.

n = int(input())
my_dict = {}


for i in range(n):
    titles=input("What is the ")
    length=input("What is length?")
    my_dict[titles] = length
print ("You have", n," on your. The names of your are:", my_dict)


shortest = min(my_dict.values())
longest = max(my_dict.values())

print("Your shortest video is", shortest,"minutes long.")
print("Your longest video is", longest,"minutes long.")


average = sum(my_dict.values())/ len(v)


print("Your average length is", average)

CodePudding user response:

Inputs in Python are stored as strings. Change the commented line and you are good to go. EDIT: If time values also contain floats then you can also change the line length=int(input("What is length?")) to length=float(input("What is length?"))

n = int(input())
my_dict = {}


for i in range(n):
    titles=input("What is the ")
    length=int(input("What is length?")) # You were taking the input of length as a string. 
    my_dict[titles] = length
print ("You have", n," on your. The names of your are:", my_dict)


shortest = min(my_dict.values())
longest = max(my_dict.values())

print("Your shortest video is", shortest,"minutes long.")
print("Your longest video is", longest,"minutes long.")


average = sum(my_dict.values())/ len(my_dict)


print("Your average length is", average)

CodePudding user response:

I have changed the logic to calculate the sum. It is working now.

n = int(input())
my_dict = {}


for i in range(n):
    titles=input("What is the ")
    length=input("What is length?")
    my_dict[titles] = length
print ("You have", n," on your. The names of your are:", my_dict)


shortest = min(my_dict.values())
longest = max(my_dict.values())

print("Your shortest video is", shortest,"minutes long.")
print("Your longest video is", longest,"minutes long.")

sum = 0
for value in my_dict.values():
    sum = sum   int(value)

average = sum/ len(my_dict.values())


print("Your average length is", average)

CodePudding user response:

If you don't wanna change your inputs type
u can change dictionary values to float
like:

n = int(input())
my_dict = {}


for i in range(n):
    titles=input("What is the ")
    length=int(input("What is length?"))  
    my_dict[titles] = length
print ("You have", n," on your. The names of your are:", my_dict)


shortest = min(my_dict.values())
longest = max(my_dict.values())

print("Your shortest video is", shortest," minutes long.")
print("Your longest video is", longest," minutes long.")


for v in my_dict.keys():
    my_dict[v] = float(my_dict[v])

avg = sum(my_dict.values()) / len(my_dict)
print (avg)```
  • Related