Python Forum
Thread Rating:
  • 3 Vote(s) - 2 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Incorrect code
#1
I have a Incorrect  code ,and I need to find the faults and fix them.
Code:
def ok(a, b, c):
    if abs(a - b) < c or a * b > 0:
        return False
    return True


def number(d, e):
    f = 0
    for i in range(len(d)):
        if not ok(d[i], d[i+1], 3):
            f += 1
            return f

print(number([1, -3, 1, 0, 0, -2, -3, 3, -3, 12], 3))
The correct solution must be given in response..
 If I give my arguments def (number([1, 4], 2))  = 0,  def (number([-1, 3], 4)) = 0 .def (number([-1, 3], 3)) = 1,def (number([[0, 3], 1)) = 0,def (number([1, 4, 0, -10, -1, 3, 42], 2)) = 1,def (number([1, -3, 1, 0, 0, -2, -3, 3, -3, 12], 3) = 5

                                           
Reply
#2
Your return statement is in the wrong place. Remember that indentation matters in Python. return f should be at the same indentation level as the for statement.

Once you fix that, you will get an index error. Your looping variable i is going to go to the last index of d, and then your if statement is going to try to go one beyond that with d[i+1]. Normally I would suggest using enumerate, however since this is in homework you may not have gone over that in class. So would suggest for i in range(len(d) - 1):
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
Ok I fixed it ,But something's still wrong...This program will return the wrong numbers


def ok(a, b, c):
    if abs(a - b) > c or a * b < 0:
        return False
    return True
 
 
def number(d, e):
    f = 0
    for i in range(len(d)-1):
        if not ok(d[i], d[i+1], 3):
            f += 1
    return f
 
print(number([-1, 3], 4)) # It must return to 0, but return to 1
print(number([1, 4, 0, -10, -1, 3, 42], 2)) # It must return to 1, but return to 5
print(number([-7, 1, -6, -6, -8], 10)) # It must return to 0, but return to 2
print(number( [-5, -2, 0, 5, 7, -9, -3], 3)) # It must return to 1, but return to 3
print(number( [-3, -9, 2, -10, -8, 0, 9, 2], 2)) # It must return to 2, but return to 6
print(number([8, -1, -8, -2, 0, 8, -5, -10, 5], 2)) # It must return to 3, but return to 7

                                 
Reply
#4
Well, not knowing the actual assignment, I'm not sure what your code is supposed to do. I can't tell you how to fix it.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#5
Let's look at just the first case:
Quote:
def ok(a, b, c):
    if abs(a - b) > c or a * b < 0:
        return False
    return True
  
  
def number(d, e):
    f = 0
    for i in range(len(d)-1):
        if not ok(d[i], d[i+1], 3):
            f += 1
    return f
  
print(number([-1, 3], 4)) # It must return to 0, but return to 1

number([-1, 3], 4)
-> f = 0
-> for i in [0]:
-> -> if not (False):
-> -> -> f += 1
-> return f
Seems like your issue is in the ok function, since it's returning False when it probably shouldn't.
abs(-1 - 3) is 4, which is indeed greater than 3.
Reply
#6
Code compares numbers. 

def ok(a, b, c):
    if abs(a - b) > c or a * b < 0:
        return False
    return True
   
   
def number(d, e):
    f = 0
    for i in range(len(d)-1):
        if not ok(d[i], d[i+1], 3):
            f += 1
    return f

d = [8, -1, -8, -2, 0, 8, -5, -10, 5] #Compares numbers Like this(8 & -1),(-1 & -8),(-8 & -2),(-2 & 0),(0 & 8)......
# If one number is positive and the second negative and The difference is greater than e f+1
e =  2
print(number(d, e)) 
Reply
#7
I think you should do some test cases for your ok() function.  Because I don't think it's doing what you think it is.
>>> def ok(a, b, c):
...     if abs(a - b) > c or a * b < 0:
...         return False
...     return True
...
>>> assert(True == ok(-1, 3, 3))
Traceback (most recent call last):
 File "<stdin>", line 1, in <module>
AssertionError
Reply
#8
(May-08-2017, 05:24 PM)nilamo Wrote:
>>> assert(True == ok(-1, 3, 3))
Traceback (most recent call last):
 File "<stdin>", line 1, in <module>
AssertionError

Why comparison to True? Works as well
assert(ok(-1, 3, 3))
Test everything in a Python shell (iPython, Azure Notebook, etc.)
  • Someone gave you an advice you liked? Test it - maybe the advice was actually bad.
  • Someone gave you an advice you think is bad? Test it before arguing - maybe it was good.
  • You posted a claim that something you did not test works? Be prepared to eat your hat.
Reply
#9
I think the real question, is why is assert still a statement in py3 instead of a function?
Reply
#10
(May-08-2017, 04:06 PM)janek30 Wrote:
def ok(a, b, c):
    if abs(a - b) > c or a * b < 0:
        return False
    return True

Usually - since you get boolean expression under if, you just return the result of expression
def ok(a, b, c):
    return not (abs(a - b) > c or a * b < 0)
or, better form
def ok(a, b, c):
    return abs(a - b) <= c and a * b >= 0

(May-08-2017, 07:43 PM)nilamo Wrote: I think the real question, is why is assert still a statement in py3 instead of a function?
Are you sure? I have 3.4, and both function and statement forms work
In [2]: assert(ok(-1,3,3))
---------------------------------------------------------------------------
AssertionError                            Traceback (most recent call last)
<ipython-input-2-53014166242a> in <module>()
----> 1 assert(ok(-1,3,3))

AssertionError:

In [3]: assert ok(-1,3,3)
---------------------------------------------------------------------------
AssertionError                            Traceback (most recent call last)
<ipython-input-3-1a98f132389b> in <module>()
----> 1 assert ok(-1,3,3)

AssertionError:
I am against showing direct comparison to True/False even to noobs - they believe that this is legitimate practice - it is legal, but not legitimate! - and it muddles understanding of boolean nature. IMHO
Test everything in a Python shell (iPython, Azure Notebook, etc.)
  • Someone gave you an advice you liked? Test it - maybe the advice was actually bad.
  • Someone gave you an advice you think is bad? Test it before arguing - maybe it was good.
  • You posted a claim that something you did not test works? Be prepared to eat your hat.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  I am getting an incorrect average, and not sure why? What's wrong with my code? shirleylam852 8 4,702 Nov-20-2020, 05:32 AM
Last Post: deanhystad
  Return giving incorrect value Calingaladha 5 3,353 Oct-24-2018, 09:53 PM
Last Post: LeSchakal
  Writing incorrect passwords to a file till its correct garth 2 4,951 Feb-10-2017, 11:41 PM
Last Post: ichabod801

Forum Jump:

User Panel Messages

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