Skip to content

Instantly share code, notes, and snippets.

@cibofdevs
Created July 17, 2019 04:01
Show Gist options
  • Save cibofdevs/0c2179047f50c013acb17895c2aff094 to your computer and use it in GitHub Desktop.
Save cibofdevs/0c2179047f50c013acb17895c2aff094 to your computer and use it in GitHub Desktop.
# 1. The textfile, travel_plans.txt, contains the summer travel plans for someone with some commentary.
# Find the total number of characters in the file and save to the variable num.
fileref = open("travel_plans.txt","r")
num = 0
for i in fileref:
num += len(i)
fileref.close()
# 2. We have provided a file called emotion_words.txt that contains lines of words that describe emotions.
# Find the total number of words in the file and assign this value to the variable num_words.
num_words = 0
fileref = "emotion_words.txt"
with open(fileref, 'r') as file:
for line in file:
num_words += len(line.split())
print("number of words : ", num_words)
# 3. Assign to the variable num_lines the number of lines in the file school_prompt.txt.
num_lines = sum(1 for line in open('school_prompt.txt'))
# 4. Assign the first 30 characters of school_prompt.txt as a string to the variable beginning_chars.
f = open('school_prompt.txt', 'r')
beginning_chars = f.read(30)
print(beginning_chars)
# 5. Challenge: Using the file school_prompt.txt, assign the third word of every line to a list called three.
three = []
with open('school_prompt.txt', 'r') as f:
three = [line.split()[2] for line in f]
print(three)
# 6. Challenge: Create a list called emotions that contains the first word of every line in emotion_words.txt.
fileref = open ("emotion_words.txt","r")
line = fileref.readlines()
emotions = []
for words in line:
word = words.split()
emotions.append(word[0])
print (emotions)
# 7. Assign the first 33 characters from the textfile, travel_plans.txt to the variable first_chars.
f = open('travel_plans.txt', 'r')
first_chars = f.read(33)
print(first_chars)
# 8. Challenge: Using the file school_prompt.txt, if the character ‘p’ is in a word, then add the word to a list called p_words.
fileref = open('school_prompt.txt', 'r')
words = fileref.read().split()
p_words = [word for word in words if 'p' in word]
@MrJunjfc
Copy link

Could someone explain line 60 of the code to me, thanks..

@Aaron1810
Copy link

p_words = []
with open('school_prompt.txt','r') as sp:
sp_1 = sp.read().split()
for word in sp_1:
if 'p' in word:
p_words.append(word)
print(p_words)
print("There are {} words that have p.".format(len(p_words)))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment