Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Sample code for ADA topic 1 Date: 2 0 - 1 0 - 2 0 2 2 Following should be inside ratNum.py
Sample code for ADA topic
Date:
Following should be inside "ratNum.py
# find greatest common divisor
# using the fact that gcdab gcdbc where c a b
# if b is zero, gcdab gcda a
def gcdab:
while b :
r a b
a b
b r
return a
# find least common multiple using gcd function
def lcma b:
return ab gcdab
# ratNum representing rational numbers ab
class ratNum:
# constructor for creating a rational number, default is zero
def initself a b:
self.a a
if b : self.b b
else:
printb shound be greater than zero!"
self.b
def inputself: # input a rational number from keyboard
while True:
a b inputinput ab b: split
a b inta intb
if b : break
printb shound be greater than zero!"
self.a a
self.b b
def strself: # convert to str for display
return fselfaselfb
def addself y: # rational number addition
b lcmselfb yb
a self.a b self.b ya b yb
return ratNumab
def subself y: # rational number subtraction
b lcmselfb yb
a self.a b self.b ya b yb
return ratNumab
def mulself y: # rational number multiplication
a self.a ya
b self.b yb
g gcda b;
a a g
b b g
return ratNumab
def truedivself y: # rational number division
if ya : printDivision by zero!"
else:
a self.a yb
b self.b ya
g gcda b
a a g
b b g
return ratNumab
def eqself y: # comparing two rational numbers
diff self ya
if diff : return
elif diff : return
return
# end of "ratNum.py
# Following should be inside "main.py
# from ratNum import
z ratNum; zinput
z ratNum; zinput
printz z zz # Task
printz z zz # Task
printzx z zz # Task
printz z zz # Task
printz z zz # Task
User login using dictionary
Date:
# user records using dictionary, name:password
Users "John Chan": abc", "Mary": cde"
# add user, name st
def addUserst pas:
if st not in Users:
Usersst pas
printst "has been added"
else: printst "could not be added"
# delete user, name st
def delUserst:
if st in Users:
del Usersst
printst "has been removed"
else: printstis not a user"
# change sts password to pas
def changePassst pas:
Usersst pas
printPassword has been changed"
from getpass import getpass # getpass function will hide user input
# return True if login successful, name st
def loginst:
#pa inputPassword:
pa getpassPassword:
for u in Users:
if u st and pa Usersu:
return True
return False
# main program
# your may read password file here
# f openuserstxt
# Users; stfreadline; Usersstfreadline
while True:
print User login
Change password
Add user
Delete user
Quit
ch inputYour choice strip
if ch :
st inputUser name
if loginst: printLogin successful!"
else: printIncorrect user name or password"
elif ch :
st inputUser name
if loginst:
#pa inputNew password
pa getpassNew password
changePassst pa
else: printIncorrect user name or password"
elif ch :
st inputUser name
#addUserst inputPassword:
addUserst getpass
elif ch :
delUser inputUser name
elif ch : break
else: printIllegal choice!"
# your may save password file here
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started