***PYTHON CODING QUESTION*** So this is the question: This is my code: def vowel_consonant_ratio(sentence): """counts th

Business, Finance, Economics, Accounting, Operations Management, Computer Science, Electrical Engineering, Mechanical Engineering, Civil Engineering, Chemical Engineering, Algebra, Precalculus, Statistics and Probabilty, Advanced Math, Physics, Chemistry, Biology, Nursing, Psychology, Certifications, Tests, Prep, and more.
Post Reply
answerhappygod
Site Admin
Posts: 899604
Joined: Mon Aug 02, 2021 8:13 am

***PYTHON CODING QUESTION*** So this is the question: This is my code: def vowel_consonant_ratio(sentence): """counts th

Post by answerhappygod »

***PYTHON CODING QUESTION***
So this is the question:
Python Coding Question So This Is The Question This Is My Code Def Vowel Consonant Ratio Sentence Counts Th 1
Python Coding Question So This Is The Question This Is My Code Def Vowel Consonant Ratio Sentence Counts Th 1 (29.99 KiB) Viewed 55 times
This is my code:
def vowel_consonant_ratio(sentence):
"""counts the number of vowels and constantants, returns
ratio"""
num_vow = 0
num_con = 0
for i in sentence:
if(i=='A' or i=='E' or i=='I' or i=='O' or i=='U' or i=='a' or
i=='e' or i=='i' or i=='o' or i=='u'):
num_vow = num_vow + 1
elif((i >='A' and i <='Z' ) or (i >='a' and i
<='z')):
num_con = num_con + 1
print("Number of vowels:", num_vow)
print("Number of consonants:", num_con)
if(num_con==0):
return 0.0;
else:
t=float(num_vow/num_con)
return round(t,4)
And these are the errors I got:
Python Coding Question So This Is The Question This Is My Code Def Vowel Consonant Ratio Sentence Counts Th 2
Python Coding Question So This Is The Question This Is My Code Def Vowel Consonant Ratio Sentence Counts Th 2 (20.79 KiB) Viewed 55 times
Could someone offer me a new way of doing this question, or help
me debug?
Write a function vowel_consonant_ratio(sentence) that takes an input parameter sentence (type string) and: • prints the number of vowels in the sentence, • prints the number of consonants in the sentence, and • returns the ratio of vowels to consonants as a float, rounded to 4 decimal places. The formatting for the outputs can be seen in the test cases. Cases where your function cannot perform the ratio calculation should return 0.0 NB: this one's not really a part of the typical NLP processing pipeline, but it's a fun exercise For example: Test Result sentence = "vowels and consonants" Number of vowels: 6 print(vowel_consonant_ratio(sentence)) Number of consonants: 13 0.4615 sentence = "what about this?" Number of vowels: 5 print(vowel_consonant_ratio(sentence)) Number of consonants: 8 0.625 sentence = "hooo0000000!" Number of vowels: 10 print(vowel_consonant_ratio(sentence)) Number of consonants: 1 10.0
Precheck only Precheck failed with the following unexpected output. ************* Module source source.py:14:8: W0381: Unnecessary semicolon (unnecessary-semicolon) source.py:7:11: R0916: Too many boolean expressions in if statement (10/5) (too-many-boolean-expressions source.py:7:11: R1714: Consider merging these comparisons with "in" to "i in ('A', 'E', 'I', 'o', 'u', source.py:9:14: R1716: Simplify chained comparison between the operands (chained-comparison) source.py: 9:48: R1716: Simplify chained comparison between the operands (chained-comparison) source.py: 13:4: R1705: Unnecessary "else" after "return" (no-else-return) ***Error*** Sorry, but your code doesn't pass the pylint style checks.
Join a community of subject matter experts. Register for FREE to view solutions, replies, and use search function. Request answer by replying!
Post Reply