0

I am new to python (2nd) day and working on a problem that asks me to Write a program that reads ASCII files (asks for file name as input), checks if it has more than two words and prints out the two first words of the file on screen.

Its a little vague but I am going to assume the file is all str, deliminiated by spaces.

ex.

text1 text2 text text text

So far I have:

name = (raw_input("Please enter the name of the file: "))
f=open(name)
with codecs.open(name, encoding='utf-8') as f:
    for line in f:
        line = line.lstrip(BOM)
words=line.split()
print words
if len(words) > 2:
    print 'There are more than two words'
    firsttow = words[:2]
    print firstrow

I am having problems writing the else statement, I want to have,

if len(words) > 2:
    print 'There are more than two words'
    firsttow = words[:2]
print firstrow
else: 
if len(words) <2:
        print 'There are under 2 words, no words will be shown'

How should this be added and is there any other ways to improve my code for this questions? I really appreciate the help

Thanks in advance

*Edit: Thanks for all the help, the last problem I had was when i run .py file, I want to be able to see the results before the cmd window closes.

Adding: raw_input("Press return to close this window...") does notwork and it closes right away. Any ideas?

Edit2* This is my current code, still trying to work on having the cmd window open after

import codecs
BOM = codecs.BOM_UTF8.decode('utf8')
name = (raw_input("Please enter the name of the file: "))

with codecs.open(name, encoding='utf-8') as f:
    words=[]            #define words here
    for line in f:
        line = line.lstrip(BOM)
        words.extend(line.split())        #append words from each line to words  

if len(words) > 2:
    print 'There are more than two words'
    firstrow = words[:2]
    print firstrow                #indentation problem here
elif len(words) <2:                    #use if
    print 'There are under 2 words, no words will be shown'

raw_input("Press return to close this window...")
3
  • Note: You are actually opening the file twice for no reason. And due to the for loop you will only look at the file's last line. Commented Sep 24, 2012 at 16:04
  • Thanks, I got rid of the extra open Commented Sep 24, 2012 at 16:07
  • @MatthewLiem where did you placed the raw_input("Press return to close this window...")? Commented Sep 24, 2012 at 16:16

2 Answers 2

1

That code should be written as:

if len(words) > 2:
    print 'There are more than two words'
    firsttow = words[:2]
    print firstrow
elif len(words) <2:
    print 'There are under 2 words, no words will be shown'

Note the indentation, and the use of elif (which means "else if").

Sign up to request clarification or add additional context in comments.

1 Comment

AHHH thats the problem, it was my indentation..was stuck on why my else or elif would never work. thanks!!
0
with codecs.open(name, encoding='utf-8') as f:
    words=[]            #define words here
    for line in f:
        line = line.lstrip(BOM)
        words.extend(line.split())        #append words from each line to words      

if len(words) > 2:
    print 'There are more than two words'
    firsttow = words[:2]
    print firstrow                #indentation problem here
if len(words) <2:                    #use if
    print 'There are under 2 words, no words will be shown'

2 Comments

Thanks, is there a reason you use another if rather than else?
@MatthewLiem both will work fine, but using elif here makes more sense.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.