-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday10.py
More file actions
34 lines (29 loc) · 1.63 KB
/
day10.py
File metadata and controls
34 lines (29 loc) · 1.63 KB
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
#Today's Learning is all about excepting handling, where I learned about try except catch block and learn how one handle the exception carefully and will not stop the code from its execution
#In Today's challenge I revised some of the previous topics like taking user_input , then creating a dictionary and printing a dictionary, opening a file for read and to write it. Also use try except block to handle the error and created a dictionary
import os
try:
with open('day10.txt',mode='r') as file:
word_dict = {}
for word in file:
word.strip() #this strip method uses to remove the trailing and leading whitespaces
try:
checking_number = float(word)
print(f"{word.strip()} is a number")
if checking_number is not None:
word_dict[word.strip()] = 'number'
except ValueError:
print(f"{word.strip()} is not a number")
word_dict[word.strip()] = 'not a number'
user_input = input('Want to see the dictionary ? write yes = ')
if user_input.lower() == 'yes':
for key in word_dict:
print(f"{key}:{word_dict[key]}")
except FileNotFoundError as e:
print(f"File Not Found. So I created a file for you. Kindly add the data for checking a number or not a number")
open('day10.txt',mode='w')
with open('day10.txt',mode='a') as file:
user_input = input('What do yo want to add in the file? Kindly write it here = ')
if os.stat('day10.txt').st_size == 0:
file.write(user_input)
else:
file.write("\n" + user_input)