I'm currently learning python and have been given the following task: create a program that will generate random lower-case letters, stopping when the sequence 'Hello' is created. It should then print the total number of characters that had to be created to generate the sequnce. As a part 2 I then need to process this 10 times taking an average of the results. I think I've managed to get the first part functionaing correctly:
import string
import random
for i in range(10):
def task8(word):
word = word.lower()
x = 0
attempts = 0
while x < len(word):
attempts += 1
z = random.choice(string.ascii_lowercase)
if z == word[x]:
x += 1
return attempts
attempts = task8("hello")
print(attempts)
Output: 130 169 56 62 60 62 147 99 31 51
However, at this point I'm struggling to get an average of this list due to a TypeError. I've tried importing statistics and taking the mean from attempts like so:
import string
import random
import statistics
for i in range(10):
def task8(word):
word = word.lower()
x = 0
attempts = 0
while x < len(word):
attempts += 1
z = random.choice(string.ascii_lowercase)
if z == word[x]:
x += 1
return attempts
attempts = task8("hello")
average = statistics.mean(attempts)
print(average)
line 324, in mean if iter(data) is data: TypeError: 'int' object is not iterable
Not sure what I'm missing here but any help would be appreciated.