In [ ]:
num = 7652
In [2]:
class Student:

  def __init__(self,name):
    self._name = name
    self._courses = []

  def add_course(self,cno,credits,grade):
    self._courses.append((cno,credits,grade))
  
  def gpa(self):
    grade_points = {'A': 4, 'B': 3, 'C': 2, 'D': 1, 'F': 0}
    points = 0
    total_credits = 0
    for (cn,cr,g) in self._courses:
      points += cr*grade_points[g]
      total_credits += cr
    if (total_credits > 0):
      return round(points/total_credits,2)
    else:
      return None
In [3]:
def print_transcript(s):
  print("\nTranscript for ",s._name,"\n")
  for c in s._courses:
    print("%6s  %.1f  %3s"%(c[0],c[1],c[2]))
  print("\nGPA: ",s.gpa(),"\n")

def main():
  s1 = Student("Tommy Jones")
  s1.add_course('CSc7003',1.5,'A')
  s1.add_course('CSc6710',4,'B')
  s1.add_course('CSc3320',3,'C')
  print_transcript(s1)

  s2 = Student("Hilary Smith")
  s2.add_course('CSc 1301',4,'A')
  s2.add_course('CSc 1302',4,'A')
  s2.add_course('CSc 2510',3,'B')
  s2.add_course('CSc 2720',3,'A')
  s2.add_course('CSc 3210',3,'C')
  s2.add_course('CSc 3320',3,'A')
  print_transcript(s2)

main()
Transcript for  Tommy Jones 

CSc7003  1.5    A
CSc6710  4.0    B
CSc3320  3.0    C

GPA:  2.82 


Transcript for  Hilary Smith 

CSc 1301  4.0    A
CSc 1302  4.0    A
CSc 2510  3.0    B
CSc 2720  3.0    A
CSc 3210  3.0    C
CSc 3320  3.0    A

GPA:  3.55 

In [35]:
s1 = Student("Raj")
s1.add_course('csc7003',1.5,'B')
s1.add_course('aaa',3.0,'A')
print(s1.gpa())
s2 = Student('Jim')
s2.add_course('csc1',3.0,'B')
print(s2.gpa())
3.67
3.0
In [4]:
digit1 = num//1000
print(digit1)
7
In [5]:
snum = str(num)
In [6]:
print(snum)
7652
In [7]:
snum[0]
Out[7]:
'7'
In [8]:
7652%1000
Out[8]:
652
In [9]:
def extract_digits(num):
    snum = str(num)
    return [int(snum[0]), int(snum[1]), int(snum[2]), int(snum[3])]
In [10]:
extract_digits(7654)
Out[10]:
[7, 6, 5, 4]
In [11]:
from random import *
In [12]:
num = randint(1000,9999)
In [13]:
print(num)
4137
In [14]:
digits = extract_digits(num)
In [15]:
print(digits)
[4, 1, 3, 7]
In [16]:
s = set(digits)
In [17]:
print(s)
{1, 3, 4, 7}
In [18]:
def no_repeating_digits(num):
    digits = extract_digits(num)
    if digits[0] != digits[1] and \
       digits[0] != digits[2] and \
       digits[0] != digits[3] and \
       digits[1] != digits[2] and \
       digits[1] != digits[3] and \
       digits[2] != digits[3]:
        return True
    else:
        return False
In [20]:
print(no_repeating_digits(7662))
False
In [45]:
def number_of_bulls(num1,num2):
    d1 = extract_digits(num1)
    d2 = extract_digits(num2)
    nbulls = 0
    for i in range(4):
        if d1[i] == d2[i]:
            nbulls = nbulls + 1
    return nbulls

def number_of_cows(num1,num2):
    d1 = extract_digits(num1)
    d2 = extract_digits(num2)
    ncows = 0
    for i in range(4):
        for j in range(4):
            print(i,j)
            if i != j and d1[i] == d2[j]:
                ncows = ncows + 1
    return ncows
In [42]:
number_of_bulls(1234,5234)
Out[42]:
3
In [49]:
number_of_cows(1234,1324)
0 0
0 1
0 2
0 3
1 0
1 1
1 2
1 3
2 0
2 1
2 2
2 3
3 0
3 1
3 2
3 3
Out[49]:
2
In [23]:
def produce_target():
    while True:
        num = randint(1000,9999)
        if no_repeating_digits(num):
            break
    return num
In [24]:
produce_target()
Out[24]:
8751
In [ ]:
def main():
    print_message()
    target = produce_target()
    num_guesses = 0
    while True:
        ...
        if bulls == 4 or num_guesses > 10:
            break