user6111799
user6111799

Reputation: 1

Translating a list of numbers to a graphic format (#)

So i am trying to get it to look like this in python 3:

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: # # # # # # #

And I have written this so far:

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()

But whenever I run it, it has an error and i honestly am stuck at this point as to what to do. If someone could help me with this, I would appreciate it so much! Thanks for anyone that will be able to help me with this!

Upvotes: 0

Views: 53

Answers (1)

poke
poke

Reputation: 388143

def car_count_graphic_format(graphic):
    for x in range(graphic):
        graphic = "#" * graphic
        print("Hour #:" ,graphic,)

You should be careful here, you are reusing the name graphic far too often—for different confusing things.

The function takes a list of car counts for each hour, so let’s call that argument car_counts. We want to print a line for each entry, so we have to iterate over the elements in car_counts. Note that we cannot use range(car_counts) here since car_counts is a list but range() expects a number. Instead, we can just directly iterate over the list to get each element in it in each iteration. Since we also want to take note of the hour, we use enumerate() to also get the index of the element in the list. Since the hours start at 1, we tell enumerate() to start counting at 1:

for hour, count in enumerate(car_counts, start=1):
    graphic = '# ' * count
    print('Hour #{}: {}'.format(hour, graphic))

Finally, we also want to find out the average, so we count the total number of cars, and then later divide by the number of hours (the length of the list) to get the average car count:

def car_count_graphic_format(car_counts):
    total_cars = 0
    for hour, count in enumerate(car_counts, start=1):
        total_cars += count
        graphic = '# ' * count
        print('Hour #{}: {}'.format(hour, graphic))

    average = round(total_cars / len(car_counts))
    graphic = '# ' * average
    print('Average: {}'.format(graphic))

Finally note that you do not want to sort your hourlist in the number_cars function, or you will lose the information which car count belongs to which hour.

Upvotes: 1

Related Questions