-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLab1_Part2.py
141 lines (117 loc) · 3.83 KB
/
Lab1_Part2.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
# -*- coding: utf-8 -*-
"""Part2.ipynb
Automatically generated by Colaboratory.
Original file is located at
https://colab.research.google.com/drive/1YKH_8wOqxHQERZweOaXM5TNxScIaLDOY
"""
# COMP 8347 - Internet Applications and Distributed Systems : Lab Assignment - I
# Team Details:
# 1. Abinaya Elanchezhian – 110061220
# 2. Eswaran Badrinarayanan Venkateswaran – 110069235
# 3. Lakshmi Narayanan Shankar – 110070078
# 4. Vishal Jayaraman – 110067134
from google.colab import files
uploaded = files.upload()
#@title Question a
# opening file "inventory.txt"
f = open("inventory.txt", "r")
# printing the read contents of the file
print(f.read())
#@title Question b
# Defining 10 strings that represent a particular product
productList = {'nutella', 'donut', 'green tea', 'black tea', 'coffee', 'black coffee', 'white mushrooms', 'magic mushrooms', 'cookies', 'tea biscuits'}
productList
#@title Question c
# Looping over each item in the productList
for x in productList:
# Opening the file in read mode
fp = open('inventory.txt', 'r')
# The variable a is the exit condition or flag for the while loop
a = True
while a:
# Reading each and every string present in the line using readline() function and storing it in data
data = fp.readline()
# 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
if x in data:
print(x)
# If the data reaches the end of file, then the loop terminates with the exit condition a = false
if not data:
a = False
#@title Question d
# Creating a list 'lst' for storing each matched items in the productList
lst=[]
for x in productList:
fp = open('inventory.txt', 'r')
a = True
while a:
data = fp.readline()
# Checking if x in productList matches with data in inventory.txt
if x in data:
# Appending each matched item in the list 'lst'
lst.append(x)
if not data:
a = False
# Printing the list
lst
#@title Question e
# Creating a dictionary k
k={}
for x in productList:
fp = open('inventory.txt', 'r')
a = True
while a:
data = fp.readline()
if x in data:
# Using strip() function to remove newline character ("\n") from the data variable
# Using readline () function to read the quantity value from the next line
# data is the key of datatype string in dict k
# readline() function will return the value (quantity) from inventory.txt and assigns it as a key-value pair - k{Product_key,Quantity_value}
k[str(data.strip())]=int(fp.readline())
if not data:
a = False
# Printing the dictionary
k
# Converting the productList as a dictionary
d1=dict.fromkeys(productList)
d1
# Updating the dictionary d1 with the quantity value k
d1.update(k)
d1
# Creating a dict d2 to pass a new key value pair{"magic mushrooms": "2"} to the dict d1
d2={}
d2["magic mushrooms"]=2
d1.update(d2)
d1
#@title Question f
a = True
while a:
# Getting user input (string)
s = input()
# Checking if user input is in the dict d1
if s in d1:
# If there is a match, then the value (quantity) of the dict d1 is printed
print(d1[s])
else:
print("Not found")
print("want to search again?")
# Getting user input to continue search or not
flag = input()
if flag != 'yes':
a = False
#@title Question g
a = True
while a:
# Getting user input (string)
s = input()
# Checking if the exception is created using try and if one is created, the except block is executed
try:
print(d1[s])
except:
# Printing the exception message
print("Not found")
# Prompting the user to conduct a new search or not
print("want to search again?")
# Storing the user input into a flag variable
flag = input()
if flag != 'yes':
a = False