For a loop inside, if the condition

print "Select the action you want to perform(A or B)"
print "(A) uppper case"
print "(B) count number of lines"

option = raw_input("Enter your option(A or B):")

if option.upper() == "A":
    for line in x:
        line = line.upper()

    print line


elif option.upper() == "B":
    for line in x:
        line = line.upper()
        count = count + 1
    print "total lines:", count

else:
    print "incorrect option"
    exit()

      

When the user types A , it only prints one line (last line) in uppercase instead of 250 lines.

Counter

works great.


PS I have not provided the first part of the code, which is for entering the file name to open.

+3


source to share


2 answers


just type your print statement as:

if option.upper() == "A":
    for line in x:
        line = line.upper()

        print line

      



Python plays a very important role in indentation, by deviating from the print statement (as in this code), the interpreter will consider it outside of the loop for

, and it will only be executed once after the complete loop execution for

.

To execute a statement print

on each iteration of the loop for

, you need to defer it inside the scope of the for loop.

+6


source


the line print line

must be indented inside the loopfor line in x



+1


source







All Articles