0
How do I create a dictionary that uses len(word) to print all words = to len (word)
word=open ("word.txt","r") for line in word: word =line.split () count =(len (line)-1) num= input (enter number) ### so as to print the word with letters equal to the number in the input.
3 Respostas
0
First of all, a few words of warning:
1) it is recommended to name your variables so that it is clear what they are (for example, "word" on the first line is actually a file, not a word);
2) it is NOT recommended to overwrite a variable that you are already using, especially while you are iterating over it (you are changing "word" from a file to a list of words, while you are iterating over the lines in that file);
3) you should always initialize your variables - in this particular case, "count".
Here is one possible way of accomplishing what you want.
myfile=open ("word.txt","r")
count=0
for line in myfile:
wordlist=line.split()
for word in wordlist:
if len(word)==15:
count = count+1
print(count)
The requirement for a dictionary is still unclear for me. If you explain more on why you want to use a dictionary, maybe we will be able to help you with that as well.
0
It's difficult to figure out what you want - you want to get a list of words on each line, and create a dictionary with them? In that case, why would you use the same key for all of them, and why would you use the LINE length (and not the word length) for the key?
Could you clarify what you want to do?
0
let's say I want to print out all words with length 15 letters in the text file