-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path20190304.py
227 lines (194 loc) · 7.83 KB
/
20190304.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
import datetime
import json
'''10-1. Learning Python: Open a blank file in your text editor and write a few
lines summarizing what you’ve learned about Python so far. Start each line
with the phrase In Python you can.... Save the file as learning_python.txt in the
same directory as your exercises from this chapter. Write a program that reads
the file and prints what you wrote three times. Print the contents once by read-
ing in the entire file, once by looping over the file object, and once by storing
the lines in a list and then working with them outside the with block.'''
file = "Documents/learningpython.txt"
for i in range(3):
print(open(file).read())
with open(file, 'r') as fin:
for lines in fin:
print(lines)
with open(file,'r') as fin:
my_text = fin.readlines()
cleaned_text = []
for text in my_text:
cleaned_text.append(text.rstrip("\n"))
print(cleaned_text)
'''10-2. Learning C: You can use the replace() method to replace any word in a
string with a different word. Here’s a quick example showing how to replace
'dog' with 'cat' in a sentence:
message = "I really like dogs."
message.replace('dog', 'cat')
'I really like cats.'
Read in each line from the file you just created, learning_python.txt, and
replace the word Python with the name of another language, such as C. Print
each modified line to the screen.'''
with open(file, 'r') as fin:
my_text = fin.readlines()
cleaned_text = []
for text in my_text:
cleaned_text.append(text.replace("python", "java").rstrip())
print(cleaned_text)
'''10-3. Guest: Write a program that prompts the user for their name. When they
respond, write their name to a file called guest.txt.'''
#
# name = input("What is your name, dear madam or sir?")
#
# with open("Documents/guest.txt", "w") as fin:
# fin.write(name)
'''10-4. Guest Book: Write a while loop that prompts users for their name. When
they enter their name, print a greeting to the screen and add a line recording
their visit in a file called guest_book.txt. Make sure each entry appears on a
new line in the file.'''
flag = True
while flag:
with open("Documents/guest_book.txt", "a") as fin:
name = input("What is your name, dear madam or sir? (type exit to exit)")
if name.lower() == "exit":
flag = False
else:
print(f"Greetings, {name}!")
fin.write(f'The following person logged on: {name} at {datetime.datetime.now()}\n')
'''10-5. Programming Poll: Write a while loop that asks people why they like
programming. Each time someone enters a reason, add their reason to a file
that stores all the responses.'''
# flag = True
# while flag:
#
# with open("Documents/responses.txt", "a") as fin:
# reason = input("Why do you like programming? (type exit to exit)")
# if reason.lower() == "exit":
# flag = False
# else:
# fin.write(f'Someone like programming because: {reason}\n')
'''10-6. Addition: One common problem when prompting for numerical input
occurs when people provide text instead of numbers. When you try to convert
the input to an int , you’ll get a TypeError . Write a program that prompts for
two numbers. Add them together and print the result. Catch the TypeError if
either input value is not a number, and print a friendly error message. Test your
program by entering two numbers and then by entering some text instead of a
number.'''
# try:
# num1 = int(input("Give the first number for the addition."))
# num2 = int(input("Give the second number for the addition."))
#
# except ValueError:
# print("You did not enter valid numbers. "
# "To make the addition please make sure your data are integers or floats.")
#
# else:
# print(float(num1) + float(num2))
'''10-7. Addition Calculator: Wrap your code from Exercise 10-6 in a while loop
so the user can continue entering numbers even if they make a mistake and
enter text instead of a number.'''
# flag = True
# while flag:
# try:
# num1 = int(input("Give the first number for the addition."))
# num2 = int(input("Give the second number for the addition."))
#
# except ValueError:
# print("You did not enter valid numbers. "
# "To make the addition please make sure your data are integers or floats.")
#
# else:
# print(float(num1) + float(num2))
# flag = False
'''10-8. Cats and Dogs: Make two files, cats.txt and dogs.txt. Store at least three
names of cats in the first file and three names of dogs in the second file. Write
a program that tries to read these files and print the contents of the file to the
screen. Wrap your code in a try-except block to catch the FileNotFound error,
and print a friendly message if a file is missing. Move one of the files to a dif-
ferent location on your system, and make sure the code in the except block
executes properly.'''
# file1 = "Documents/cats.txt"
# file2 = "Documents/dogs.txt"
#
# documents = []
# documents.append(file1)
# documents.append(file2)
# documents.append("Not existing file")
#
# with open(file1, 'w') as cats:
# cats.write("Nero\n")
# cats.write("Chef\n")
# cats.write("Lion\n")
#
# with open(file2, 'w') as dogs:
# dogs.write("Rufus\n")
# dogs.write("Max\n")
# dogs.write("Nes\n")
#
#
# for doc in documents:
# try:
# my_pets = []
# with open(doc, 'r') as pets:
# my_pets = pets.readlines()
#
# except FileNotFoundError:
# print("This is not a valid file.")
#
# else:
# for pet in my_pets:
# print(pet.rstrip())
'''10-10. Common Words: Visit Project Gutenberg (http://gutenberg.org/ )
and find a few texts you’d like to analyze. Download the text files for these
works, or copy the raw text from your browser into a text file on your
computer.
You can use the count() method to find out how many times a word or
phrase appears in a string. For example, the following code counts the number
of times 'row' appears in a string:
line = "Row, row, row your boat"
line.count('row')
2
line.lower().count('row')
3
Notice that converting the string to lowercase using lower() catches
all appearances of the word you’re looking for, regardless of how it’s
formatted.
Write a program that reads the files you found at Project Gutenberg and
determines how many times the word 'the' appears in each text.'''
# oz = "Documents/The Marvellous Land of Oz.txt"
#
# with open(oz, 'r') as oz:
# print(oz.read().lower().count("the"))
'''10-11. Favorite Number: Write a program that prompts for the user’s favorite
number. Use json.dump() to store this number in a file. Write a separate pro-
gram that reads in this value and prints the message, “I know your favorite
number! It’s _____.”'''
# user_num = []
#
# while True:
# user_inp = input("Tel me your favorite number. I will keep it secret, I promise. "
# "(Enter exit to exit.")
# try:
# user_num.append(int(user_inp))
#
# except ValueError:
# if user_inp.lower() == "exit":
# break
# print("That's no number! Don't try to fool me...")
#
#
#
# with open("Documents/FavNums.txt", 'w') as favnum_file:
# json.dump(user_num, favnum_file)
#
# with open("Documents/FavNums.txt", 'r') as favnum_file:
# loaded_nums = json.load(favnum_file)
#
# print(f'I know your favorite numbers. They are: {", ".join(str(num) for num in loaded_nums)}, and I will tell everyone. Muhahaha, you fool, '
# f'you shouldn\'t have trusted me...')
'''10-13. Verify User: The final listing for remember_me.py assumes either that the
user has already entered their username or that the program is running for the
first time. We should modify it in case the current user is not the person who
last used the program.
Before printing a welcome back message in greet_user() , ask the user if
this is the correct username. If it’s not, call get_new_username() to get the correct
username.'''