Getting a type error from the num = re.search line

时间:2017-12-18 05:55:28

标签: python

This code is creating this type error TypeError: search() missing 1 required positional argument: 'string'

here is the code

import re
f=open('JokesPC.txt', 'w')
f.write('how does a king react when he farts?')
print("how does a king react when he farts?")
#answer is nothing noble gases don't cause reactions
answer = input("what is your answer?")
f.write(answer)
f.close()
f = open('JokesPC.txt', 'r')
count = 0
num = 0
for line in f:
    num = re.search("?P<[G|g]host>")
    for i in num:
        count += 1

print(count)
f.close()

1 个答案:

答案 0 :(得分:0)

search expects 2 arguments first one is pattern and second string in which you want to search pattern. As you're not providing second argument which is providing a string to look up regex pattern you're getting error. So as a fix just replace following line of code

num = re.search("?P<[G|g]host>")

with

num = re.search("?P<[G|g]host>", line)
相关问题