In python 3 I need this script updated to call itself in the same function and g
ID: 3849720 • Letter: I
Question
In python 3
I need this script updated to call itself in the same function and guard itself
The module with the class in it.
The testing code can be included. In that case, "guard" it by checking __name__.
class rational_numbers:
#constructor function to call a nume/denom and rational
def __init__(self,P,Q=1):
#if statement should denom <0
if Q < 0:
#creates variable self parameter for nume being -P
self.nume = -P
#creates variable self parameter for denom being absolute value to Q
self.denom = abs(Q)
#elif statement for nume <0 and denom<0
elif P < 0 and Q < 0:
#creates variable self parameter for absolute value of P
self.nume = abs(P)
#creates variable self parameter for absolute value of Q
self.denom = abs(Q)
#elif statement for Q equal to 0
elif Q==0:
#raise zero division erro in try except statement in main
raise ZeroDivisionError
#else statement for not falling in above
else:
#creates variable self parameter for p
self.nume = P
#creates varaible for self parameter for q
self.denom =Q
#creates function __str__ for string representation
def __str__(self):
#returns value of string
return '('+str(self.nume) + '/'+str(self.denom)+ ')'
#creates plus function add sum of two rationals together
def plus(self,other):
#returns value
return rational_numbers((self.nume*other.denom) + (self.denom * other.nume), (self.denom * other.denom))
#creates star function to add product of two rationals together
def star(self,other):
#returns value
return rational_numbers((self.numer * other.nume), (self.denom * other.denom))
#creates toFloat function to handle float/approximation of value
def toFloat(self):
return "%.2f" % (self.nume/self.denom)
#code for testing
#imports rational_numbers from above or if file was seperated and in same folder
import rational_numbers
#creates main function
def main():
#creates variable 'a' using class_rational
a=rational_numbers.rational(3)
#creates variable 'b' using class_rational
b=rational_numbers.rational(-1,-2)
#creates variable 'c' using class_rational
c=b.star(b)
#prints 'a' value to user display
print('a =', a)
#prints 'b' value to user display
print('b =', b)
#prints 'c' value to user display
print('c =', c)
#prints 'a + b' value to user display
print(a,' + ', b,' = ', a.plus(b))
#prints 'b * b' value to user display
print(b,' * ', b,' = ', a.star(b))
#prints 'c * c' value to user display
print(c,' * ', b,' = ', c.star(b))
#prints 'a' value to user display as float
print(a,' = ', a.toFloat())
#prints 'b' value to user display as float
print(b,' = ', b.toFloat())
try:
a = rational_numbers.rational(3, 0)
except ZeroDivisionError:
print('Houston, we have a problem.')
main()
Explanation / Answer
Please Elaborate what do you want to be done on the script in comments, precisely which function you want to update or what functionality you want and
As i comprehend from your query, There is no need to secure your module rational_numbers.py with __name__ as it does not have test code , I have shown in the program when you will be using __name__ to secure the module ,although i have secured the code and I removed the errors from the script
rational_numbers.py:
class rational_numbers:
#constructor function to call a nume/denom and rational
def __init__(self,P,Q=1):
#if statement should denom <0
if Q < 0:
#creates variable self parameter for nume being -P
self.nume = -P
#creates variable self parameter for denom being absolute value to Q
self.denom = abs(Q)
#elif statement for nume <0 and denom<0
elif P < 0 and Q < 0:
#creates variable self parameter for absolute value of P
self.nume = abs(P)
#creates variable self parameter for absolute value of Q
self.denom = abs(Q)
#elif statement for Q equal to 0
elif Q==0:
#raise zero division erro in try except statement in main
raise ZeroDivisionError
#else statement for not falling in above
else:
#creates variable self parameter for p
self.nume = P
#creates varaible for self parameter for q
self.denom =Q
#creates function __str__ for string representation
def __str__(self):
#returns value of string
return '('+str(self.nume) + '/'+str(self.denom)+ ')'
#creates plus function add sum of two rationals together
def plus(self,other):
#returns value
return rational_numbers((self.nume*other.denom) + (self.denom * other.nume), (self.denom * other.denom))
#creates star function to add product of two rationals together
def star(self,other):
#returns value
return rational_numbers((self.nume * other.nume), (self.denom * other.denom))
#creates toFloat function to handle float/approximation of value
def toFloat(self):
return "%.2f" % (self.nume/self.denom)
if __name__ == "__main__":
# It is better to use when you have tester code in your module
# If there is only class definition in your module then you don't need the __name__ security
def main():
#creates variable 'a' using class_rational
a=rational_numbers(3)
#creates variable 'b' using class_rational
b=rational_numbers(-1,-2)
#creates variable 'c' using class_rational
c=b.star(b)
#prints 'a' value to user display
print('a =', a)
#prints 'b' value to user display
print('b =', b)
#prints 'c' value to user display
print('c =', c)
#prints 'a + b' value to user display
print(a,' + ', b,' = ', a.plus(b))
#prints 'b * b' value to user display
print(b,' * ', b,' = ', a.star(b))
#prints 'c * c' value to user display
print(c,' * ', b,' = ', c.star(b))
#prints 'a' value to user display as float
print(a,' = ', a.toFloat())
#prints 'b' value to user display as float
print(b,' = ', b.toFloat())
try:
a = rational_numbers(3, 0)
except ZeroDivisionError:
print('Houston, we have a problem.')
main()
main.py:
#imports rational_numbers from above or if file was seperated and in same folder
#import rational_numbers
# Correct import would be as we are importing class rational_numbers from module rational_numbers
from rational_numbers import rational_numbers
#creates main function
def main():
#creates variable 'a' using class_rational
a=rational_numbers(3)
#creates variable 'b' using class_rational
b=rational_numbers(-1,-2) # The correct method to call the constructor
#creates variable 'c' using class_rational
c=b.star(b)
#prints 'a' value to user display
print('a =', a)
#prints 'b' value to user display
print('b =', b)
#prints 'c' value to user display
print('c =', c)
#prints 'a + b' value to user display
print(a,' + ', b,' = ', a.plus(b))
#prints 'b * b' value to user display
print(b,' * ', b,' = ', a.star(b))
#prints 'c * c' value to user display
print(c,' * ', b,' = ', c.star(b))
#prints 'a' value to user display as float
print(a,' = ', a.toFloat())
#prints 'b' value to user display as float
print(b,' = ', b.toFloat())
try:
a = rational_numbers(3, 0)
except ZeroDivisionError:
print('Houston, we have a problem.')
main()
PLEASE RATE!!!
OUTPUT:-
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.