我怎样才能让我的输入随机选择
How can I make my input make a random selection
我需要弄清楚观众花了多少钱观看我字典中的一项。首先,我需要让他们输入我得到的观众人数。但是我必须让每个观众随机 select 一个项目(允许重复)。
然后显示每位观看者花费时间在每个项目上的总时间。最后,我需要显示所有观众观看的所有 items/vids 之间花费的总时间,以及
观众观看了项目(不能重复)。我很困惑从哪里开始。
代码:
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')
我正在努力寻找解决方案,但我不知道从哪里开始,所以我想我不妨先 post 这里,以防我找不到答案。 (此外,我不需要从技术上用直方图来演示这第一部分,我认为这很容易,如果您有其他想法,请告诉我!)
据我了解,您想知道号码。然后观众会得到一个随机视频。最后,告诉他们观看该视频所花费的时间,每个视频所花费的总时间(所有观众),以及最后所花费的总时间(所有观众观看所有视频)。如果我错了,请纠正我。您可以使用以下代码:
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)
我需要弄清楚观众花了多少钱观看我字典中的一项。首先,我需要让他们输入我得到的观众人数。但是我必须让每个观众随机 select 一个项目(允许重复)。
然后显示每位观看者花费时间在每个项目上的总时间。最后,我需要显示所有观众观看的所有 items/vids 之间花费的总时间,以及 观众观看了项目(不能重复)。我很困惑从哪里开始。
代码:
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')
我正在努力寻找解决方案,但我不知道从哪里开始,所以我想我不妨先 post 这里,以防我找不到答案。 (此外,我不需要从技术上用直方图来演示这第一部分,我认为这很容易,如果您有其他想法,请告诉我!)
据我了解,您想知道号码。然后观众会得到一个随机视频。最后,告诉他们观看该视频所花费的时间,每个视频所花费的总时间(所有观众),以及最后所花费的总时间(所有观众观看所有视频)。如果我错了,请纠正我。您可以使用以下代码:
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)