Python script help, need assistance writing a code that will take a text file an
ID: 670233 • Letter: P
Question
Python script help, need assistance writing a code that will take a text file and out put the contents for what I am looking for. Been stuck for a while, thanks for any help given!
You have a file quotes.txt containing quotes, one per line, on security. Write a function quote() that takes a piece of text (i.e., a string) as input and prints any quote that contains that piece of text.
>>> quote('watch')
>>> quote('who')
'Anyone who considers arithmetical methods of producing random digits is, of course, in a state of sin.' John Von Neumann
>>> quote('is')
'The price of freedom is eternal vigilance.' Thomas Jefferson
'Anyone who considers arithmetical methods of producing random digits is, of course, in a state of sin.' John Von Neumann
'And so it often happens that an apparently ingenious idea is in fact a weakness which the scientific cryptographer seizes on for his solution.' Herbert Yardley
Explanation / Answer
def quote(q):
with open('quotes.txt') as fp:
for line in fp:
if q in line:
print(line)
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.