File tree 4 files changed +66
-65
lines changed
1 File handle/File handle text
4 files changed +66
-65
lines changed Original file line number Diff line number Diff line change 3
3
using read function
4
4
and display those words, which are less than 4 characters. """
5
5
6
- F = open ("story.txt" , "r" )
7
- value = F .read ()
8
- lines = value .split ()
9
- count = 0
6
+ def display_words ():
7
+ with open ("story.txt" ) as F :
8
+ lines = F .read ()
9
+ words = lines .split ()
10
+ count = 0
11
+ for word in words :
12
+ if (len (word ) < 4 ):
13
+ print (word )
14
+ count += 1
15
+ return count
16
+
17
+ if __name__ == "__main__" :
18
+ print (display_words ())
19
+
20
+
21
+
10
22
11
- for i in lines :
12
- if len (i ) < 4 :
13
- print (i )
14
- count += 1
15
- else :
16
- pass
17
23
18
- print ("The total number of words with length less than 4 are" , count )
Original file line number Diff line number Diff line change 3
3
4
4
5
5
def lowercase ():
6
- F = open ("happy.txt" , "r" )
7
- count = 0
8
- count_ = 0
9
- value = F .read ()
10
- for i in value :
11
- if i .islower ():
12
- count += 1
13
- elif i .isupper ():
14
- count_ += 1
15
- print ("The total number of lower case letters are" , count )
16
- print ("The total number of upper case letters are" , count_ )
17
- print ("The total number of letters are" , count + count_ )
6
+ with open ("happy.txt" ) as F :
7
+ count_lower = 0
8
+ count_upper = 0
9
+ value = F .read ()
10
+ for i in value :
11
+ if i .islower ():
12
+ count_lower += 1
13
+ elif i .isupper ():
14
+ count_upper += 1
15
+ print ("The total number of lower case letters are" , count_lower )
16
+ print ("The total number of upper case letters are" , count_upper )
17
+ print ("The total number of letters are" , count_lower + count_upper )
18
18
19
-
20
- lowercase ()
19
+ if __name__ == "__main__" :
20
+ lowercase ()
Original file line number Diff line number Diff line change 3
3
4
4
5
5
def lowercase ():
6
- F = open ("happy.txt" , "r" )
7
- count = 0
8
- count_ = 0
9
- value = F .read ()
10
- for i in value :
11
- if i .islower ():
12
- count += 1
13
- elif i .isupper ():
14
- count_ += 1
15
- print ("The total number of lower case letters are" , count )
16
- print ("The total number of upper case letters are" , count_ )
17
- print ("The total number of letters are" , count + count_ )
6
+ with open ("happy.txt" ) as F :
7
+ count_lower = 0
8
+ count_upper = 0
9
+ value = F .read ()
10
+ for i in value :
11
+ if i .islower ():
12
+ count_lower += 1
13
+ elif i .isupper ():
14
+ count_upper += 1
15
+ print ("The total number of lower case letters are" , count_lower )
16
+ print ("The total number of upper case letters are" , count_upper )
17
+ print ("The total number of letters are" , count_lower + count_upper )
18
18
19
-
20
- lowercase ()
19
+ if __name__ == "__main__" :
20
+ lowercase ()
Original file line number Diff line number Diff line change 4
4
5
5
# step1:
6
6
def write_to_file ():
7
- F = open ("happy.txt" , "a" )
8
- while True :
9
- text = input ("enter any text" )
10
-
11
- F .write (
12
- text + "\n "
13
- ) # write function takes exactly 1 arguement so concatenation
14
- choice = input ("do you want to enter more, y/n" )
15
- if choice == "n" :
16
- break
17
- F .close ()
18
-
19
-
7
+ with open ("happy.txt" , "a" ) as F :
8
+ while True :
9
+ text = input ("enter any text" )
10
+ F .write (
11
+ text + "\n "
12
+ ) # write function takes exactly 1 arguement so concatenation
13
+ choice = input ("do you want to enter more, y/n" )
14
+ if choice == "n" :
15
+ break
16
+
20
17
# write_to_file()
21
18
22
19
# step2:
23
20
def check_first_letter ():
24
- F = open ("happy.txt" , "r" )
25
- value = F .read ()
26
- count = 0
27
- line = value .split ()
28
- for i in line :
29
- if i [0 ] in ["m" , "M" , "i" , "I" ]:
30
- count += 1
31
- print (i )
32
-
33
- print ("The total number of sentences starting with I or M are" , count )
34
-
35
-
36
- check_first_letter ()
21
+ with open ("happy.txt" ) as F :
22
+ value = F .read ()
23
+ count = 0
24
+ line = value .split ()
25
+ for i in line :
26
+ if i [0 ] in ["m" , "M" , "i" , "I" ]:
27
+ count += 1
28
+ print (i )
29
+ print ("The total number of sentences starting with I or M are" , count )
30
+
31
+ if __name__ == "__main__" :
32
+ check_first_letter ()
You can’t perform that action at this time.
0 commit comments