num = 7652
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
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()
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())
digit1 = num//1000
print(digit1)
snum = str(num)
print(snum)
snum[0]
7652%1000
def extract_digits(num):
snum = str(num)
return [int(snum[0]), int(snum[1]), int(snum[2]), int(snum[3])]
extract_digits(7654)
from random import *
num = randint(1000,9999)
print(num)
digits = extract_digits(num)
print(digits)
s = set(digits)
print(s)
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
print(no_repeating_digits(7662))
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
number_of_bulls(1234,5234)
number_of_cows(1234,1324)
def produce_target():
while True:
num = randint(1000,9999)
if no_repeating_digits(num):
break
return num
produce_target()
def main():
print_message()
target = produce_target()
num_guesses = 0
while True:
...
if bulls == 4 or num_guesses > 10:
break