Question

def average_word_length(string): num_words = 0 if not type(string)==str: return "Not a string" if not "A" or...

def average_word_length(string):

num_words = 0

if not type(string)==str:

return "Not a string"

if not "A" or not "B" or not "C" or not "D" or not "E" or not "F" or not "G" or not "H" or not "I" or not "J"\ or not "K" or not "L" or not "M" or not "N" or not "O" or not "P" or not "Q" or not "R" or not "S" or not "T"\ or not "U" or not "V" or not "W" or not "X" or not "Y" or not "Z" or not "a" or not "b" or not "c" or not "d"\ or not "e" or not "f" or not "g" or not "h" or not "i" or not "j" or not "k" or not "l" or not "m" or not "n"\ or not "o" or not "p" or not "q" or not "r" or not "s" or not "t" or not "u" or not "v" or not "w" or not "x"\ or not "y" or not "z" in string :

return "No words"

else:

for i in string:

if i == " " and (i+1) == " ":

num_words += 0

if i == " ": num_words += 1

if i == "," or i == "." or i == "!" or i == "?":

num_words+=0

else:

num_words=1

if i == " ":

numl=len(string)-string.count(char)

if i=="," or i=="." or i=="!" or i=="?":

numl=len(string)-1

else: numl=len(string)

return numl/num_words

print(average_word_length("Hi"))
print(average_word_length("Hi, Lucy"))
print(average_word_length(" What big spaces you have!"))
print(average_word_length(True))
print(average_word_length("?!?!?! ... !"))

When the function works, the following code should output: 2.0, 3.0, 4.0, "Not a string","No words"

Not sure what the mistake is because it output something else

0 0
Add a comment Improve this question Transcribed image text
Answer #1


Given below is the fixed code (tested on python 3.5) for the question.
****INDENTATION GETS LOST WHEN CODE IS UPLOADED. MAKE SURE TO INDENT CODE EXACTLY AS SHOWN IN IMAGE. ****
Please don't forget to rate the answer if it was helpful. Thank you

def average_word_length(string):
if not type(string)==str:
return "Not a string"

num_words = 0
word = ''
total_length = 0

for c in string:
if c in " \t\n,.!?": #check if its any delimiter character
if not word == '':
num_words += 1
total_length += len(word)
word = ''
else:
word += c

#increment for last word
if not word == '':
num_words += 1
total_length += len(word)

if num_words == 0:
return "No words"
else:
return total_length/num_words

print(average_word_length("Hi"))
print(average_word_length("Hi, Lucy"))
print(average_word_length(" What big spaces you have!"))
print(average_word_length(True))
print(average_word_length("?!?!?! ... !"))

Add a comment
Know the answer?
Add Answer to:
def average_word_length(string): num_words = 0 if not type(string)==str: return "Not a string" if not "A" or...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Please explain the python code below L1 = [2, 15, 'Carol', 7.4, 0, -10, -6, 42,...

    Please explain the python code below L1 = [2, 15, 'Carol', 7.4, 0, -10, -6, 42, 27, -1, 2.0, 'hello', [2, 4], 23] print("L1 =",L1) odds =[] evens=[] list=['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z','A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',',','.'] no=0 for i in L1: i=str(i) for j in list: if i.find(j)>=0: no=1 if no==1: None else: i=int(i) if i%2==0: evens.append(i) else: odds.append(i) no=0      ...

  • Page 3 of 7 (Python) For each substring in the input string t that matches the...

    Page 3 of 7 (Python) For each substring in the input string t that matches the regular expression r, the method call re. aub (r, o, t) substitutes the matching substring with the string a. Wwhat is the output? import re print (re.aub (r"l+\d+ " , "$, "be+345jk3726-45+9xyz")) a. "be$jk3726-455xyz" c. "bejkxyz" 9. b. "be$jks-$$xyz" d. $$$" A object a- A (2) 10. (Python) What is the output? # Source code file: A.py class A init (self, the x): self.x-...

  • convert the following code from python to java. def topkFrequent(nums, k): if not nums: return [...

    convert the following code from python to java. def topkFrequent(nums, k): if not nums: return [ ] if len(nums) == 1: return nums [0] # first find freq freq dict d = {} for num in nums: if num in d: d[num] -= 1 # reverse the sign on the freq for the heap's sake else: d[num] = -1 h = [] from heapq import heappush, heappop for key in di heappush(h, (d[key], key)) res = [] count = 0...

  • in python and according to this #Creating class for stack class My_Stack: def __init__(self): self.items = [] def isEmpty(self): return self.items == [] def Push(self, d): self.items.append(d) def Po...

    in python and according to this #Creating class for stack class My_Stack: def __init__(self): self.items = [] def isEmpty(self): return self.items == [] def Push(self, d): self.items.append(d) def Pop(self): return self.items.pop() def Display(self): for i in reversed(self.items): print(i,end="") print() s = My_Stack() #taking input from user str = input('Enter your string for palindrome checking: ') n= len(str) #Pushing half of the string into stack for i in range(int(n/2)): s.Push(str[i]) print("S",end="") s.Display() s.Display() #for the next half checking the upcoming string...

  • COMPLETE THE _CONSTRUCT() AND CONSTRUCTTREE() FUNCTIONS class expressionTree:    class treeNode: def __init__(self, value, lchild, rchild):...

    COMPLETE THE _CONSTRUCT() AND CONSTRUCTTREE() FUNCTIONS class expressionTree:    class treeNode: def __init__(self, value, lchild, rchild): self.value, self.lchild, self.rchild = value, lchild, rchild def __init__(self): self.treeRoot = None    #utility functions for constructTree def mask(self, s): nestLevel = 0 masked = list(s) for i in range(len(s)): if s[i]==")": nestLevel -=1 elif s[i]=="(": nestLevel += 1 if nestLevel>0 and not (s[i]=="(" and nestLevel==1): masked[i]=" " return "".join(masked) def isNumber(self, expr): mys=s.strip() if len(mys)==0 or not isinstance(mys, str): print("type mismatch error: isNumber")...

  • Spell it out! Use the following Java concepts to compile the program below:   String myName =...

    Spell it out! Use the following Java concepts to compile the program below:   String myName = "Chuck";     int length = myName.length();     char firstChar = myName.charAt(0);     char secondChar = myName.charAt(1);     if (myName.equals("Tom")) {       System.out.println ("Sorry, Tom!");   } Write a program that uses a METHOD to translate these individual characters:   input output input output input output input output input output a 4 g 9 m /\\/\\ s $ y ‘/ b B h |-| n |\\| t...

  • this python code below is to find the longest common subsequence from two input string !!!...

    this python code below is to find the longest common subsequence from two input string !!! however it give me the wrong answer, can any one fix it thanks def lcs(s1, s2): matrix = [["" for x in range(len(s2))] for x in range(len(s1))] for i in range(len(s1)): for j in range(len(s2)): if s1[i] == s2[j]: if i == 0 or j == 0: matrix[i][j] = s1[i] else: matrix[i][j] = matrix[i-1][j-1] + s1[i] else: matrix[i][j] = max(matrix[i-1][j], matrix[i][j-1], key=len) cs =...

  • # Problem 3 def printexp(tree): sVal = "" if tree: sVal = '(' + printexp(tree.getLeftChild()) sVal = sV...

    # Problem 3 def printexp(tree): sVal = "" if tree: sVal = '(' + printexp(tree.getLeftChild()) sVal = sVal + str(tree.getRootVal()) sVal = sVal + printexp(tree.getRightChild())+')' return sVal #### Functions and classes to help with Homework 6 #### You should not make any changes to the functions and classes in this file, #### but you should understand what they do and how they work. import sys ### This function will be re-defined in hw6.py. However, it is needed for ExpTree, ###...

  • python function will Return True if string x contains 3 vowels in a row, in consecutive...

    python function will Return True if string x contains 3 vowels in a row, in consecutive locations, false otherwise. assuming that 'vowels' refer to the following lowercase lttrs: a,e,i,o,u programs fails partially, only allowed to use float, str, int, appen, split, strip, len, range def vowels_three(x): for i in range (o, len(x), 2): if x[i] not in ('a,e,i,o,u'): return False return True

  • JAVA CODE String[] str = in.nextLine().split("\\s"); for(int i=0;i<=str.length;i++){ if (str[i].equals(stop)) break;    if(i%2==0){ System.out.print(str[i]+" "); }...

    JAVA CODE String[] str = in.nextLine().split("\\s"); for(int i=0;i<=str.length;i++){ if (str[i].equals(stop)) break;    if(i%2==0){ System.out.print(str[i]+" "); }    } lets say the INPUT for the array is: girl, boy, man, woman, girl, man, boy i dont want the array to print any string twice, i want the output to be like this: print all of them but print each once ---------- OUTPUT: girl, boy, man, woman

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT