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      ...

  • 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...

  • 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...

  • 12.22 Chapter 4: Encrypt Characters Simple Caesar Cipher challenge. You'll need to correct code to print...

    12.22 Chapter 4: Encrypt Characters Simple Caesar Cipher challenge. You'll need to correct code to print ciphertext character correctly. With offset of 3 the output should be ORIGINAL CHARACTERS 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 ENCRYPTED CHARACTERS D E F G H I J K L M N O P Q R S T U V W X Y Z...

  • Create a graph in python for a given input series without using any libraries

    I was asked this question:Given any input series a corresponding graph must be generated without the use of any libraries.After, trying my best, I arrived at this solution to which they replied it had a logical issue.# Create the matrix print("Enter the sequence with spaces: ") arr = list(map(int, input().split())) count = len(arr) rows = int(sum(arr))  cols = int(sum(arr) + 4) content = [[" "]*cols for _ in range(rows)] maxq = 0 maxp = 0 content[0][0] = "/" # Apply the positions in the matrix p = 0 q = arr[0] k = 0 for l in range(q):     if (k != q):         content[k][p] = "/"         p = p + 1         k = k + 1 p = q flag = 1 i = 0 j = 0 k = 0 c = 0 temp = 0 r = 0 flag = 1 for i,j in enumerate(arr):     c = c + 1     if c < count:         k = arr[i+1]     else:         k = 0     if arr[i]:         if flag == 1:             content[q][p] = "/\\"             if maxq < q:                 maxq = q                 maxp = p             qori = q             pori = p             p = p + k             temp = q - k...

  • having trouble with the following swift 5 problem. It is just printing the number 4 twice...

    having trouble with the following swift 5 problem. It is just printing the number 4 twice and it seems to be ignoring camelCasePhraseTwo, please help if you can: /* Problem 4 Write a function that takes in a camel-case string and returns an integer representing the number of words in the string Notes: - Assume all input will contain only letters, and the string will be a valid camel-cased phrase - Do not loop through the array manually. */ func...

  • 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-...

  • Need help in c++ programming to output the lines in my code: if word if found:...

    Need help in c++ programming to output the lines in my code: if word if found:            cout << "'" << word << "' was found in the grid" << endl;            cout << "'" << word << "' was not found in the grid" << endl; The words can be touching if they are horizontally, vertically, or diagonally adjacent. For example, the board: Q W E R T A S D F G Z X C V B Y U A...

  • 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

  • This is a Python Program Write a program that encrypts letters based on the following key....

    This is a Python Program Write a program that encrypts letters based on the following key. Original letter to encrypted letter A M B L C K D J E I F H G G H F I E J D K C L B M A N Z O Y P X Q W R V S U T T U S V R W Q X P Y O Z N Write the output to a file

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