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

Answers

Answer 1

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.


Related Questions

JAVA
Write a program that requests the user input a word, then prints every other letter of the word starting with the first letter.
Hint - you will need to use the substring method inside a loop to get at every other character in the String. You can use the length method on the String
to work out when this loop should end.
Sample run:
Input a word:
domain
dmi

Answers

import java.util.Scanner;

public class JavaApplication42 {

   public static void main(String[] args) {

       Scanner scan = new Scanner(System.in);

       System.out.println("Input a word:");

       String word = scan.nextLine();

       for (int i = 0; i < word.length(); i+=2){

           System.out.print(word.substring(i,i+1));

           

       }

   }

   

}

I hope this helps!

The program is an illustration of loops.

Loops are used to carry out repetition operations; examples are the for-loop and the while-loop.

The program in Java, where comments are used to explain each line is as follows:

import java.util.*;

public class Main {

public static void main(String args[]) {

//This creates a Scanner object

Scanner input = new Scanner(System.in);

//This prompts user for input

System.out.print("Input a word: ");

//This gets input from the user

String userinput = input.nextLine();

//The following iterates through every other character of userinput  

      for(int i =0;i<userinput.length();i+=2) {

//This prints every other characters

          System.out.print(userinput.charAt(i));

}

  }

}

The above program is implemented using a for loop

Read more about similar programs at:

https://brainly.com/question/19104397

Using the rule of thirds places items of interest in a photograph along one of the imaginary gridlines or at the point of their intersection.

True
False

Answers

I think the answer is it’s true

Answer: tureee

Explanation::)

Analyze the map below and answer the question that follows. A topographic map of Northwestern Russia. Areas on the map are circled and labeled 1, 2, 3, and 4. 1 is a mountain range. 2 is a flat area. 3 is a highland area. 4 is a plateau. Image by Natural Earth The region labeled with the number 1 on the map above is the __________.

Answers

Answer:region 1

Explanation:

Answer:

region 1

Explanation:

correct on edg 2020

Match the type of multimedia artist to the description of the jobs they perform.

storyboard artist

background artist

special effects animator

multimedia designers

works with other animators to set

the scenes depicting the key

characters

arrowRight

works with various elements to

create content for web pages

arrowRight

creates key frames that depict

all the important scenes in the

story

arrowRight

provides visual enhancements

to make the action depicted

more interesting and appealing

Answers

Answer:

d or a

Explanation:

hope it helps

guys can u help me write a program need help asp will give 25 points and brainlist if correct

Answers

import java.util.Scanner;

public class Lab04b_Numbers {

   

   public static void main(String[] args) {

       Scanner keyboard = new Scanner(System.in);

       System.out.print("Enter your GPA: ");

       double gpa = keyboard.nextDouble();

       System.out.print("Enter cost of a new car: ");

       double cost = keyboard.nextDouble();

       System.out.print("Enter average amount of rain this month: ");

       double rain = keyboard.nextDouble();

       System.out.print("What is your average for history class: ");

       double avg = keyboard.nextDouble();

       System.out.println("In 2020 a car cost $"+cost+".");

       System.out.println("Wow "+gpa+" GPA is good, but "+rain+" inches is bad.");

       System.out.println("Now only "+avg+" percent of people like tacos.");

       System.out.println("Sorry for the "+gpa+" percent who don't.");

       System.out.println(rain+", "+gpa+" or "+avg+" which do you want for a grade?");

   }

   

}

I think this is what you're looking for. Best of luck.

I need the answer to life. I cant live without my POGCHAMPS!

Answers

Answer:

Pray to God and ask him for help in life. May God bless you

Explanation:

Answer:

Pog

Explanation:

Poggers

In an inequality between two numbers, -2.1 is located below the other number on a vertical number line. So, the other number is -2.1. One possible value of the other number is .

Answers

Answer:

-2.0

Explanation:

Given

Number = -2.1

Required

Determine the other number

From the question, we understand that the number is represented on a vertical number.

From bottom to top, numbers on a vertical number line increases.

Since the other number is at the top of -2.1, then we can conclude that this number is greater than-2.1

Hence, possible values are ,-2.0, -1.9, etc.

Answer:

First blank: greater than.

Second bank: -1.5

Explanation:

What would be the results if the Formula =2*C8+3*D9 is entered into a cell if the value displayed in C8 is 3,and the value in cell D9 is 2? Select one: a. 3 b. 18 c. 12 d. 25

Answers

Answer:

The correct answer is:

12 (c)

Explanation:

The codes as shown C8 and D9 represent cells in an Excel spreadsheet. the cell tags describe the column letter and row numbers. For instance; C8 means the 8th row in Column C, while D9 means; the 9th row in column D. In order to do mathematical functions with Excel, the formulae starting with an equal sign (=) like the one shown in this example are used. The function is solved as follows:

= 2*C8+3*D9

where:  

* = multiplication sign (×)

C8 = 3

D9 = 2

∴ 2*C8+3*D9 = (2 × 3) + (3 × 2)

= 6 + 6 = 12

∴ the result is 12

A ______ is a portable device for recording audio and video.
a. recorder
b. video
c. television
d. camcorder

Answers

Answer:

D

Explanation:

A camcorder is a portable device for recording audio and video. Thus, option D is correct.

What is video?

A video is a recording of a motion, or television program for playing through a television. A camcorder is a device or gadget that is used to record a video. audio is the sounds that we hear and mix media is a tool used to edit the video.

Digital camcorders are known to have a better resolution. The recorded video in this type of camcorder can be reproduced, unlike using an analog which losing image or audio quality can happen when making a copy. Despite the differences between digital and analog camcorders, both are portable, and are used for capturing and recording videos.

A camcorder is a self-contained portable electronic device with video and recording as its primary function. A camcorder is specially equipped with an articulating screen mounted on the left side, a belt to facilitate holding on the right side, hot-swappable battery facing towards the user, hot-swappable recording media, and an internally contained quiet optical zoom lens.

Therefore, A camcorder is a portable device for recording audio and video. Thus, option D is correct.

Learn more about recording audio on:

https://brainly.com/question/27861365

#SPJ5

4.8 Code Practice: Question 2

Answers

Answer:

for y in range(88, 43, -4):

   print(y, end=" ")

Explanation;

yw

what isa programming language

Answers

Answer:

programming language is a formal language comprising a set of instruction that produce various kind of out put.

Answer:

A programming language defines a set of instructions that are compiled together to perform a specific task by the CPU .hope it's helps you have a great day keep smiling be happy stay safe.

Mr. King is in an area on the taskbar opening programs he use frequently, what is this area called? Q_____ L_______h.

Answers

Answer:

Microsoft Windows. The default settings for the taskbar in Microsoft Windows place it at the bottom of the screen and includes from left to right the Start menu button, Quick Launch bar, taskbar buttons, and notification area.

Explanation:

Which statement correctly defines the function of a servo
motor's drive?
А.
It reverses the polarity of the magnets in the motor to change its direction.
B.
It amplifies the power to speed up the motor and generate more torque.
C.
It provides feedback to the controller about the motor's position and torque.
D
It computes the value of the torque produced for a given electric current.

Answers

Answer:

B . It amplifies the power to speed up the motor and generate more torque

Given the formula on the left, label the parts. SUM . A2:D19 . E55 . (A2:A19,C13,E55,19) .

Answers

Answer:

SUM  

✔ function name

.

A2:D19  

✔ range

.

E55  

✔ value

.

(A2:A19,C13,E55,19)  

✔ argument

Answer:

1. B

2. A

3. A

4. C

Explanation:

On edge2020 I got it right

.
Why does the Moon have a greater effect on the tides compared to the Sun? (Select WILL GIVE BRANLEST PLS ASAP

two.)
The Moon is smaller than the Sun.
The Sun is closer to the Earth.
The Moon’s gravitational pull on the Earth’s oceans is stronger.
The Moon is closer to the Earth.
The Sun’s gravitational pull on the Earth’s oceans is stronger.

Answers

The correct option that explains the reason why the moon will have greater effect on the tides compared to the sun is;

Option D; The moon is closer to the Earth

Generally speaking, ocean tides on the earth are caused by both the gravity of the moon and the gravity of the sun.

Now, the mode of generation of ocean tides is not really by the total strength of gravity, but rather by the gravitational gradient.

Gravitational gradient is simply defined as the gravity differences from one spot to the other.

Now, from research, we know that the sun is far larger than the moon and this means in consequence it will have a far stronger total gravity than that of the moon. However, due to the fact that the moon is closer to the earth than the sun, it means the moon would possess a stronger gravitational gradient than which the sun possesses.

Now, let's get to ocean tides; The ocean tides arise as a result of the response of ocean water to a gravitational gradient and as a result, the moon will have a greater role in creating tides than that which the sun does.  

Read more at; https://brainly.com/question/24996618

HELP PLEASE SOMEONE PLEASE HELP ME!!!!!

Answers

I feel it is C, but I am not sure if it is correct, I can only apologize if it is wrong.

True or False? You should never move an injured person.
True
False

Answers

Answer:

true

Explanation:

to develop vocabulary, a student should increase their

a) use of expression

b) use of punctuation

c) oral reading rate

d) familiarity with sight words

Answers

A because you can decrease the rest and none would make sense. I learned this!!

Write a for loop in python that prints 10 random numbers between 1 and 100 inclusive.

Answers

import random

for x in range(10):

   print(random.randint(1,100))

Can you guys list all the Linux Debian Apps that can only be installed on a Linux terminal command line, I have a deb 32 bit

Answers

Answer:

Two of those are apt and apt get and the other one is dpkg. 4. You will see a list of Debian Ensure package lists and installed packages are up to date. ... Apt may fail to remove old Linux kernels when using an Ubuntu version that 39 s ... To install it just enter the following command from the Terminal sudo apt install apt ...

Explanation:

Rewritable (write, erase, write again) is known as _______.

a.
CD-R
b.
CD-RW
c.
DVD-R
d.
CD-ROM

Answers

Answer:

c

Explanation:

What are 5 different google g-suite tools that you can use to collaborate and communicate with othe

Answers

it is google docs like stuff made by google

In which area of the screen can features and functions of Word be accessed?

Command
Ribbon
Scroll bar
Tab

Please Hurry!!!

Answers

Answer:

Toolbar

Explanation:

j don't know if you meant that instead of scroll bar, but tool bar is the answer

Answer:

scroll bar

Explanation:

Wilma is looking for facts about social media for her research project. What fact should she use for her project? a Communicating with social media is hard b Social media is a great way to communicate. c Social media is fun and easy to use. d The minimum age for some social media sites is 13. please help me and i will give you 5 stars

Answers

Answer:

d.

Explanation:

Out of all of the options listed, the only choice that is actually a fact would be that the minimum age for some social media sites is 13. This is because this age restriction is not set by the social media sites but instead by the Children's Online Privacy Protection Act (COPPA) within the US. The other options are all personal opinion, the most accurate of which is that "Social media is a great way to communicate." which there can still be someone with an opposing argument which means that it is not factual but instead it is an opinion.

At the start of the school year, Brianna’s history teacher announces that students’ final grades will be weighted based on how they do in three categories: exams (60 percent), final presentation (30 percent), and class participation (10 percent). Which strategy would most likely help Brianna get the best grade? spending more time studying for exams than creating the final presentation splitting her time evenly between studying for exams and creating the final presentation concentrating on creating the final presentation and speaking up in class working hard at participating in class and studying for exams

Answers

Answer:

it is A

Explanation:

did on edge test

Answer:

A

Explanation:

Which of the following allows a user to quickly access a frequently used computer app? Alphabetize program icons. O Create a shortcut on the toolbar. O Delete all icons on the screen. Place all icons in one folder.​

Answers

Answer:

Create a shortcut on the toolbar

Explanation:

You can use process of elimination. alphabetizing would not help to find it quicker, but it is an easier way. Deleting all icons wouldn't help at all. And placing all icons in a folder would make it even messier. So, it is creating a shortcut. Plus, I got it right on my test :)

Answer:

its a shortcut

Explanation:

took da test and got 100

pls help quick

Question #4. This question is worth 55 points. You may use the exam reference sheet. Write the pseudocode for a procedure, pow2() that displays the nth power of 2 where n is a parameter passed into the procedure. PROCEDURE pow2 ) { }​

Answers

Answer:

The procedure is completed as follows:

PROCEDURE pow2(n){

count = 1

power = 1

WHILE count <= n

       power = power * 2

       count = count + 1

END WHILE

Print power

}

Explanation:

A pseudocode is not a real code and might not be syntactically correct. This is so because it only helps to understand what the code is all about and gives and insight of the code.

Having said that, the pseudocode for the program is as follows:

This line defines the procedure

PROCEDURE pow2(n){

This line initializes count to 1

count = 1

This line initializes power to 1

power = 1

The following iteration iterates from 1 to n

WHILE count <= n

This calculates the nth power of 2

       power = power * 2

       count = count + 1

The ends the while loop

END WHILE

This prints the calculated power

Print power

The procedure ends here

}

Note that: I'll assume that the 4 lines illustrated do not mean that the pseudocode be written in just 4 lines.

So, more lines would be needed

If you were the manager at a firm which deals with sensitive information of its customers, employees and other stake holders, how would you make sure that information is protected and is altered only by the designated authorized person

Answers

Answer:

explanation below

Explanation:

Management controls are some of the techniques and mechanisms that can be put in place to implement security policies – which ensure information and information systems are protected.  These controls are not only used by managers but can be exercised by selected users.  

These controls must be put in place to cover all forms of information security, physical security and classification of those information.  

Which of these are reasons to evacuate a building during an earthquake? Mark all that apply.
A. There is a water leak.
B. The building is on fire.
C. There is structural damage to the building like newly cracked walls.
D. There is a gas leak.

Answers

Answer:

D

Explanation:

Answer:

c, b

Explanation:

I am guessing those are correct I don't really know

What is a characteristic of tasks in Outlook?
The Tasks view can be seen by clicking the View tab.

New tasks are created in the Appointments form

Tasks are an action item in Outlook and are basically a to-do list.

Old tasks are automatically deleted from your list​

Answers

Answer:

C. Tasks are an action item in Outlook and are basically a to-do list.

Explanation:

A to-do List is just a list of tasks.

and I took the test and got it right

Answer:

C. is correct

Explanation:

edg 2021

Other Questions
What is the difference between the green party and the socialist party? Explain the concept of microeconomics. PLEASE HELP meeeeee asap if there are 90 calories in 3/4 of yogurt, how many calories are in 3 cups of yogurt? Which equation represents a line that is perpendicular to y=x?A.y=2xB.y=x+6C.y=2xD.y=x+1 What is the product of (0.5)(1.2)(5.22)? 9.8 3.132 3.132 9.8What is the product of (0.5)(1.2)(5.22)? 9.8 3.132 3.132 9.8What is the product of (0.5)(1.2)(5.22)? 9.8 3.132 3.132 9.8What is the product of (0.5)(1.2)(5.22)? 9.8 3.132 3.132 9.8 What happens when a state law conflicts with a federal law?The state law, under the Supremacy Clause, is declared invalidThe Federal law, under the Supremacy Clause, is declared invalid.Article 6 of the Constitution encourages states to secede (leave the union).Article 2 of the Constitution states that this situation will not occur. 50 verbs past present future to copy paste now please 4. Savannah earns $247.00. How manyhours did she work?2 Please help me. Based on the questions and answers provided in the first column of the table below, fill in the empty box in the second column with the correct Spanish pronoun. 1. Tess est ms cansada que Luca.CiertoFalso2. Luca est tan contenta como Tess.CiertoFalso3. Tess est ms aburrida que Luca.CiertoFalso4. Luca est menos triste que Tess.Cierto Falso What is chemical weathering Which element from "The Three Sisters" is drawn from "The Three Fates" of Greek mythology? what is an example of an innovative solution to an engineering problem? Explain briefly why you chose this answer. pls help Solve for m. -7+4m+10=15-2m Alice is saving her chore money in a bank account. She makes a deposit of $13 in the first month.Then every month, she deposits $25. Alice forms a sequence of her account balance, An where nrefers to the number of months. Which of the statements are correct about this sequence?Select all that apply. 1) Mark's new car can drive 725 miles in 8hours. How many miles can Mark's car drive inan hour? How many miles can his car drive in3 hours? Anthony's flight arrives at eight, but the cab driver forgot to pick him up.Which choice best corrects the verb tense error?O Anthony's flight has arrived at eight, but the cab driver forgets to pick him up. Anthony's flight will arrive at eight, but the cab driver forgot to pick him up.O Anthony's flight arrived at eight, but the cab driver forgot to pick him up.Anthony's flight will arrive at eight, but the cab driver forgets to pick him up. Which method of freezing preserves the quality and taste of food? Explain the role of a cultural hearth in the spread of cultural traits through diffusion.