所以我想让它在Python3中看起来像这样:
How many hours are in your sample? 7
Enter the count of cars for hour #1: 2
Enter the count of cars for hour #2: 5
Enter the count of cars for hour #3: 9
Enter the count of cars for hour #4: 11
Enter the count of cars for hour #5: 12
Enter the count of cars for hour #6: 6
Enter the count of cars for hour #7: 5
Hour #1: # #
Hour #2: # # # # #
Hour #3: # # # # # # # # #
Hour #4: # # # # # # # # # # #
Hour #5: # # # # # # # # # # # #
Hour #6: # # # # # #
Hour #7: # # # # #
Average: # # # # # # #
到目前为止,我写了这样一篇文章:
def main():
hourlist = number_cars()
graphic = hourlist
car_count_graphic_format(graphic)
def number_cars():
hours = input("How many hours are in your sample? ")
hours = int(hours)
hourlist = []
for i in range(hours):
hourcount = input("Enter the count of cars for hour #" +str(i+1)+ ": ")
hourcount = int(hourcount)
hourlist.append(hourcount)
hourlist.sort()
return hourlist
def car_average(car_count):
car_sum = 0
for position in range(len(car_count)):
car_sum = car_sum + car_count[position]
average = car_sum / len(car_count)
return average
def car_count_graphic_format(graphic):
for x in range(graphic):
graphic = "#" * graphic
print("Hour #:" ,graphic,)
main()
但每当我运行它时,它都会出错,我真的被困在这一点上,不知道该怎么办。如果有人能帮我,我会非常感激的!谢谢任何能帮我的人!你知道吗
在这里您应该小心,您太频繁地重复使用名称
graphic
来处理各种令人困惑的事情。你知道吗该函数获取每小时的汽车计数列表,因此我们调用该参数
car_counts
。我们希望为每个条目打印一行,因此必须遍历car_counts
中的元素。注意,我们不能在这里使用range(car_counts)
,因为car_counts
是一个列表,但range()
需要一个数字。相反,我们可以直接迭代列表,在每次迭代中获得列表中的每个元素。因为我们还想记下小时,所以我们使用enumerate()
来获取列表中元素的索引。因为小时从1开始,我们告诉enumerate()
从1开始计数:最后,我们还想找出平均数,所以我们计算出汽车总数,然后除以小时数(列表长度)得到平均汽车数:
最后请注意,您不想在
number_cars
函数中对您的hourlist
排序,否则您将丢失哪个车次属于哪个小时的信息。你知道吗相关问题 更多 >
编程相关推荐