1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
jek_recluse [69]
3 years ago
7

[Files, for loops, exceptions; 20pt] Using a for loop, write a function called countWord(). It takes 2 parameters: The name of a

text file (e.g. gettysburg.txt that is included with this test) and a word to be searched within that file. Your code should return the number of times the given word appears in the file. Capitalization should not matter. Make certain to handle file exceptions gracefully.
Computers and Technology
1 answer:
Sergeu [11.5K]3 years ago
6 0

def countWord(name, word):

   try:

       f = open(name, "r")

       lst = ([])

       w = ""

       for x in f.readlines():

           w += x.lower()

       lst = w.split()

       f.close()

       return lst.count(word)

   except FileNotFoundError:

       print("Please create a file or use the name of an existing text file.")

print("Your word appears", countWord("gettysburg.txt", "random"), "time(s)")

The text file I used for testing looks like:

random words

random words

I'm putting random words in here

random

this is random

RaNdOm

I didn't really know what exceptions your professor is looking for so I just used the file not found one. Best of luck.

You might be interested in
Impaired drivers not only harm themselves but they harm other individuals and affect our _________________.
lawyer [7]

entire society. impaired drivers affect all of those things.

4 0
4 years ago
Read 2 more answers
A bit pattern is shown below.<br> 01001110<br> Convert the bit pattern into decimal
Vera_Pavlovna [14]

Answer:

78

Explanation:

I always convert to hexadecimal first:

0100 = 4

1110 = E

(the matching of groups of 4 bits to one hex char is a lookup you can memorize)

so we have 4E, which is 4*16+14 = 78

Of course you can also go the long route:

01001110 = 0·2⁷+1·2⁶+0·2⁵+0·2⁴+1·2³+1·2²+1·2¹+0·2⁰ = 78

5 0
3 years ago
Which describes a design theme in PowerPoint? a set of elements that unify the slides and complement the topic a printed handout
chubhunter [2.5K]

a set of elements that unify the slides and complement the topic

5 0
4 years ago
Read 2 more answers
What is the Content Tab for?
anygoal [31]

Content Tabs enable Thunderbird to display remote content in a tab, which users can browse in (mostly) the same way as with a browser. For example, invoking Thunderbird's Help. What's New menu option opens a tab that displays web content.

5 0
4 years ago
Read 2 more answers
For the purposes of laying out content, the total height of a box is calculated by adding which of the following?
jenyasd209 [6]
<span>When laying out content, the box is calculated by setting the width and height of the element using CSS to the size of the content area, the full size of an element can be calculated by also adding in borders, padding and margins.</span>
6 0
3 years ago
Read 2 more answers
Other questions:
  • What type of device is the printer?
    13·2 answers
  • a _____ is a recreation of a thumbnail in a desktop publishing software, also known as a "rough". a. proof b. digital draft c. l
    15·1 answer
  • A ____________ is a set of commands which can be run by calling it by name. (Phyton)
    7·2 answers
  • A(n) _____ is a type of computer crime perpetrator whose primary motive is to achieve financial gain.
    5·1 answer
  • All resources are limited. Which of the following is NOT a resource?
    11·1 answer
  • We are committed to creating and cultivating a graduate and postdoctoral community that values and respects individuals, regardl
    11·1 answer
  • How do you check how much space is left in current drive?
    9·1 answer
  • Write a program that reads the subtotal and the gratuity rate, then computes the gratuity and total. For example, if the user en
    6·1 answer
  • The gradual wearing away or breaking down of rocks by abrasion is a type of __________________ weathering.
    8·1 answer
  • Complete the firstMiddleLast() function to return a new string with the first, middle, and last character from string. If string
    9·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!