Python Forum
missing 1 required positional argument
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
missing 1 required positional argument
#1
given this code, I should extend
the class so that it can be initialized with only one real value.

for instance,
print(interval(1))
should print [1,1].

This is the code (which is the same as my previous task),
but I have no Idea how to change it, as the code the way it is now,
it produces the following error:

Error:
line 183, in <module> print(interval(1)) TypeError: __init__() missing 1 required positional argument: 'right'
Any help/hint?

class interval:
    def __init__(self,left,right):
        self.right=right
        self.left=left
    def __add__(self,other):
        a,b,c,d = self.left, self.right, other.left, other.right
        return interval(a+c,b+d)
    
    def __sub__(self,other):
        a,b,c,d=self.left,self.right,other.left,other.right
        return interval(min(a,b)-max(c,d),max(a,b)-min(c,d))
    def __mul__(self,other):
        a,b,c,d=self.left,self.right,other.left,other.right
        return interval(min(a*c, a*d, b*c, b*d),
                            max(a*c, a*d, b*c, b*d))
    def __truediv__(self, other):
        a, b, c, d = self.left, self.right, other.left, other.right
        # [c,d] cannot contain zero:
        print(min(a/c, a/d, b/c, b/d))
        
        if c*d <= 0:
            raise ValueError ('Interval %s cannot be denominator because it contains zero' % other)
        
        return interval(min(a/c, a/d, b/c, b/d), max(a/c, a/d, b/c, b/d))
    
    def __contains__(self,other):
        return (other>=self.left and other<=self.right)
    
    def __repr__(self):
        return '[{},{}]'.format(self.left,self.right)
 
print(interval(1))
Reply
#2
The error is because the __init__ of class interval takes two arguments left & right.
As right has no default argument you must supply a value, you can give right a defualt value as follows.
class interval:
    def __init__(self,left,right=None):
Reply
#3
the way you suggested, makes the program run instead of presenting an error, but the printout statement
is [1,None] instead of [1,1].
Reply
#4
class interval:
    def __init__(self,left,right=None):
        self.right=right
        self.left=left
Alter how self.right is assigned based on the value of the passed in right
Reply
#5
this is exactly how I have my code written, and again, the outcome I obtain
is still [1,None].

How do I obtain [1,1] ?
Reply
#6
(May-07-2019, 08:27 PM)Yoriz Wrote:
class interval:
    def __init__(self,left,right=None):
        self.right=right
        self.left=left
Alter how self.right is assigned based on the value of the passed in right
Yes you need to alter it as stated else it will just give the same answer.
Reply
#7
I am sorry, but I am not understanding what you mean by "alter".
Should I assign self.right another value perhaps?

I just wrote self.right=left and it looks like it works,
but I am not sure if this is the way I am supposed to write it or is
just a coincidence that I am getting the expected outcome?
Reply
#8
The same question was asked by someone else just the other day, so i'm not just giving away the answer Tongue, obviously doing the same homework.
if you pass in a value for right you want self.right to be right, but if right is None, you want to set self.right to something else.
Reply
#9
(May-07-2019, 08:41 PM)mcgrim Wrote: I am sorry, but I am not understanding what you mean by "alter".
Should I assign self.right another value perhaps?

I just wrote self.right=left and it looks like it works,
but I am not sure if this is the way I am supposed to write it or is
just a coincidence that I am getting the expected outcome?

if you just change it to self.right=left what happens when you want to do print(interval(1, 2)).
Alter(change) it so it works for both print(interval(1)) and print(interval(1, 2))
Reply
#10
I tried to
 print(interval(1,2))
and I still obtain [1,1], so I am assuming is correct,
isn't it?

I am not asking you to give me the answer, but at least to let me know
If I got it myself or not.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  TypeError: __init__() missing 1 required positional argument: 'successor siki 1 4,248 Mar-08-2021, 02:05 PM
Last Post: Larz60+
  Missing 1 required positional argument in python code edwinostby 7 9,743 Jan-19-2021, 12:52 PM
Last Post: Serafim
  Missing positional arguments error?? hhydration 2 2,099 Oct-01-2020, 05:33 AM
Last Post: buran
  TypeError: Missing required positional arguments liaisa 7 28,464 Sep-25-2020, 08:16 PM
Last Post: deanhystad
  missing positional argument error programmert 1 2,771 Oct-18-2019, 11:05 AM
Last Post: Larz60+
  missing 1 required positional argument jedmond2 4 6,570 Sep-19-2019, 12:00 PM
Last Post: jefsummers
  TypeError: __init__() missing 3 required positional arguments Pythonhelp82 6 22,911 Jan-24-2019, 04:25 AM
Last Post: Pythonhelp82
  TypeError: method missing 1 positional argument koolinka 4 4,966 Nov-18-2018, 04:53 PM
Last Post: ichabod801
  another positional argument error (...and executing objects stored in a list) itmustbebunnies 7 4,146 Nov-16-2018, 07:18 PM
Last Post: itmustbebunnies
  Class positional argument error itmustbebunnies 2 2,960 Nov-07-2018, 11:09 AM
Last Post: stullis

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020