Created
March 16, 2020 10:21
-
-
Save mayankdawar/213eaa9d9db42a9c7be5e60402a7a140 to your computer and use it in GitHub Desktop.
Create a string called first_forty that is comprised of the first 40 characters of emotion_words2.txt.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#Create a string called first_forty that is comprised of the first 40 characters of emotion_words2.txt. | |
file = open("emotion_words2.txt","r") | |
const = file.read() | |
first_forty = const[:40] |
#Create a string called first_forty that is comprised of the first 40 characters of emotion_words2.txt.
# This solution uses a function to break from a nested loop immediately to the outside of the whole loop thing.
def first_forty_func(file_name):
first_forty = ''
count = 0
with open(file_name, 'r') as file_ref:
for line in file_ref:
line = line.strip()
for char in line:
count += 1
if count == 41:
return first_forty
first_forty += char
first_forty = first_forty_func('emotion_words2.txt')
print('first_forty', first_forty)
print('\nlen(first_forty)', len(first_forty))
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.