0

As title says... This is just an exercise I'm doing to get better at python. Any help would be greatly appreciated

So this is what i have tried...

read = open('file.txt', 'r')
t = read.read()
for i[0] in t:
    if i[0] == 'r' or 'R':
        print(i[0])

and...

read = open('file.txt', 'r')
t = read.read()
for i[0] in t:
    if i[0] == 'r' or 'R':
        print(i[0])

also...

EDIT....... I have been able to find the word with 'R' and 'r' but now I cant figure out how to print the word containing the 'r' and 'R'

for i in t:
if i[0] == 'r' or i[0] == 'R':
        print(i)    

i tried various types of things and this is what works. I even tried:

if i[0] in ['r', 'R']

and it didnt work for whatever reason.

any help to print the word would be appreciated cause all I get right now is

r R r r R

first one i just get an error saying i is not defined. The second just prints out every word of the document

  • Is there supposed to be a difference between the two versions of your code (other than indentation)? Because they seem identical to me, with all the same problems. – Blckknght Sep 12 '19 at 03:11
  • I don't think this question is quite a duplicate, since there are several other issues going on, but [this previous question(and it's answers)](https://stackoverflow.com/q/15112125/1405065) are relevant to part of the problem (the `or` operator). – Blckknght Sep 12 '19 at 03:14
  • @Blckknght for the difference in code the indentation is just a formatting error made by me when i posted it on here. As far as everything else i had tried the in ['r', 'R'] and that doesnt work either. I figured out how to find the words with r but now im having trouble printing the word instead of just 'r' –  Sep 12 '19 at 03:38

1 Answers1

0

You have a whole bunch of issues going on here.

The exception about i not being defined is from for i[0] in t:, where you're trying to assign a value from your iteration to the first element of some value i which doesn't exist. You probably want just for i in t without the indexing.

The next issue is what you're iterating over. When you iterate directly on a string like t, you get each character individually. You don't get the text split up into words, which I think is what you want. Tokenizing real text is a bit tricky, but for your purposes, you may be able to get by with t.split(), which splits a string on any whitespace.

Your last issues were related to using or incorrectly, but it looks like you may have fixed them already. Anyway, here's what I'd suggest for the final code (with some other non-essential improvements, like better variable names and a with statement to close the file):

with open('file.txt', 'r') as f:
    text = f.read()

for word in text.split():
    if word[0] in 'Rr':
        print(word)
Blckknght
  • 100,903
  • 11
  • 120
  • 169
  • forgive me for I am still very new and honestly was just testing out what I have learned so far. This is much better and I honestly didnt even think to use iterations like this. So you have taught me something new which I'm grateful for, thank you for taking the time to fix this. –  Sep 14 '19 at 01:07