def file(char, filename):
for currentFile in filename:
print(currentFile.strip())
def string(char, str):
count = 0
if char in 'abcdefghijklmnopqrstuvwxyz':
count += 1
string(char,str)
else:
print("Incorrect Letters")
print(count)
def main():
char = input("Enter character: ")
openFile = input("Enter the filename: ")
filename = open(openFile)
file(char, filename)
string(char, str)
main()
我想要計算某些字符,例如,如果我要在字符輸入提示中放入「W」,它應該只計算W.我該怎麼做?我正在嘗試在def字符串函數中執行遞歸蟒蛇計數某些字符
謝謝。
看一看這個http://stackoverflow.com/questions/1155617/count-occurrence-of-a-character-in-a-python -string –
'file'和'string'已經存在於python中,所以不要把它們當作我thod的名字。 –
由於你沒有變量'str',這行:'string(char,str)'不起作用。 –