Click here to Skip to main content
15,886,362 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
So I have a file named fruits.txt and inside is:
pear
apple
orange
mandarin
watermelon
pomegranate

my goal is to print out the length of each word on different lines. So the result should look like this:

4
5
6
8
10
11

what I did in the cmd line is
Python
file = open("fruits.txt", "r")
content = file.read()
file.close()
answer = len(content) for i in content


This gave me an error "SyntaxError: invalid syntax" and pointed to the "r" in "for". How would i solve this issue?

What I have tried:

<pre lang="Python">
file = open("fruits.txt", "r")
content = file.read()
file.close()
answer = len(content) for i in content
Posted
Updated 11-Oct-18 6:26am
v2

1 solution

Python
answer = len(content) for i in content

That is not a valid Python statement; see 4. More Control Flow Tools — Python 3.4.9 documentation[^].

See also section 7.2.1 at 7. Input and Output — Python 3.7.1rc1 documentation[^].
 
Share this answer
 
Comments
Kenoly Luyeye 15-Oct-18 10:41am    
So how should I change my code for it to work??
Richard MacCutchan 15-Oct-18 11:37am    
Go to the link I gave you and look at some of the sample code.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900