简体   繁体   English

如何在 python 中检查字符串是否包含特定字符

[英]How to check if a string contains a specific character or not in python

I am new to python, but fairly experienced in programming.我是 python 的新手,但在编程方面经验丰富。 While learning python I was trying to create a simple function that would read words in from a text file (each line in the text file is a new word) and then check if the each word has the letter 'e' or not.在学习 python 时,我试图创建一个简单的 function,它可以从文本文件中读取单词(文本文件中的每一行都是一个新单词),然后检查每个单词是否包含字母“e”。 The program should then count the amount of words that don't have the letter 'e' and use that amount to calculate the percentage of words that don't have an 'e' in the text file.然后程序应该计算没有字母“e”的单词的数量,并使用该数量来计算文本文件中没有“e”的单词的百分比。

I am running into a problem where I'm very certain that my code is right, but after testing the output it is wrong.我遇到了一个问题,我非常确定我的代码是正确的,但是在测试 output 之后它是错误的。 Please help!请帮忙!

Here is the code:这是代码:

def has_n_e(w):
    hasE = False
    for c in w:
        if c == 'e':
            hasE = True
    return hasE

f = open("crossword.txt","r")
count = 0

for x in f:
    word = f.readline()
    res = has_n_e(word)
    if res == False:
        count = count + 1

iAns = (count/113809)*100 //113809 is the amount of words in the text file
print (count)
rAns = round(iAns,2)
sAns = str(rAns)
fAns = sAns + "%"
print(fAns)

Here is the code after doing some changes that may help:这是进行一些可能有帮助的更改后的代码:

def has_n_e(w):
    hasE = False
    for c in w:
        if c == 'e':
            hasE = True
    return hasE

f = open("crossword.txt","r").readlines()
count = 0

for x in f:
    word = x[:-1]
    res = has_n_e(word)# you can use ('e' in word) instead of the function
    if res == False:
        count = count + 1

iAns = (count/len(f))*100 //len(f) #is the amount of words in the text file
print (count)
rAns = round(iAns,2)
sAns = str(rAns)
fAns = sAns + "%"
print(fAns)

Hope this will help希望这会有所帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在python中替换包含特定字符的字符串? - How to replace a string that contains a specific character in python? 如何使用 Python 中的 range() 函数和布尔值检查字符串是否包含特定字符? - How to check check if a string contains a specific character(s) using range() functions and bools in Python? 如何检查python字符串列表在字符串的最后一个字母中是否包含特定字符? - How to check if a python string list contains a specific character in the last letter of a string? 如何在Python中检查列表是否包含特定字符串 - how to check if a list contains a specific string or not in python 如何检查字符是否在 Python 的字符串中的特定 position 中? - How to check if a character is in a specific position in a string in Python? Python:如何检查unicode字符串是否包含一个cased字符? - Python: How to check if a unicode string contains a cased character? Python:检查字符串是否包含中文字符? - Python: Check if a string contains chinese character? 检查字符串是否包含所需的字符 position python - check if the string contains the desired Character position python 检查字符串是否在python中包含字符 - check whether a string contains a character in python 检查字符串是否只包含python中的某个字符 - Check if string only contains a certain character in python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM