So I made this script:
names = open("names.txt","r")
for loop in names:
if loop.endswith('s'):
print(loop)
for loop in names:
if loop.startswth('A'):
print(loop)
There was a file called names.txt
which had 10 names in it. This program is supposed to print all the names that start with A and names that end with s but it doesn't work. Can you help me?
You've got a couple problems:
names
empty and bypasses the body of the loop)endswith('s')
, but almost every line would actually end with a newline, which you haven't strippedAs a minimal fix for both problems (maintaining the behavior of two loops so you print the lines the end with
s
separately and first):If any passing line being printed on a single pass is fine, and you don't need lines beginning with
A
and ending withs
to be printed twice, you can simplify to: