diff --git a/.idea/.gitignore b/.idea/.gitignore
new file mode 100644
index 0000000..26d3352
--- /dev/null
+++ b/.idea/.gitignore
@@ -0,0 +1,3 @@
+# Default ignored files
+/shelf/
+/workspace.xml
diff --git a/.idea/BCS-2021.iml b/.idea/BCS-2021.iml
new file mode 100644
index 0000000..d0876a7
--- /dev/null
+++ b/.idea/BCS-2021.iml
@@ -0,0 +1,8 @@
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/.idea/inspectionProfiles/profiles_settings.xml b/.idea/inspectionProfiles/profiles_settings.xml
new file mode 100644
index 0000000..105ce2d
--- /dev/null
+++ b/.idea/inspectionProfiles/profiles_settings.xml
@@ -0,0 +1,6 @@
+
+
+
+
+
+
\ No newline at end of file
diff --git a/.idea/misc.xml b/.idea/misc.xml
new file mode 100644
index 0000000..d56657a
--- /dev/null
+++ b/.idea/misc.xml
@@ -0,0 +1,4 @@
+
+
+
+
\ No newline at end of file
diff --git a/.idea/modules.xml b/.idea/modules.xml
new file mode 100644
index 0000000..be3317f
--- /dev/null
+++ b/.idea/modules.xml
@@ -0,0 +1,8 @@
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/.idea/vcs.xml b/.idea/vcs.xml
new file mode 100644
index 0000000..94a25f7
--- /dev/null
+++ b/.idea/vcs.xml
@@ -0,0 +1,6 @@
+
+
+
+
+
+
\ No newline at end of file
diff --git a/README.md b/README.md
index ea6acf2..5a8645e 100644
--- a/README.md
+++ b/README.md
@@ -1,2 +1,9 @@
# BCS-2021
This repository is created to collect the assignment solutions for the subject CSC1102 Principles of Programming for the students of Bachelors of Computer Science Year I, in the faculty of Computing and Informatics, MUST.
+## Group 6 Members
+
+1. AKANYIJUKA GASTON 2020/BCS/066/PS
+2. KATUSHABE MOREEN 2020/BCS/034/PS
+3. NANZIRI CHARLOTTE HELGA 2020/BCS/090/PS
+4. OMARE DANIEL 2020/BCS/057/PS
+5. BEILEL AUGUSTINE 2019/BCS/022/PS
\ No newline at end of file
diff --git a/src/.idea/.gitignore b/src/.idea/.gitignore
new file mode 100644
index 0000000..26d3352
--- /dev/null
+++ b/src/.idea/.gitignore
@@ -0,0 +1,3 @@
+# Default ignored files
+/shelf/
+/workspace.xml
diff --git a/src/.idea/inspectionProfiles/profiles_settings.xml b/src/.idea/inspectionProfiles/profiles_settings.xml
new file mode 100644
index 0000000..105ce2d
--- /dev/null
+++ b/src/.idea/inspectionProfiles/profiles_settings.xml
@@ -0,0 +1,6 @@
+
+
+
+
+
+
\ No newline at end of file
diff --git a/src/.idea/misc.xml b/src/.idea/misc.xml
new file mode 100644
index 0000000..d56657a
--- /dev/null
+++ b/src/.idea/misc.xml
@@ -0,0 +1,4 @@
+
+
+
+
\ No newline at end of file
diff --git a/src/.idea/modules.xml b/src/.idea/modules.xml
new file mode 100644
index 0000000..f669a0e
--- /dev/null
+++ b/src/.idea/modules.xml
@@ -0,0 +1,8 @@
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/src/.idea/src.iml b/src/.idea/src.iml
new file mode 100644
index 0000000..d0876a7
--- /dev/null
+++ b/src/.idea/src.iml
@@ -0,0 +1,8 @@
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/src/.idea/vcs.xml b/src/.idea/vcs.xml
new file mode 100644
index 0000000..6c0b863
--- /dev/null
+++ b/src/.idea/vcs.xml
@@ -0,0 +1,6 @@
+
+
+
+
+
+
\ No newline at end of file
diff --git a/src/chapter2/exercise2.py b/src/chapter2/exercise2.py
new file mode 100644
index 0000000..9236220
--- /dev/null
+++ b/src/chapter2/exercise2.py
@@ -0,0 +1,2 @@
+name = input("what is your name?\n")
+print("YOUR WELCOME", name)
diff --git a/src/chapter2/exercise3.py b/src/chapter2/exercise3.py
new file mode 100644
index 0000000..4bf7491
--- /dev/null
+++ b/src/chapter2/exercise3.py
@@ -0,0 +1,4 @@
+Hours = float(input("Enter Hours"))
+Rate = float(input("Enter Rate"))
+Gross_Pay = Hours * Rate
+print(Gross_Pay)
diff --git a/src/chapter2/exercise4.png b/src/chapter2/exercise4.png
new file mode 100644
index 0000000..90bdd52
Binary files /dev/null and b/src/chapter2/exercise4.png differ
diff --git a/src/chapter2/exercise5.py b/src/chapter2/exercise5.py
new file mode 100644
index 0000000..5ee3d8e
--- /dev/null
+++ b/src/chapter2/exercise5.py
@@ -0,0 +1,3 @@
+x = float(input("Enter the temperature in degrees C :"))
+y = (((x * 9) / 5) + 32) # y is the temperature in degrees Fahrenheit
+print(y, "°F")
diff --git a/src/chapter2/exercise6.py b/src/chapter2/exercise6.py
new file mode 100644
index 0000000..e3e4145
--- /dev/null
+++ b/src/chapter2/exercise6.py
@@ -0,0 +1,6 @@
+x1 = float(input("enter x1 :"))
+x2 = float(input("enter x2 :"))
+y1 = float(input("enter y1 :"))
+y2 = float(input("enter x1 :"))
+d = ((((x2 - x1) ** 2) + ((y2 - y1) ** 2)) ** 0.5)
+print(d)
diff --git a/src/chapter2/exercise7.py b/src/chapter2/exercise7.py
new file mode 100644
index 0000000..d79fcf1
--- /dev/null
+++ b/src/chapter2/exercise7.py
@@ -0,0 +1,16 @@
+c = float(input("Enter Amount Between 0-99 :"))
+print(c // 20, "Twenties")
+c = c % 20
+print(c // 10, "Tens")
+c = c % 10
+print(c // 5, "Fives")
+c = c % 5
+print(c // 1, "Ones")
+c = c % 1
+print(c // 0.25, "Quarters")
+c = c % 0.25
+print(c // 0.1, "Dimes")
+c = c % 0.1
+print(c // 0.05, "Nickles")
+c = c % 0.05
+print(c // 0.01, "Pennies")
diff --git a/src/chapter2/exercise8.py b/src/chapter2/exercise8.py
new file mode 100644
index 0000000..09ad22f
--- /dev/null
+++ b/src/chapter2/exercise8.py
@@ -0,0 +1,6 @@
+t = float(input("Enter t"))
+C = float(input("Enter C"))
+r = float(input("Enter r"))
+n = float(input("Enter n"))
+p = round(C * (1 + (r / n)) ** (t * n), 2) # The value of "p" already is rounded off to 2 dp using the round function
+print(p)
diff --git a/src/chapter3/.idea/.gitignore b/src/chapter3/.idea/.gitignore
new file mode 100644
index 0000000..26d3352
--- /dev/null
+++ b/src/chapter3/.idea/.gitignore
@@ -0,0 +1,3 @@
+# Default ignored files
+/shelf/
+/workspace.xml
diff --git a/src/chapter3/.idea/chapter3.iml b/src/chapter3/.idea/chapter3.iml
new file mode 100644
index 0000000..d0876a7
--- /dev/null
+++ b/src/chapter3/.idea/chapter3.iml
@@ -0,0 +1,8 @@
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/src/chapter3/.idea/inspectionProfiles/profiles_settings.xml b/src/chapter3/.idea/inspectionProfiles/profiles_settings.xml
new file mode 100644
index 0000000..105ce2d
--- /dev/null
+++ b/src/chapter3/.idea/inspectionProfiles/profiles_settings.xml
@@ -0,0 +1,6 @@
+
+
+
+
+
+
\ No newline at end of file
diff --git a/src/chapter3/.idea/misc.xml b/src/chapter3/.idea/misc.xml
new file mode 100644
index 0000000..d56657a
--- /dev/null
+++ b/src/chapter3/.idea/misc.xml
@@ -0,0 +1,4 @@
+
+
+
+
\ No newline at end of file
diff --git a/src/chapter3/.idea/modules.xml b/src/chapter3/.idea/modules.xml
new file mode 100644
index 0000000..c47dba7
--- /dev/null
+++ b/src/chapter3/.idea/modules.xml
@@ -0,0 +1,8 @@
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/src/chapter3/.idea/vcs.xml b/src/chapter3/.idea/vcs.xml
new file mode 100644
index 0000000..b2bdec2
--- /dev/null
+++ b/src/chapter3/.idea/vcs.xml
@@ -0,0 +1,6 @@
+
+
+
+
+
+
\ No newline at end of file
diff --git a/src/chapter3/excercise1.py b/src/chapter3/excercise1.py
new file mode 100644
index 0000000..56afc33
--- /dev/null
+++ b/src/chapter3/excercise1.py
@@ -0,0 +1,10 @@
+x = float(input("enters hours"))
+y = float(input("enters rate"))
+z = (x * y) # this is the pay for hours of work below or equal to 40
+q = y * 40
+m = (1.5 * (x - 40) * y) # m is the extra hours worked beyond the 40 hours .
+r = (q + m) # this is the pay for hours of work above 40
+if x <= 40:
+ print(z)
+elif x > 40:
+ print(r)
diff --git a/src/chapter3/excercise2.py b/src/chapter3/excercise2.py
new file mode 100644
index 0000000..5110f51
--- /dev/null
+++ b/src/chapter3/excercise2.py
@@ -0,0 +1,13 @@
+try:
+ x = float(input("enters hours"))
+ y = float(input("enters rate"))
+ z = (x * y) # this is the pay for hours of work below or equal to 40
+ q = y * 40
+ m = (1.5 * (x - 40) * y) # m is the extra hours worked beyond the 40 hours .
+ r = (q + m) # this is the pay for hours of work above 40
+ if x <= 40:
+ print(z)
+ elif x > 40:
+ print(r)
+except:
+ print("ERROR, PLEASE ENTER NUMERIC INPUT")
diff --git a/src/chapter3/excercise3.py b/src/chapter3/excercise3.py
new file mode 100644
index 0000000..6ede7ca
--- /dev/null
+++ b/src/chapter3/excercise3.py
@@ -0,0 +1,18 @@
+try:
+ score = float(input('enter score'))
+ # A program that prompts a user to enter a score from 0 to 1 and prints the corresponding grade
+ if 0 <= score < 0.6:
+ print(score, 'F')
+ elif 0.6 <= score < 0.7:
+ print(score, 'D')
+ elif 0.7 <= score < 0.8:
+ print(score, 'C')
+ elif 0.8 <= score < 0.9:
+ print(score, 'B')
+ elif 0.9 <= score <= 1:
+ print(score, 'A')
+ else:
+ print('error, out of range')
+
+except:
+ print("INVALID ENTRY")
diff --git a/src/chapter3/excercise4.py b/src/chapter3/excercise4.py
new file mode 100644
index 0000000..79e3849
--- /dev/null
+++ b/src/chapter3/excercise4.py
@@ -0,0 +1,10 @@
+try:
+ age = int(input('enter age'))
+ if age >= 18:
+ print('you can vote') # prints out only people who can vote
+ elif 0 <= age <= 17:
+ print('too young to vote')
+ elif age < 0:
+ print('you are a time traveller')
+except:
+ print('error, please enter a digit')
diff --git a/src/chapter3/excercise5.py b/src/chapter3/excercise5.py
new file mode 100644
index 0000000..44face0
--- /dev/null
+++ b/src/chapter3/excercise5.py
@@ -0,0 +1,15 @@
+""""A PROGRAM THAT PROMPTS THE USER FOR
+ THE NUMBER OF PEOPLE ATTENDING THEIR WEDDING AND PRINTS THE CORRESPONDING PRICE IN THE CONSOLE"""""
+try:
+ a = int(input('Number of guests'))
+ if 0 < a <= 50:
+ print('price =$4000')
+ elif 50 < a <= 100:
+ print('price = $10000')
+ elif 100 < a <= 200:
+ print('price = $15000')
+ elif a > 200:
+ print('price = $20000')
+except:
+ print('error, please enter a digit')
+
diff --git a/src/chapter3/exercise6.jpeg.jpg b/src/chapter3/exercise6.jpeg.jpg
new file mode 100644
index 0000000..1dbaca0
Binary files /dev/null and b/src/chapter3/exercise6.jpeg.jpg differ
diff --git a/src/chapter3/exercise7.py b/src/chapter3/exercise7.py
new file mode 100644
index 0000000..b0104b1
--- /dev/null
+++ b/src/chapter3/exercise7.py
@@ -0,0 +1,19 @@
+try:
+ g = input('enter job location')
+ b = int(input('enter pay'))
+ if g.upper() == 'MBARARA' and b <= 4000000: # (. upper) converts all the locations into uppercase
+ print('No Thanks, I can find something better')
+ elif g.upper() == 'MBARARA' and b > 4000000:
+ print('I can work with this')
+ elif g.upper() == 'KAMPALA' and b >= 10000000:
+ print('I can work with this')
+ elif g.upper() == 'KAMPALA' and b < 10000000:
+ print('No Way!')
+ elif g.upper() == 'SPACE' and b >= 0:
+ print('Without doubt, I will work')
+ elif b >= 6000000: # g==other districts and b>=6000000
+ print('I will surely work')
+ else:
+ print('No thanks, I can find something better')
+except:
+ print('invalid entry')
diff --git a/src/chapter4/exercise1.pdf b/src/chapter4/exercise1.pdf
new file mode 100644
index 0000000..fe1e25e
Binary files /dev/null and b/src/chapter4/exercise1.pdf differ
diff --git a/src/chapter4/exercise2.py b/src/chapter4/exercise2.py
new file mode 100644
index 0000000..0e07ff9
--- /dev/null
+++ b/src/chapter4/exercise2.py
@@ -0,0 +1,20 @@
+c = (input('ENTER INITIAL AMOUNT OF INVESTMENT >>'))
+n = (input('ENTER NUMBER OF YEARS THE INTEREST IS COMPOUNDED PER YEAR>>'))
+r = (input('ENTER YEARLY RATE OF INTEREST>> '))
+t = (input('ENTER NUMBER OF YEARS UNTIL MATURATION>>'))
+
+
+def investment(w, x, y, z):
+ try:
+ w = float(c)
+ x = float(n)
+ y = float(r)
+ z = float(t)
+ p = round(w * (1 + (y / x)) ** (z * x), 2) # p is rounded to 2 dp using the round function
+ return p
+ except ValueError:
+ return "INVALID ENTRY"
+
+
+p = investment(c, r, n, t)
+print(p)
diff --git a/src/chapter4/exercise3.pdf b/src/chapter4/exercise3.pdf
new file mode 100644
index 0000000..1ade818
Binary files /dev/null and b/src/chapter4/exercise3.pdf differ
diff --git a/src/chapter4/exercise4.pdf b/src/chapter4/exercise4.pdf
new file mode 100644
index 0000000..b33086a
Binary files /dev/null and b/src/chapter4/exercise4.pdf differ
diff --git a/src/chapter4/exercise6.py b/src/chapter4/exercise6.py
new file mode 100644
index 0000000..1918493
--- /dev/null
+++ b/src/chapter4/exercise6.py
@@ -0,0 +1,21 @@
+x = (input("enters hours"))
+y = (input("enters rate"))
+
+
+def compute_pay(hours, rate):
+ """The try block ensures that the user enters a
+ value between from 0-1 otherwise an error message pops up"""
+ try:
+ hours = float(x)
+ rate = float(y)
+ if hours <= 40:
+ pay= float(hours * rate)
+ else:
+ pay = float(40 * rate + (hours - 40) * 1.5 * rate)
+ return pay
+ except ValueError:
+ return "INVALID ENTRY"
+
+
+pay = compute_pay(x, y)
+print(pay)
diff --git a/src/chapter4/exercise7.py b/src/chapter4/exercise7.py
new file mode 100644
index 0000000..4099e67
--- /dev/null
+++ b/src/chapter4/exercise7.py
@@ -0,0 +1,27 @@
+# A program that prompts a user to enter a score from 0 to 1 and prints the corresponding grade
+s = (input('enter score'))
+
+
+def compute_grade(score):
+ # this makes sure that the user enters only float values otherwise an error message pops up
+ try:
+ score = float(s)
+ if 0 <= score < 0.6:
+ print(score, 'F')
+ elif 0.6 <= score < 0.7:
+ print(score, 'D')
+ elif 0.7 <= score < 0.8:
+ print(score, 'C')
+ elif 0.8 <= score < 0.9:
+ print(score, 'B')
+ elif 0.9 <= score <= 1:
+ print(score, 'A')
+ else:
+ print('error, out of range')
+ return "grade"
+ except ValueError:
+ return "INVALID ENTRY"
+
+
+grade = compute_grade(s)
+print(grade)
diff --git a/src/chapter5/excercise1.py b/src/chapter5/excercise1.py
new file mode 100644
index 0000000..ee2605d
--- /dev/null
+++ b/src/chapter5/excercise1.py
@@ -0,0 +1,18 @@
+try:
+ total = 0
+ count = 0
+# the user will be able to input numbers repeatedly
+ while True:
+ x = input('enter a number')
+# when done is entered, the loop will be broken
+ if x == 'done':
+ break
+ x = int(x)
+# we are finding the total, count and average of the numbers entered
+ total = total + x
+ count = count + 1
+ average = total / count
+ print('done!')
+ print(count, total, average)
+except:
+ print('Invalid Input')
\ No newline at end of file
diff --git a/src/chapter5/exercise1.py b/src/chapter5/exercise1.py
new file mode 100644
index 0000000..5f38091
--- /dev/null
+++ b/src/chapter5/exercise1.py
@@ -0,0 +1,16 @@
+total = 0
+count = 0
+average = 0
+
+while True:
+ try:
+ inp = input("Enter a number: ")
+ if inp == "done":
+ break
+ value = float(inp)
+ total = value + total
+ count = count + 1
+ average = total / count
+ except ValueError:
+ print("Invalid input.")
+print(total, count, average)
diff --git a/src/chapter5/exercise2.py b/src/chapter5/exercise2.py
new file mode 100644
index 0000000..09eb3b3
--- /dev/null
+++ b/src/chapter5/exercise2.py
@@ -0,0 +1,32 @@
+total = 0
+count = 0
+average = 0
+smallest = None
+largest = None
+print('before largest:', largest)
+while True:
+ inp = input('>')
+ if inp == "done":
+ break
+ try:
+ if float(inp):
+ total += float(inp)
+ count += 1
+ new_value = float(inp)
+ if largest is None or new_value > largest:
+ largest = new_value
+ print('new_largest is', largest)
+ if smallest is None or new_value < smallest:
+ smallest = new_value
+ print('new_smallest is', smallest)
+ except ValueError:
+ print('invalid input')
+ if count != 0:
+ print('done')
+ print('largest is', largest)
+ print('smallest is', smallest)
+ print('count is', count)
+ print(total)
+ print('average=', total / count)
+ else:
+ print('enter a number please')
diff --git a/src/chapter5/exercise3.py b/src/chapter5/exercise3.py
new file mode 100644
index 0000000..bef49d0
--- /dev/null
+++ b/src/chapter5/exercise3.py
@@ -0,0 +1,109 @@
+# the first 5 lines indicate the initial number of stock in the machine
+nickels = 25
+dimes = 25
+quarters = 25
+ones = 0
+fives = 0
+
+x = ('Menu for deposits :\nq-deposit a quarters\nd-deposit a dimes\n'
+ 'n-deposit a nickels\no-deposit a one dollar bill \nf-deposit a five dollar bill\n'
+ .format(quarters, dimes, nickels, ones, fives))
+
+n = "nickel"
+d = "dimes"
+q = "quarter"
+o = "ones"
+f = "fives"
+print(" >>>> =========================" "RESTART" "=========================\n >>>>")
+print("welcome to the vending machine change make program")
+print("change maker initialised. ")
+print('stock contains :\n{} quarters\n{} dimes\n{} nickels\n{} ones \n{} fives\n'
+ .format(quarters, dimes, nickels, ones, fives))
+
+while True:
+ int_str = input("\nPlease enter purchase price (XX.XX) or 'q' to quit:")
+ if int_str == 'q':
+ break # this terminates the program the moment q is entered
+ price = float(int_str)
+ if price > 0 or (price % 5 * 100) != 0: # this makes sure that the purchase price is is a non negative value or a
+ # multiple of 5
+ print(x)
+ break
+ else:
+ print('Illegal price: must be a non negative multiple of five cents')
+
+while int_str != 'q':
+ while True:
+ dollar = float(input('\nenter the number of dollar for payment:'))
+ if dollar > price:
+ price = price * 100
+ dollar *= 100
+ print(x)
+ break
+ else:
+ print('insufficient amount')
+
+ noOfQuarters = 0
+ noOfDimes = 0
+ noOfNickels = 0
+ noOfOnes = 0
+ noOfFives = 0
+ change = dollar - price
+ print('Please take the change below \n', (change / 100))
+
+ noOfQuarters = int(change / 25)
+ if noOfQuarters != 0:
+ if noOfQuarters <= quarters:
+ quarters -= noOfQuarters
+ change -= noOfQuarters * 25
+ else:
+ noOfQuarters = quarters
+ quarters = 0
+ change -= noOfQuarters * 25
+
+ noOfDimes = int(change / 10)
+ if noOfDimes != 0:
+ if noOfDimes <= dimes:
+ dimes -= noOfDimes
+ change -= noOfDimes * 10
+ else:
+ noOfDimes = dimes
+
+ dimes = 0
+ change -= noOfDimes * 10
+
+ noOfNickels = int(change / 5)
+ if noOfNickels != 0:
+ if noOfNickels <= fives:
+ fives -= noOfNickels
+ change -= noOfNickels * 5
+ else:
+ noOfNickels = fives
+ fives = 0
+ change -= noOfNickels * 5
+
+ totalCoin = noOfDimes + noOfQuarters + noOfNickels + noOfOnes + noOfFives
+ print('\n Least number of coin needed to change is {}'.format(int(totalCoin)))
+ if noOfQuarters != 0:
+ print('{} quarters'.format(noOfQuarters))
+ if noOfDimes != 0:
+ print('{} dimes'.format(noOfDimes))
+ if noOfNickels != 0:
+ print('{} nickels'.format(noOfNickels))
+ if noOfOnes != 0:
+ print('{} ones'.format(noOfOnes))
+ if noOfFives != 0:
+ print('{} fives'.format(noOfFives))
+
+ print('\nStock contains : {}quarters\n{}dimes\n {}nickels\n {}ones\n and {}fives'
+ .format(quarters, dimes, nickels, ones, fives))
+ while True:
+ int_str = input('\n\n\nPlease enter the price in for of XX.XX or q to quit:')
+ if int_str == 'q':
+ break
+ price = float(int_str)
+ if price > 0 or (price % 5 * 100) != 0: # this makes sure that the purchase price is is a non negative value
+ # or a multiple of 5
+ break
+ else:
+ print('Illegal price: must be a non negative multiple of five cents')
diff --git a/src/chapter6/exercise1.py b/src/chapter6/exercise1.py
new file mode 100644
index 0000000..ebd6aaa
--- /dev/null
+++ b/src/chapter6/exercise1.py
@@ -0,0 +1,5 @@
+word = input(">>>")
+index = len(word) - 1
+while index >= 0:
+ print(word[index])
+ index -= 1
diff --git a/src/chapter6/exercise3.py b/src/chapter6/exercise3.py
new file mode 100644
index 0000000..5d515e2
--- /dev/null
+++ b/src/chapter6/exercise3.py
@@ -0,0 +1,13 @@
+word = input('Enter a word')
+letter = input('Enter a letter')
+
+
+def count(word, letter):
+ counter = 0
+ for character in word:
+ if character == letter:
+ counter = counter + 1
+ print(counter)
+
+
+count(word, letter)
diff --git a/src/chapter6/exercise4.py b/src/chapter6/exercise4.py
new file mode 100644
index 0000000..8e98d61
--- /dev/null
+++ b/src/chapter6/exercise4.py
@@ -0,0 +1,5 @@
+string = 'banana'
+count = 0
+for char in string:
+ count += 1
+print('a>>', string.count('a', 1))
diff --git a/src/chapter6/exercise5.py b/src/chapter6/exercise5.py
new file mode 100644
index 0000000..6165b6b
--- /dev/null
+++ b/src/chapter6/exercise5.py
@@ -0,0 +1,9 @@
+string = 'X-DSPAM-Confidence:0.8475' # we changed 'str' to 'string' since str being a built in word, it was
+# it was causing a warning
+colon = string.find(':')
+extract = string[colon + 1:]
+# print(type(extract))
+print(extract)
+f = float(extract)
+# print(type(f))
+print('FLOAT VALUE =', f)
diff --git a/src/chapter7/exercise1.py b/src/chapter7/exercise1.py
new file mode 100644
index 0000000..08f886d
--- /dev/null
+++ b/src/chapter7/exercise1.py
@@ -0,0 +1,7 @@
+# A program to read through a file and print the contents of the file
+# (line by line) all in upper case
+fname = input('Enter a file name')
+fhand = open(fname)
+for line in fhand:
+ line = line.rstrip()
+ print(line.upper())
diff --git a/src/chapter7/exercise2.py b/src/chapter7/exercise2.py
new file mode 100644
index 0000000..4bd0445
--- /dev/null
+++ b/src/chapter7/exercise2.py
@@ -0,0 +1,18 @@
+# Write a program to prompt for a file name, and then read through the
+# file and look for lines of the form:
+# X-DSPAM-Confidence: 0.8475
+file_name = input('Enter the file name>>>> ') # user is prompted to enter the file name
+try:
+ file_handle = open('file_name')
+except FileNotFoundError:
+ print('File cannot be opened>>>>', file_name)
+ exit()
+count = 0
+confidencesum = 0
+for line in 'file_handle':
+ if line.startswith('X-DSPAM-Confidence:'):
+ count += 1
+ confidence = float(line[20:26])
+ confidencesum = confidence+1
+averageconfidence = confidencesum / count
+print("Average spam confidence>>>> ", str(averageconfidence))
diff --git a/src/chapter7/exercise3.py b/src/chapter7/exercise3.py
new file mode 100644
index 0000000..0ddd088
--- /dev/null
+++ b/src/chapter7/exercise3.py
@@ -0,0 +1,15 @@
+fname = input('Enter a file name: ')
+try:
+ fhand = open('fname')
+except FileNotFoundError:
+ if fname == 'na na boo boo':
+ print("NA NA BOO BOO TO YOU - You have been punk'd!")
+ exit()
+ else:
+ print('File can not be opened: ', fname)
+ exit()
+count = 0
+for line in 'fhand':
+ if line.startswith('Subject:'):
+ count = count + 1
+ print('There are', count, 'subject lines in', fname)
diff --git a/src/chapter8/exercise1.py b/src/chapter8/exercise1.py
new file mode 100644
index 0000000..f41aa09
--- /dev/null
+++ b/src/chapter8/exercise1.py
@@ -0,0 +1,23 @@
+
+def chop(lst):
+ del lst[0] # removes 1st element in the list
+ del lst[-1] # removes last element in the list
+ return None
+
+
+def middle(lst):
+ new = lst[1:]
+ del new[-1]
+ return new
+
+
+list1 = [1, 2, 3, 4]
+list2 = [1, 2, 3, 4]
+
+chop_list = chop(list1)
+print(list1)
+print(chop_list)
+
+middle_list = middle(list2)
+print(list2)
+print(middle_list)
diff --git a/src/chapter8/exercise2.py b/src/chapter8/exercise2.py
new file mode 100644
index 0000000..b04c717
--- /dev/null
+++ b/src/chapter8/exercise2.py
@@ -0,0 +1,11 @@
+new_file = input('Enter file to open>>')
+fhand = open('new_file', 'r')
+count = 0
+for line in fhand:
+ words = line.split()
+ print ('Debug:', words)
+ if len(words) == 0 or len(words) < 3:
+ continue
+ if words[0] != 'From':
+ continue
+ print(words[2])
diff --git a/src/chapter8/exercise3.py b/src/chapter8/exercise3.py
new file mode 100644
index 0000000..20b5c93
--- /dev/null
+++ b/src/chapter8/exercise3.py
@@ -0,0 +1,8 @@
+
+fhand = open('mbox-short.txt')
+count = 0
+for line in fhand:
+ words = line.split()
+ # print 'Debug:', words
+ if (len(words) > 0) and (words[0] == 'From'):
+ print(words[2])
diff --git a/src/chapter8/exercise4.py b/src/chapter8/exercise4.py
new file mode 100644
index 0000000..1fb4364
--- /dev/null
+++ b/src/chapter8/exercise4.py
@@ -0,0 +1,11 @@
+fhand = open('romeo.txt')
+lst = list()
+for line in fhand: # to read every line of file romeo.txt
+ word = line.rstrip().split() # to eliminate the unwanted blanks and turn the line into a list of words
+ for element in word:
+ if element in lst: # if element is repeated
+ continue
+ else:
+ lst.append(element)
+lst.sort() # this sorts the words when the loop ends.
+print(lst)
diff --git a/src/chapter8/exercise5.py b/src/chapter8/exercise5.py
new file mode 100644
index 0000000..f1a51f4
--- /dev/null
+++ b/src/chapter8/exercise5.py
@@ -0,0 +1,13 @@
+try:
+ fname = input('please enter file name >>>')
+ fhand = open(fname) # make the input file readable.
+ count = 0
+ for line in fhand:
+ words = line.split() # this splits the line into words
+ if len(words) < 3 or words[0] != 'From':
+ continue
+ print(words[1])
+ count += 1
+ print('There were %d lines in the file with From as the first word' % count)
+except FileNotFoundError:
+ print('file not found')
diff --git a/src/chapter8/exercise6.py b/src/chapter8/exercise6.py
new file mode 100644
index 0000000..2e606d5
--- /dev/null
+++ b/src/chapter8/exercise6.py
@@ -0,0 +1,18 @@
+num_list = []
+while True:
+ input_number = (input('Enter a number>>> ')).lower() # THE INPUT IS AUTOMATICALLY CONVERTED INTO LOWERCASE
+ if input_number == 'done':
+ break
+
+ try:
+ number = float(input_number)
+ except ValueError:
+ print('INVALID ENTRY')
+ continue
+
+ num_list.append(number) # add each number input to the list that was created above
+
+if num_list:
+ print(num_list)
+ print('Maximum: ', max(num_list) or None)
+ print('Minimum: ', min(num_list) or None)
diff --git a/src/mid term/project.py b/src/mid term/project.py
new file mode 100644
index 0000000..58a6002
--- /dev/null
+++ b/src/mid term/project.py
@@ -0,0 +1,79 @@
+# A PROGRAM TO COMPUTE AND DISPLAY FOR A UTILITY COMPANY WHICH SUPPLIES WATER TO ITS CUSTOMERS #
+print(">>> ====================================== RESTART ==================================================== >>>")
+
+
+# calculating the number of gallons used by the customer
+def meter_reading(a, b):
+ if a < b:
+ gallons = (b - a) / 10
+ else:
+ gallons = ((1000000000 - a) + b) / 10
+ print(f'Gallons of water used:{gallons}')
+ return gallons
+
+
+# prompting the user for inputs
+while True:
+ code = input("Enter customer code:\n")
+ if code == "R" or code == "r":
+ initial_meter = int(input('Enter beginning meter reading:\n'))
+ if initial_meter < 0 or initial_meter >= 1000000000:
+ print("invalid entry, please enter a valid meter number")
+ break
+ else:
+ final_meter = int(input('Enter ending meter reading:\n'))
+ if final_meter < 0 or final_meter >= 1000000000:
+ print(" invalid entry , please enter a valid meter number")
+ print(f'Customer code:{code}')
+ print(f'initial meter number:{initial_meter:9d}')
+ print(f'final meter reading:{final_meter:9d}')
+ gallons = meter_reading(initial_meter, final_meter)
+
+ # calculating the pay bill for residential customers
+ pay_bill = 5.00 + (gallons * 0.0005)
+ print(f'Amount billed: ${pay_bill :.02f}')
+
+ # calculating the pay bill for commercial customers
+ elif code == "c" or code == "C":
+ initial_meter = int(input('Enter beginning meter reading:\n'))
+ if initial_meter < 0 or initial_meter >= 1000000000:
+ print("invalid entry, please enter a valid meter number")
+ break
+ else:
+ final_meter = int(input('Enter ending meter reading:\n'))
+ if final_meter < 0 or final_meter >= 1000000000:
+ print(" invalid entry , please enter a valid meter number")
+ print(f'Customer code:{code}')
+ print(f'initial meter number:{initial_meter:9d}')
+ print(f'final meter reading:{final_meter:9d}')
+ gallons = meter_reading(initial_meter, final_meter)
+ if gallons <= 4000000:
+ pay_bill = 1000
+ else:
+ pay_bill = 1000 + (gallons - 4000000) * 0.00025
+ print(f' pay_bill: ${pay_bill: .02f}')
+
+ # calculating the pay bill for industrial customers
+ elif code == "i" or code == "I":
+ initial_meter = int(input('Enter beginning meter reading:\n'))
+ if initial_meter < 0 or initial_meter >= 1000000000:
+ print("invalid entry, please enter a valid meter number")
+ break
+ else:
+ final_meter = int(input('Enter ending meter reading:\n'))
+ if final_meter < 0 or final_meter >= 1000000000:
+ print(" invalid entry , please enter a valid meter number")
+ print(f'Customer code:{code}')
+ print(f'initial meter number:{initial_meter:9d}')
+ print(f'final meter reading:{final_meter:9d}')
+ gallons = meter_reading(initial_meter, final_meter)
+ if gallons <= 4000000:
+ pay_bill = 1000.00
+ elif 4000000 < gallons <= 10000000:
+ pay_bill = 2000.00
+ else:
+ pay_bill = 2000.00 + (gallons - 10000000) * 0.00025
+ print(f' pay_bill: ${pay_bill: .02f}')
+ else:
+ print(" Invalid code")
+ break
diff --git a/src/project2/.idea/.gitignore b/src/project2/.idea/.gitignore
new file mode 100644
index 0000000..26d3352
--- /dev/null
+++ b/src/project2/.idea/.gitignore
@@ -0,0 +1,3 @@
+# Default ignored files
+/shelf/
+/workspace.xml
diff --git a/src/project2/.idea/.name b/src/project2/.idea/.name
new file mode 100644
index 0000000..68b7185
--- /dev/null
+++ b/src/project2/.idea/.name
@@ -0,0 +1 @@
+project2_a.py
\ No newline at end of file
diff --git a/src/project2/.idea/inspectionProfiles/profiles_settings.xml b/src/project2/.idea/inspectionProfiles/profiles_settings.xml
new file mode 100644
index 0000000..105ce2d
--- /dev/null
+++ b/src/project2/.idea/inspectionProfiles/profiles_settings.xml
@@ -0,0 +1,6 @@
+
+
+
+
+
+
\ No newline at end of file
diff --git a/src/project2/.idea/misc.xml b/src/project2/.idea/misc.xml
new file mode 100644
index 0000000..d56657a
--- /dev/null
+++ b/src/project2/.idea/misc.xml
@@ -0,0 +1,4 @@
+
+
+
+
\ No newline at end of file
diff --git a/src/project2/.idea/modules.xml b/src/project2/.idea/modules.xml
new file mode 100644
index 0000000..bd50512
--- /dev/null
+++ b/src/project2/.idea/modules.xml
@@ -0,0 +1,8 @@
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/src/project2/.idea/project2.iml b/src/project2/.idea/project2.iml
new file mode 100644
index 0000000..d0876a7
--- /dev/null
+++ b/src/project2/.idea/project2.iml
@@ -0,0 +1,8 @@
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/src/project2/.idea/vcs.xml b/src/project2/.idea/vcs.xml
new file mode 100644
index 0000000..b2bdec2
--- /dev/null
+++ b/src/project2/.idea/vcs.xml
@@ -0,0 +1,6 @@
+
+
+
+
+
+
\ No newline at end of file
diff --git a/src/project2/project2_a.py b/src/project2/project2_a.py
new file mode 100644
index 0000000..4ac9fa3
--- /dev/null
+++ b/src/project2/project2_a.py
@@ -0,0 +1,22 @@
+# A program in “project2_a.py” will copy selected lines from “measles.txt” into a
+# file selected by the user
+try:
+ file = open('measles.txt', 'r')
+ output_file = (input('Output file name>>')).lower()
+ year = input('Enter year >>')
+ if '.txt' in output_file:
+ pass
+ else:
+ print('Error,output file should have [.txt] extension,re-run!!') # output file should have an extension
+ exit()
+ file_saved = open(output_file, 'w')
+ for line in file:
+ if (year == " ") or (year.lower() == "all"):
+ file_saved.write(line) # This is to add data to the new file created.
+ elif line[88:].startswith(year): # #year begins at index 88
+ file_saved.write(line)
+except FileNotFoundError:
+ print("COULD NOT OPEN FILE")
+
+file.close()
+output_file.close()
diff --git a/src/project2/project2_b.py b/src/project2/project2_b.py
new file mode 100644
index 0000000..9a8014b
--- /dev/null
+++ b/src/project2/project2_b.py
@@ -0,0 +1,84 @@
+def open_file():
+ while True:
+ file_name = input("Enter input file name>>> ")
+ try:
+ fhand = open(file_name, "r")
+ break
+ except FileNotFoundError:
+ print("Couldn't open file, Invalid name or file doesn't exist!")
+ continue
+ return fhand
+
+
+def process_file(fhand):
+ while True:
+ year = input("Enter year>>> ")
+ if len(year) == 4: # the entered year must have 4 characters.
+ break
+ else: # When the user enters a wrong year, he is re-prompted until a valid year is entered
+ print("Year must be four digits,please enter a valid year!!")
+ continue
+ while True: # Prompt the user for income level
+ print("Income levels;\n Input 1 for WB_LI\n Input 2 for WB_LMI\n Input 3 for WB_UMI\n Input 4 for WB_HI")
+ income = input("Enter income level>> ")
+ if income == "1":
+ income = "WB_LI"
+ break
+ elif income == "2":
+ income = "WB_LMI"
+ break
+ elif income == "3":
+ income = "WB_UMI"
+ break
+ elif income == "4":
+ income = "WB_HI"
+ break
+ else:
+ print("Invalid income level!!") # if an income level other than (1,2,3,4) is input
+ continue
+
+ count = 0
+ percentages = []
+ countries = []
+ for line in fhand:
+ if (line[88:92] == year) and (line[51:56] == income or line[51:57] == income):
+ count += 1
+ percentages.append(int(line[59:61])) # For each line met,add percentages to the percentages list.
+ country = ((str(line[0:51])).strip())
+ countries.append(country) # adds percentages to the list of countries
+ continue
+ # A dictionary with country as the key and percentages as the value.
+ country_percentage = dict(zip(countries, percentages))
+
+ if count > 0:
+ percent_sum = sum(percentages)
+ average = percent_sum / count
+ maximum = max(percentages)
+ minimum = min(percentages)
+
+ # this gets countries for maximum percentages to this list
+ country_max = [country for country, percentage in country_percentage.items() if percentage == maximum]
+ # this gets countries for minimum percentages to this list
+ country_min = [country for country, percentage in country_percentage.items() if percentage == minimum]
+
+ print(f"Number of countries in the record: {count}")
+ print(f"Average percentage for {year} with {income} is {average:.1f}%")
+
+ print(f"The following countries have the maximum percentage in {year} with {income} of {maximum}%")
+ for i in country_max:
+ print(" -", i) # This prints the countries with maximum percentage.
+ print(f"The following countries have the minimum percentage in {year} with {income} of {minimum}%")
+ for i in country_min:
+ print(" -", i) # This prints the countries with minimum percentage.
+
+ else:
+ print(f"There are no records for the year {year} in the file") # in case there are no items in the list
+
+
+def main():
+ fhand = open_file()
+ process_file(fhand)
+ fhand.close()
+
+
+main()