Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- '''
- quiz scores
- '''
- 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(1,10))
- names=["fred", "bert", "harry", "fred"]
- my_word="supercalifragilisticexpialidociousUMDIDDLEIDDLEIDDLE"
- #print top scorers and scorers in a range 7-9
- print("Scores", scores)
- print("{0} learners got top marks".format(count_items(10,scores)))
- print("{0} learners got between 7 and 9 (inc)".format(count_in_range([7,9],scores)))
- #works for lists of strings too
- print("Names", names)
- print("{0} freds".format(count_items("fred",names)))
- #count vowels in a string
- print("Word", my_word)
- print("{0} vowels in word".format(count_vowels_in_string(my_word)))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement