Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import matplotlib.pyplot as plot
- from random import randint
- #count items in a list
- def count_items(target, my_list):
- item_count=0
- for i in my_list:
- if i==target:
- item_count+=1
- return item_count
- #count items in a range (passed as [lower, upper])
- def count_in_range(target_range, my_list):
- my_count=0
- for i in my_list:
- if i>=target_range[0] and i<=target_range[1] :
- my_count+=1
- return my_count
- #count vowels in a string
- def count_vowels_in_string(my_string):
- vowels="aeiou"
- my_count=0
- lc_string=my_string.lower()
- for i in vowels:
- my_count=my_count+lc_string.count(i)
- return my_count
- #data to work with
- scores=[]
- for i in range(30):
- scores.append(randint(0,10))
- names=["fred", "bert", "harry", "fred"]
- my_word="supercalifragilisticexpialidociousUMDIDDLEIDDLEIDDLE"
- #produce a bar chart of scores
- print(scores)
- performance=[] #will contain a number for each score
- for i in range(11):
- performance.append(count_items(i, scores))
- plot.bar(range(11), performance, align='center', alpha=0.5)
- plot.xticks(range(11))
- plot.ylabel('Score frequency')
- plot.title('Scores on a quiz')
- plot.show()
- plot.savefig(fname="Quiz Chart.png")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement