Welcome to my documentation for the Coursera specialization "Programming for Everybody"

university of michigan logo course info: https://online.umich.edu/series/python-for-everybody/

#3.3 A program that converts scores to grades


                score = float(input("Enter Score: "))
                if score >= 0.9:
                    print("A")
                elif score >= 0.8:
                    print("B")
                elif score >= 0.7:
                    print("C")
                elif score >= 0.6:
                    print("D")
                elif score < 0.6:
                    print("F")
                else:
                    print("this score is out of range")
            

#4.6 A program that computes gross pay


                hours = float(input("how many hours did you work?"))
                rate = float(input("what is your rate of pay?"))
                
                def computepay(h, r):
                    if h > 40:
                        overtime_rate = r * 1.5
                        extra_hours = h - 40
                        normal_hours = h - extra_hours
                        pay = (overtime_rate * extra_hours) + (normal_hours * r)
                        print("Pay", pay)
                    else:
                        print("Pay", h * r)
                    return
                
                computepay(hours, rate)
                    

#5.2 A program that prints the largest of numbers entered


                largest = None
                smallest = None
                
                while True:
                    num = input("Enter a number: ")
                    if num.lower() == "done":
                        print("Maximum is", largest)
                        print("Minimum is", smallest)
                        break
                    try:
                        num = int(num)
                        largest = num if largest is None or num > largest else largest
                        smallest = num if smallest is None or num < smallest else smallest
                    except ValueError:
                        print("Invalid input")
                    

#6.5 A program that reads a file and extracts numbers from it


                *uses mbox-short.txt*
                text = "X-DSPAM-Confidence:    0.8475"
                start = text.find("0")
                end = text.find("5")
                print(float(text[23:29]))

#7.1 A program that reads a file and then prints the file in uppercase


                *uses words.txt*
                fname = input("Enter file name: ")
                fh = open(fname)
                for line in fh:
                    print((line.upper()).strip())

#7.2 A program that averages a set of numbers it finds in a file


                *uses mbox-short.txt*
                fname = input("Enter file name: ")
                fh = open(fname)
                x = 0
                value = 0
                for line in fh:
                    if not line.startswith("X-DSPAM-Confidence:"):
                        continue
                    else:
                        x = x + 1
                        value = value + float(line[20:26])
                average = value / x
                print("Average spam confidence:", average)

#8.4 A program that takes all of the unique words in a file and prints them out in alphabetical order


                *uses romeo.txt*
                fname = input("Enter file name: ")
                fh = open(fname)
                list = list()
                
                for line in fh:
                    lines = line.split()
                    for word in lines:
                        if word not in list:
                            list.append(word)
                list.sort()
                print(list)

#8.5 A program that opens a file and counts the number of times a line begins with a specific word


                *uses mbox-short.txt*
                fname = input("Enter file name: ")
                if len(fname) < 1:
                    fname = "mbox-short.txt"

                fh = open(fname)
                count = 0

                for line in fh:
                    if line.startswith("From "):
                        new_line = line.split()
                        print(new_line[1])
                        count = count + 1

                print("There were", count, "lines in the file with From as the first word")
            

#9.4 A program that calculates the most prolific email sender based on data from a file


                *uses mbox-short.txt*
                name = input("Enter file:")
                if len(name) < 1:
                    name = "mbox-short.txt"
                file = open(name)
                dictionary = {}
                emails = []

                big_number = 0
                big_word = None

                count = 0
                for line in file:
                    if line.startswith("From "):
                        lines_of_from = line.split()
                        emails.append(lines_of_from[1])
                    
                for email in emails:
                    dictionary[email] = dictionary.get(email, 0) + 1
                    
                for key in dictionary:
                    if dictionary[key] > big_number or big_number == None:
                        big_number = dictionary[key]
                        big_word = key

                print(big_word, big_number)
            

#10.2 A program that counts the number of times an email was sent per hour of the day


                *uses mbox-short.txt*
                name = input("Enter file:")
                if len(name) < 1:
                name = "mbox-short.txt"
                file = open(name)

                times = []
                hours = []
                dictionary_of_hours = {}


                for line in file:
                if line.startswith("From "):
                    from_lines = line.split()
                    times.append(from_lines[5])
                    
                for time in times:
                hhmmss = time.split(":")
                hours.append(hhmmss[0])

                hours.sort()
                
                for hour in hours:
                dictionary_of_hours[hour] = dictionary_of_hours.get(hour, 0) + 1

                for key, value in dictionary_of_hours.items():
                print(key, value)
            

#11.2 A program that finds the sum of all numbers in a file

*uses sample.txt*
                import re
                input_file = input("file name: ")
                file = open(input_file)
               
                numbers = []
                count = 0
               
                for line in file:
                    x = re.findall("[0-9]+", line)
                    for index in x:
                        y = re.findall("[0-9]+", index)
                        count = count + int(y[0])
                print(count)                  
            

#12.5 A program that finds the sum of all numbers in the span tags within a file

*uses http://py4e-data.dr-chuck.net/comments_42.html or "http://py4e-data.dr-chuck.net/comments_2014612.html"*
                from urllib.request import urlopen
                from bs4 import BeautifulSoup
                import ssl
                
                count = 0
                
                # Ignore SSL certificate errors
                ctx = ssl.create_default_context()
                ctx.check_hostname = False
                ctx.verify_mode = ssl.CERT_NONE
                
                url = input('Enter - ')
                html = urlopen(url, context=ctx).read()
                soup = BeautifulSoup(html, "html.parser")
                
                # Retrieve all of the span tags
                tags = soup('span')
                for tag in tags:
                    print('Contents:', tag.contents[0])
                    count=count+int(tag.contents[0])
                print(count)
            

#12.6 A program that follows a link at x position, x number of times

*uses http://py4e-data.dr-chuck.net/known_by_Shayan.html or "http://py4e-data.dr-chuck.net/known_by_Fikret.html"*
                import urllib.request, urllib.parse, urllib.error
                from bs4 import BeautifulSoup
                import ssl
                import re

                # Ignore SSL certificate errors
                ctx = ssl.create_default_context()
                ctx.check_hostname = False
                ctx.verify_mode = ssl.CERT_NONE

                urls = []
                url = input("Enter URL: ")
                count = input("Enter count: ")
                try:
                    count = int(count)
                except:
                    print("please enter a number")
                    count = int(input("Enter count: "))
                    
                position = input("Enter position: ")
                try:
                    position = int(position)-1
                except:
                    print("please enter a number")
                    position = int(input("Enter position: "))

                print("Retrieving: ", url)
                for number in range(count):
                    html = urllib.request.urlopen(url, context=ctx).read()
                    soup = BeautifulSoup(html, 'html.parser')
                    tags = soup('a')
                    url = tags[position].get('href', None)
                    print("Retrieving: ", url)