Python 3.x 没有打印出正确的字母-Python
问题是: A-编写一个名为has_no_e的函数,如果给定的单词中没有字母“e”,该函数将返回True 它 B-修改您以前的程序,只打印没有“e”的单词,并计算 列表中没有“e”的单词百分比Python 3.x 没有打印出正确的字母-Python,python-3.x,letters,Python 3.x,Letters,问题是: A-编写一个名为has_no_e的函数,如果给定的单词中没有字母“e”,该函数将返回True 它 B-修改您以前的程序,只打印没有“e”的单词,并计算 列表中没有“e”的单词百分比 def main(): file = input("Enter a file: ") file_output = open(file, 'r') output = (file_output.read()) for line in output: word =
def main():
file = input("Enter a file: ")
file_output = open(file, 'r')
output = (file_output.read())
for line in output:
word = line.strip()
if len(word)>20:
print (word)
no_e = has_no_e(word)
count = 0
for line in output:
word = line.strip()
if has_no_e(word):
count +=1
print (word)
percent = (count/1)*100 #Im still working on the percentage part
print (percent)
def has_no_e(word):
for char in word:
if char in 'Ee':
return False
return True
输出:
c
h
我
c
o
o
l
900.0
问题:所以我的问题是,不是打印出整个单词,而是打印出每个字母(e除外)。我试图让它打印出整个单词,如果它没有字母e。例如,我的档案说“奶酪很酷”。我希望它只输出“很酷”。我该怎么做呢?另外,对于百分比:如何将单词组合为一个字母而不是单个字母?这是我可以做的百分比(count/#words)*100。file\u output.read()
读取整个文件并将其作为字符串返回。因此,output
是字符串“cheese is good”
问题是字符串中的行:。您希望它有line=“cheese”
,然后是line=“is”
,最后是line=“good”
然而,它却一个字符接一个字符。因此,它使用line='c'
运行循环,然后使用line='h'
等等。要使其按预期执行,您需要在换行符或空格上拆分输出
(取决于文件分隔单词的方式),并遍历结果列表