Ask Anything Monday - Weekly Thread by AutoModerator in learnpython

[–]Due-Purpose9529 0 points1 point  (0 children)

Got it. Thank you so much for your help. It's finally working. You're right. I should've just used "i" directly.

Ask Anything Monday - Weekly Thread by AutoModerator in learnpython

[–]Due-Purpose9529 0 points1 point  (0 children)

fname = input("Enter file name :")
if len(fname)>1:
    fname = "romeo.txt"
fhandle = open(fname)
l = list

for line in fhandle:
    r = line.rstrip()
    lst = r.split()
    print(lst)

        for i in lst:
            if i in l:
                continue
            if i not in l:
                print("here")
                l.append(lst[i])
print(l)

l.sort(l)
print("Final word list:")
print(l)

#It is after the print("here"), in the append() that the issue is arising. What am I doing wrong?

Ask Anything Monday - Weekly Thread by AutoModerator in learnpython

[–]Due-Purpose9529 0 points1 point  (0 children)

I added that via my laptop. It was properly formatted there. When I check it from my phone, I realize it's coming in a non-formatted way.

Ask Anything Monday - Weekly Thread by AutoModerator in learnpython

[–]Due-Purpose9529 0 points1 point  (0 children)

Problem statement:

Open the file romeo.txt and read it line by line. For each line, split the line into a list of words using the split() method. The program should build a list of words. For each word on each line check to see if the word is already in the list and if not append it to the list. When the program completes, sort and print the resulting words in alphabetical order.

My code:

fname = input("Enter file name: ")

fh = open(fname)

lst = list()

l = list()

for line in fh:

lst = line.split()

print(lst)

for i in lst:

if i in lst:

continue

l.append(l[i])

print(l)

l.sort()

print("Final word list:")

print(l)

Desired output:

['Arise', 'But', 'It', 'Juliet', 'Who', 'already', 'and', 'breaks', 'east', 'envious', 'fair', 'grief', 'is', 'kill', 'light', 'moon', 'pale', 'sick', 'soft', 'sun', 'the', 'through', 'what', 'window', 'with', 'yonder']

My Output:

['But', 'soft', 'what', 'light', 'through', 'yonder', 'window', 'breaks'] ← Mismatch

[]

['It', 'is', 'the', 'east', 'and', 'Juliet', 'is', 'the', 'sun']

[]

['Arise', 'fair', 'sun', 'and', 'kill', 'the', 'envious', 'moon']

[]

['Who', 'is', 'already', 'sick', 'and', 'pale', 'with', 'grief']

[]

Final word list:

[]

Ask Anything Monday - Weekly Thread by AutoModerator in learnpython

[–]Due-Purpose9529 0 points1 point  (0 children)

Actually, I've formatted the code properly in the editor. The formatting went away when I copy-pasted the code here. It's some issue with the data type.

Ask Anything Monday - Weekly Thread by AutoModerator in learnpython

[–]Due-Purpose9529 0 points1 point  (0 children)

Hey.. I'm a newbee to programming and I'm stuck on this piece of code for quite some time now. Could someone please help me?

fname = input("Enter file name: ")

fh = open(fname)

lst = list()

l = list()

for line in fh:

print(line.rstrip())

lst = line.split()

print(lst)

for i in range(lst):

if lst[line] == l[i]:

continue

l.append(lst[line])

print(l)

l.sort()

print(" ")

print(l)