1
+ # -*- coding: utf-8 -*-
2
+ """Part2.ipynb
3
+
4
+ Automatically generated by Colaboratory.
5
+
6
+ Original file is located at
7
+ https://colab.research.google.com/drive/1YKH_8wOqxHQERZweOaXM5TNxScIaLDOY
8
+ """
9
+
10
+ # COMP 8347 - Internet Applications and Distributed Systems : Lab Assignment - I
11
+
12
+ # Team Details:
13
+ # 1. Abinaya Elanchezhian – 110061220
14
+ # 2. Eswaran Badrinarayanan Venkateswaran – 110069235
15
+ # 3. Lakshmi Narayanan Shankar – 110070078
16
+ # 4. Vishal Jayaraman – 110067134
17
+
18
+ from google .colab import files
19
+ uploaded = files .upload ()
20
+
21
+ #@title Question a
22
+
23
+ # opening file "inventory.txt"
24
+ f = open ("inventory.txt" , "r" )
25
+ # printing the read contents of the file
26
+ print (f .read ())
27
+
28
+ #@title Question b
29
+
30
+ # Defining 10 strings that represent a particular product
31
+ productList = {'nutella' , 'donut' , 'green tea' , 'black tea' , 'coffee' , 'black coffee' , 'white mushrooms' , 'magic mushrooms' , 'cookies' , 'tea biscuits' }
32
+ productList
33
+
34
+ #@title Question c
35
+
36
+ # Looping over each item in the productList
37
+ for x in productList :
38
+ # Opening the file in read mode
39
+ fp = open ('inventory.txt' , 'r' )
40
+ # The variable a is the exit condition or flag for the while loop
41
+ a = True
42
+ while a :
43
+ # Reading each and every string present in the line using readline() function and storing it in data
44
+ data = fp .readline ()
45
+ # Checking if item x in productList matches data (every line read) and if there is a match, the item is printed: else the loop continues
46
+ if x in data :
47
+ print (x )
48
+ # If the data reaches the end of file, then the loop terminates with the exit condition a = false
49
+ if not data :
50
+ a = False
51
+
52
+ #@title Question d
53
+
54
+ # Creating a list 'lst' for storing each matched items in the productList
55
+ lst = []
56
+ for x in productList :
57
+ fp = open ('inventory.txt' , 'r' )
58
+ a = True
59
+ while a :
60
+ data = fp .readline ()
61
+ # Checking if x in productList matches with data in inventory.txt
62
+ if x in data :
63
+ # Appending each matched item in the list 'lst'
64
+ lst .append (x )
65
+ if not data :
66
+ a = False
67
+ # Printing the list
68
+ lst
69
+
70
+ #@title Question e
71
+
72
+ # Creating a dictionary k
73
+ k = {}
74
+ for x in productList :
75
+ fp = open ('inventory.txt' , 'r' )
76
+ a = True
77
+ while a :
78
+
79
+ data = fp .readline ()
80
+ if x in data :
81
+ # Using strip() function to remove newline character ("\n") from the data variable
82
+ # Using readline () function to read the quantity value from the next line
83
+ # data is the key of datatype string in dict k
84
+ # readline() function will return the value (quantity) from inventory.txt and assigns it as a key-value pair - k{Product_key,Quantity_value}
85
+ k [str (data .strip ())]= int (fp .readline ())
86
+ if not data :
87
+ a = False
88
+ # Printing the dictionary
89
+ k
90
+
91
+ # Converting the productList as a dictionary
92
+ d1 = dict .fromkeys (productList )
93
+ d1
94
+
95
+ # Updating the dictionary d1 with the quantity value k
96
+ d1 .update (k )
97
+
98
+ d1
99
+
100
+ # Creating a dict d2 to pass a new key value pair{"magic mushrooms": "2"} to the dict d1
101
+ d2 = {}
102
+ d2 ["magic mushrooms" ]= 2
103
+ d1 .update (d2 )
104
+ d1
105
+
106
+ #@title Question f
107
+
108
+ a = True
109
+ while a :
110
+ # Getting user input (string)
111
+ s = input ()
112
+ # Checking if user input is in the dict d1
113
+ if s in d1 :
114
+ # If there is a match, then the value (quantity) of the dict d1 is printed
115
+ print (d1 [s ])
116
+ else :
117
+ print ("Not found" )
118
+ print ("want to search again?" )
119
+ # Getting user input to continue search or not
120
+ flag = input ()
121
+ if flag != 'yes' :
122
+ a = False
123
+
124
+ #@title Question g
125
+
126
+ a = True
127
+ while a :
128
+ # Getting user input (string)
129
+ s = input ()
130
+ # Checking if the exception is created using try and if one is created, the except block is executed
131
+ try :
132
+ print (d1 [s ])
133
+ except :
134
+ # Printing the exception message
135
+ print ("Not found" )
136
+ # Prompting the user to conduct a new search or not
137
+ print ("want to search again?" )
138
+ # Storing the user input into a flag variable
139
+ flag = input ()
140
+ if flag != 'yes' :
141
+ a = False
0 commit comments