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 won't 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.
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)
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)