Home > Software design >  Need help making my input make a random selection
Need help making my input make a random selection

Time:02-12

I need to to figure out how much the viewer spent watching one of the items in my dictionary. First I need to allow them to input how many viewers, which I got. But then I have to have each viewer them randomly select an item (it is allowed to duplicate). Then show the total time that was spent on ever item that each viewer spent time on. Finally I need to show the total amount of time spent between all the items/vids that all viewers watched, as well as which items were watched by the viewers (cant be duplicated). Sorry thats a lot but I'm very confused on where to begin... code:

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)


sub = 0
while sub >= 0:
    sub = int(input("How many subscribers do you currently have? "))




if sub >= 0:
    print('Histogram:')
    for i in range(sub):
        choice = random.choice(list(my_dict))
        for i in range(choice):
                print('*', end=' ')
    print('\n')

Im working on finding a solution, but IDEK where to start so thinking I might as well post here first in case I can't find the answer. My projects due soon so a little worried... (ALso don't need to demonstrate this first part with a histogram technically, I thought it would just be easy, if you have another idea please let me know!)

CodePudding user response:

From what I have understood, you want to know the no. of viewers then get a random video. Finally, tell the amount of time they have spent watching that video,total time spend on each video(by all viewers), and at last total time spent(by all viewers in watching all videos). Correct me If I am wrong. You can use the following code:

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)



sub = int(input("How many subscribers do you currently have? "))
total_time = 0 
total_time_eachvid = {}


if sub >= 0:
    print('Histogram:')
    for i in range(sub):
        choice = random.choice(list(my_dict))
        if choice in total_time_eachvid:
            total_time_eachvid[choice]  = my_dict[choice] #If the video was already selected by a viewer once then add the time.
        else:
            total_time_eachvid[choice] = my_dict[choice] #If the video appears for first time then enter the length of the video.
        print("You have spent", my_dict[choice], "time.") # Printing the time or length of the video that the viewer selected(got randomly).
        total_time  = my_dict[choice]
    print('\n')
print("Total Time spent by all viewers is:", total_time)
print("Total time spent on each video", total_time_eachvid)
  • Related