Your task is to write a program that loops through the words in the provided jane_eyre.txt file and counts those words which fulfill both of the following conditions:
The word has more than 10 characters
The word does not contain the letter "e"
Once you've finished the aggregation, let your program print out the following message:
There are 10 long words without an 'e'.
My work:
count = 0
f = open("jane_eyre.txt").read()
words = f.split()
if len(words) > 10 and "e" not in words:
count += 1
print("There are", count, "long words without an 'e'.")
The count result is 1 but it should be 10. What's wrong with my work??
You have to iterate on each word, with best practice:
with open('jane_eyre.txt') as fp: # use a context manager
count = 0 # initialize counter to 0
for word in fp.read().split(): # for each word
if len(word) > 10 and not 'e' in word: # your condition
count += 1 # increment counter
print(f"There are {count} long words without an 'e'.") # use f-strings
But pay attention to punctuation: "imaginary," is 10 characters length but the word itself has only 9 characters.
You need to loop through each word. So something like:
count = 0
f = open("jane_eyre.txt").read()
words = f.split()
for word in words:
if len(word) > 10 and "e" not in word:
count += 1
print("There are", count, "long words without an 'e'.")
Related
I am very new to python and new to this website as well so if this question isn't worded well I apologize. Basically, the program I am trying to create is one that takes user inputs, counts the words in the inputs, and then also counts the occurrences of a specific list of common words. My problem is that when using a test case that has more than one occurrence of a word in my list, the counter does not count past 1. For example, if the user input is "A statement is a program instruction", it will only count the use of the word "a" one time. Below I have included my code, I also want to preface this with being my first attempt at using loops. I believe my problem lies within lines 31-32.
#step1
from re import X
words = ['the','be','to', 'of', 'and','a','in','that','have','i']
frequency = [0,0,0,0,0,0,0,0,0,0]
assert len(words) == len(frequency), "the length of words and frequency should be equal!"
total_frequency = 0
#step2
text = input("Enter some text:").lower()
#step3 repeated until == quit through step 7
while text != 'quit':
#step4 split the input
text_list = text.split(' ')
#step5 update the frequency
total_frequency = total_frequency+len(text_list)
#step6 update the counter
for idx, this_word in enumerate(words):
# in the inner loop, you have to compare this_word with each element of text_list if they are the same, add on to frequency[idx]
if this_word in text_list:
frequency[idx]+=1
#step7: ask for another input
text = input("Enter some text:").lower()
#step 8 format output
keys = ' '.join(words)
print(f'Total number of words:{total_frequency}')
print("{:^50}".format('Word frequency of top ten words'))
print('-'*50)
print(keys)
for idx, this_word in enumerate(words):
print(f'{frequency[idx]:^{len(this_word)}} ',end="")
print()
# END
OUTPUT:
Enter some text:A statement is a program instruction
Enter some text:quit
Total number of words:6
Word frequency of top ten words
--------------------------------------------------
the be to of and a in that have i
0 0 0 0 0 1 0 0 0 0
Replace your inner loop with this:
for word in text_list:
if word in words:
frequency[words.index(word)] += 1
and you will get the results you want. Note that it would be better to store your words and frequencies in a dict, so you could let Python do the searching, like this:
words = {'the':0,'be':0,'to':0, 'of':0, 'and':0,'a':0,'in':0,'that':0,'have':0,'i':0}
total_frequency = 0
#step2
text = input("Enter some text:").lower()
#step3 repeated until == quit through step 7
while text != 'quit':
#step4 split the input
text_list = text.split(' ')
#step5 update the frequency
total_frequency = total_frequency+len(text_list)
#step6 update the counter
for word in text_list:
if word in words:
words[word] += 1
text = input("Enter some text:").lower()
#step 8 format output
keys = ' '.join(words.keys())
print(f'Total number of words:{total_frequency}')
print("{:^50}".format('Word frequency of top ten words'))
print('-'*50)
print(keys)
for word, cnt in words.items():
print(f'{cnt:^{len(word)}} ',end="")
print()
if this_word in text_list returns true if 'this_word' is in there. you need another loop.
for word in text_list:
if this_word == word:
frequency[idx]+=1
I'm new to programming in python and I have a challenge that I've been attempting for a few days now but I can't seem to figure out what is wrong with my code. My code take a text file and tells me how many sentences, words, and syllables are in the text. I have everything running fine except my code is counting a syllable containing consecutive vowels as multiple syllables and I can't seem to figure out how to fix it. Any help at all would be appreciated.
For example if the file has this:
"Or to take arms against a sea of troubles, And by opposing end them? To die: to sleep."
It should come out as saying the text has 21 syllables but the program tells me it has 26 because it counts the consecutive vowels more than once.
fileName = input("Enter the file name: ")
inputFile = open(fileName, 'r')
text = inputFile.read()
# Count the sentences
sentences = text.count('.') + text.count('?') + \
text.count(':') + text.count(';') + \
text.count('!')
# Count the words
words = len(text.split())
# Count the syllables
syllables = 0
vowels = "aeiouAEIOU"
for word in text.split():
for vowel in vowels:
syllables += word.count(vowel)
for ending in ['es', 'ed', 'e']:
if word.endswith(ending):
syllables -= 1
if word.endswith('le'):
syllables += 1
# Compute the Flesch Index and Grade Level
index = 206.835 - 1.015 * (words / sentences) - \
84.6 * (syllables / words)
level = int(round(0.39 * (words / sentences) + 11.8 * \
(syllables / words) - 15.59))
# Output the results
print("The Flesch Index is", index)
print("The Grade Level Equivalent is", level)
print(sentences, "sentences")
print(words, "words")
print(syllables, "syllables")
Instead of counting the number of occurrences of each vowel for each word, we can iterate through the characters of the word, and only count a vowel if it isn't preceded by another vowel:
# Count the syllables
syllables = 0
vowels = "aeiou"
for word in (x.lower() for x in text.split()):
syllables += word[0] in vowels
for i in range(1, len(word)):
syllables += word[i] in vowels and word[i - 1] not in vowels
for ending in {'es', 'ed', 'e'}:
if word.endswith(ending):
syllables -= 1
if word.endswith('le'):
syllables += 1
Trying to count the total number of words in a for loop but sum() method isn't working and failed trying to use list-append method:
for line in open("jane_eyre.txt"):
strip = line.rstrip()
words = strip.split()
for i in words:
length = len(i)
if length > 10 and "e" not in i:
print(i)
#Can't find a way to make it work here..
sum(i)
The output of the words are:
possibility
drawingroom
disposition
drawingroom
introductory
accumulation
introductory
surrounding
continually
inflictions
*Couldn't find a way to make get "10" as the sum.
Thanks.
you can use this...
c = 0 #number of words
for line in open("jane_eyre.txt"):
strip = line.rstrip()
words = strip.split()
for i in words:
length = len(i)
if length > 10 and "e" not in i:
print(i)
#Can't find a way to make it work here..
c +=1 #increasing number of words
print(c)
sum() is not what you're looking for here. To understand sum() usage better, have a read of the documentation.
You can store a wordcount prior to the loop and increase the value every time you match your if statement.
words = ['hello', 'these', 'are', 'words', 'they', 'get', 'longer', 'indtroductory']
wordcount = 0
for word in words:
if len(word) > 10 and "e" not in word:
print(word)
wordcount += 1
#indtroductory
print(wordcount)
#1
If you need to access the words later, you could append them to a list and count the objects that exist within the list to obtain the count as well.
Try here:
count = 0
for line in open("jane_eyre.txt"):
strip = line.rstrip()
words = strip.split()
for i in words:
length = len(i)
if length > 10 and "e" not in i:
print(i)
#Can't find a way to make it work here..
count = count + 1
print(count)
Hello I have recently been trying to create a progam in Python 3 which will read a text file wich contains 23005 words, the user will then enter a string of 9 characters which the program will use to create words and compare them to the ones in the text file.
I want to print words which contains between 4-9 letters and that also contains the letter in the middle of my list. For example if the user enters the string "anitsksem" then the fifth letter "s" must be present in the word.
Here is how far I have gotten on my own:
# Open selected file & read
filen = open("svenskaOrdUTF-8.txt", "r")
# Read all rows and store them in a list
wordList = filen.readlines()
# Close File
filen.close()
# letterList index
i = 0
# List of letters that user will input
letterList = []
# List of words that are our correct answers
solvedList = []
# User inputs 9 letters that will be stored in our letterList
string = input(str("Ange Nio Bokstäver: "))
userInput = False
# Checks if user input is correct
while userInput == False:
# if the string is equal to 9 letters
# insert letter into our letterList.
# also set userInput to True
if len(string) == 9:
userInput = True
for char in string:
letterList.insert(i, char)
i += 1
# If string not equal to 9 ask user for a new input
elif len(string) != 9:
print("Du har inte angivit nio bokstäver")
string = input(str("Ange Nio Bokstäver: "))
# For each word in wordList
# and for each char within that word
# check if said word contains a letter from our letterList
# if it does and meets the requirements to be a correct answer
# add said word to our solvedList
for word in wordList:
for char in word:
if char in letterList:
if len(word) >= 4 and len(word) <= 9 and letterList[4] in word:
print("Char:", word)
solvedList.append(word)
The issue that I run into is that instead of printing words which only contain letters from my letterList, it prints out words which contains at least one letter from my letterList. This also mean that some words are printed out multiple time, for example if the words contains multiple letters from letterList.
I've been trying to solve these problems for a while but I just can't seem to figure it out. I Have also tried using permutations to create all possible combinations of the letters in my list and then comparing them to my wordlist, however I felt that solution was to slow given the number of combinations which must be created.
# For each word in wordList
# and for each char within that word
# check if said word contains a letter from our letterList
# if it does and meets the requirements to be a correct answer
# add said word to our solvedList
for word in wordList:
for char in word:
if char in letterList:
if len(word) >= 4 and len(word) <= 9 and letterList[4] in word:
print("Char:", word)
solvedList.append(word)
Also since I'm kinda to new to python, if you have any general tips to share, I would really appreciate it.
You get multiple words mainly because you iterate through each character in a given word and if that character is in the letterList you append and print it.
Instead, iterate on a word basis and not on a character basis while also using the with context managers to automatically close files:
with open('american-english') as f:
for w in f:
w = w.strip()
cond = all(i in letterList for i in w) and letterList[4] in w
if 9 > len(w) >= 4 and cond:
print(w)
Here cond is used to trim down the if statement, all(..) is used to check if every character in the word is in letterList, w.strip() is to remove any redundant white-space.
Additionally, to populate your letterList when the input is 9 letters, don't use insert. Instead, just supply the string to list and the list will be created in a similar, but noticeably faster, fashion:
This:
if len(string) == 9:
userInput = True
for char in string:
letterList.insert(i, char)
i += 1
Can be written as:
if len(string) == 9:
userInput = True
letterList = list(string)
With these changes, the initial open and readlines are not needed, neither is the initialization of letterList.
You can try this logic:
for word in wordList:
# if not a valid work skip - moving this check out side the inner for-each will improve performance
if len(word) < 4 or len(word) > 9 or letterList[4] not in word:
continue
# find the number of matching words
match_count = 0
for char in word:
if char in letterList:
match_count += 1
# check if total number of match is equal to the word count
if match_count == len(word):
print("Char:", word)
solvedList.append(word)
You can use lambda functions to get this done.
I am just putting up a POC here leave it to you to convert it into complete solution.
filen = open("test.text", "r")
word_list = filen.read().split()
print("Enter your string")
search_letter = raw_input()[4]
solved_list = [ word for word in word_list if len(word) >= 4 and len(word) <= 9 and search_letter in word]
print solved_list
This is what I have so far:
while len(words) != 5:
words = raw_input("Enter a 5 worded sentence: ").split()
print "Try again. The word count is:", wordCount
if len(words) == 5:
print "Good! The word count is 5!"
The problem is I get this:
Enter a 5 worded sentence: d d d d
Try again. The word count is: 4
Enter a 5 worded sentence: d d d d d d
Try again. The word count is: 4
Enter a 5 worded sentence: d d d d d
Try again. The word count is: 4
Good! The word count is 5!
When I enter more or less than 5 words, it keeps that word count and doesn't change.
Since Python doesn't have a do-while loop like some other languages, this idiom prevents duplication of the raw_input function, and makes sure the loop runs at least once. Make sure to update word_count after getting new input.
while 1:
words = raw_input("Enter a 5 worded sentence: ").split()
word_count = len(words)
if word_count == 5: break
print "Try again. The word count is:", word_count
print "Good! The word count is 5!"
You just need to re-order some of your logic:
# prompt before entering loop
words = raw_input("Enter a 5 worded sentence: ").split()
while len(words) != 5:
print "Try again. The word count is:", len(words)
words = raw_input("Enter a 5 worded sentence: ").split()
# no need to test len again
print "Good! The word count is 5!"
The variable wordCount should be updated inside the loop, after you accept the input. Only then it will reflect the new value. Something like this:-
while len(words) != 5:
words = raw_input("Enter a 5 worded sentence: ").split()
wordCount = len(words)
print "Try again. The word count is:", wordCount
if len(words) == 5:
print "Good! The word count is 5!"
I think your code snippet is missing parts. Anyways, you should evaluate the wordCount after raw_input so that it gets updated with new values.
wordCount = 0
while wordCount != 5:
words = raw_input("Enter a 5 worded sentence: ").split()
wordCount = len(words)
print "Try again. The word count is:", wordCount
print "Good! The word count is 5!"
def xlen(string_data):
try:
count = 0
while 1:
string_data[count]
count = count + 1
except(IndexError):
print count
xlen('hello')