Problem solving

why return 3 I need one

Hi, instead of an image, can you post your code like this:

```
# paste code here
```

or do this:

[code]
# paste code here
[/code]

Then I can change it to show you how to fix it. BUT, it looks like you wrote all this code out of your head and only ran it once. You should build code, and that means start with only a few lines, run it, a few more, run it, a few more, run it, until it’s working as you intended. That’s easier than dumping a ton of code out of your brain and then trying to fix it after.

1 Like
[code]
def isOdd(n):
      if n % 2 == 1:
           print("weird")
def isEven(n):
      if n % 2 == 0:
           print("Not weird")
def main(n):
     if isEven(n) and range(2, 6):
          retrun isEven(n)
     elif isEven(n) and range(6, 21) n < 20:
          return isOdd(n)
     elif isEven(n) and n > 20:
          return isEven(n)
     elif isOdd(n):
          return isOdd(n)
n = int(input("add num: "))
main(n)
[/code]

A great opportunity for debugging here @BeKingOrDiepjhtcss …

Run the script with python’s debugger:

python -m pdb main.py

Note - my debug file was called BeKing.py…

I used list on the file to see methods then next to skip through their definition. Then s (step) through each line of code as the file runs from main… See if you can work out why you are getting three printouts when 6 is used as the argument, by following the lines being executed. This is definitely more hassle than building smaller chunks and @zedshaw prescribes that for a reason :wink:

Look for the –call– and –return– flags on each line, as this is where your program is either calling the function or returning output. Try drawing it out on a piece of paper…

[code]

/Users/gpkesley/projects/hacks/BeKing.py(1)()
-> def isOdd(n):
(Pdb) list
1 -> def isOdd(n):
2 if n % 2 == 1:
3 print(“weird”)
4
5 def isEven(n):
6 if n % 2 == 0:
7 print(“Not weird”)
8
9 def main(n):
10 if isEven(n) and range(2, 6):
11 return isEven(n)
(Pdb) next
/Users/gpkesley/projects/hacks/BeKing.py(5)()
-> def isEven(n):
(Pdb) next
/Users/gpkesley/projects/hacks/BeKing.py(9)()
-> def main(n):
(Pdb) s
/Users/gpkesley/projects/hacks/BeKing.py(19)()
-> n = int(input("add num: "))
(Pdb) s
add num: 6
/Users/gpkesley/projects/hacks/BeKing.py(20)()
-> main(n)
(Pdb) s
–Call–
/Users/gpkesley/projects/hacks/BeKing.py(9)main()
-> def main(n):
(Pdb) s
/Users/gpkesley/projects/hacks/BeKing.py(10)main()
-> if isEven(n) and range(2, 6):
(Pdb) s
–Call–
/Users/gpkesley/projects/hacks/BeKing.py(5)isEven()
-> def isEven(n):
(Pdb) s
/Users/gpkesley/projects/hacks/BeKing.py(6)isEven()
-> if n % 2 == 0:
(Pdb) s
/Users/gpkesley/projects/hacks/BeKing.py(7)isEven()
-> print(“Not weird”)
(Pdb) s
Not weird
–Return–
/Users/gpkesley/projects/hacks/BeKing.py(7)isEven()->None
-> print(“Not weird”)
(Pdb) s
/Users/gpkesley/projects/hacks/BeKing.py(12)main()
-> elif isEven(n) and range(6, 21) and n < 20:
(Pdb) s
–Call–
/Users/gpkesley/projects/hacks/BeKing.py(5)isEven()
-> def isEven(n):
(Pdb) s
/Users/gpkesley/projects/hacks/BeKing.py(6)isEven()
-> if n % 2 == 0:
(Pdb) s
/Users/gpkesley/projects/hacks/BeKing.py(7)isEven()
-> print(“Not weird”)
(Pdb) s
Not weird
–Return–
/Users/gpkesley/projects/hacks/BeKing.py(7)isEven()->None
-> print(“Not weird”)
(Pdb) s
/Users/gpkesley/projects/hacks/BeKing.py(14)main()
-> elif isEven(n) and n > 20:
(Pdb) s
–Call–
/Users/gpkesley/projects/hacks/BeKing.py(5)isEven()
-> def isEven(n):
(Pdb) s
/Users/gpkesley/projects/hacks/BeKing.py(6)isEven()
-> if n % 2 == 0:
(Pdb) s
/Users/gpkesley/projects/hacks/BeKing.py(7)isEven()
-> print(“Not weird”)
(Pdb) s
Not weird
–Return–
/Users/gpkesley/projects/hacks/BeKing.py(7)isEven()->None
-> print(“Not weird”)
(Pdb) s
/Users/gpkesley/projects/hacks/BeKing.py(16)main()
-> elif isOdd(n):
(Pdb) s
–Call–
/Users/gpkesley/projects/hacks/BeKing.py(1)isOdd()
-> def isOdd(n):
(Pdb) s
/Users/gpkesley/projects/hacks/BeKing.py(2)isOdd()
-> if n % 2 == 1:
(Pdb) s
–Return–
/Users/gpkesley/projects/hacks/BeKing.py(2)isOdd()->None
-> if n % 2 == 1:
(Pdb) s
–Return–
/Users/gpkesley/projects/hacks/BeKing.py(16)main()->None
-> elif isOdd(n):
(Pdb) s
–Return–
/Users/gpkesley/projects/hacks/BeKing.py(20)()->None
-> main(n)
(Pdb) s
–Return–
(1)()->None
(Pdb) s
/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/bdb.py(584)run()
-> self.quitting = True[/code]

1 Like

after use python’s debugger:

[code]
def isOdd(n):
      if n % 2 == 1:
           return True
def isEven(n):
      if n % 2 == 0:
           return True


def main(n):
     if isEven(n) == True and range(2, 6):
          print("Not Weird")
     elif isEven(n) == True and range(6, 21):
          print("Weird")
     elif isEven(n) == True and n > 20:
          print("Not Weird")
     elif isOdd(n):
          print("Weird")
n = int(input("add num: "))
main(n)
[code]

I don’t think those ranges in your tests do what you expect them to do…

I actually search about it , I think should test with n

Thanks for the code. Looks like @florian and @gpkesley are helping you, but a quick clarification:

You either do triple-backtick OR use [code], not both. So either:

[code]
# code here
[/code]

Or this:

```
# code here
``` 

But not both together.

1 Like

I think @florian was referring to your use of range(). Remember you have to be explicit with your logic.

if isEven(n) == True and range(2, 6):

You are testing n has no remainder in isEven() and the your next test for the range has no statement to test against (but I expect you think that it will check if n is in that range). You need to state conditions explicitly.

if isEven(n) == True and n in range(2, 6):

You can make this script a little easier if you define only one function too.

def isEven(n):
    if n % 2 == 0:
        return True
    else:
        return False 

n = int(input("Add num: "))

if isEven(n) == True and n in range(2,6):
    print("Even and under 6")
elif isEven(n) == True and n in range(6, 21):
    print("Even and between 6 and 21")
elif isEven(n) == True and n > 20:
    print("Even and greater than 20")
elif isEven(n) == False:
    print("Not at all even!") 
2 Likes

As an aside:
Division/modulo is an expensive operation. Interestingly, you can do parity checks an order of magnitude more efficient by bitwise AND-ing against 1:

n & 1 == 0 iff 2 | n, otherwise 1

In the context of your tests:

if not n & 1 and n in range(2,6): ...
1 Like

how can I explain?? , okay,
take a look in this picture

if you see in first if condition 3 is odd and I must return “Weird”
and the second if condition says is even and in range(2, 6) and 3 in range but not even

def isOdd(n):
      if n % 2 == 1:
           return True
def isEven(n):
      if n % 2 == 0:
           return True

def main(n):
     if isEven(n) == True and n == 2 and n < 6:
          print("Not Weird")
     elif isEven(n) == True and n == 6 and n <21:
          print("Weird")
     elif isEven(n) == True and n > 20:
          print("Not Weird")
     elif isOdd(n):
          print("Weird")

n = int(input("add num: "))
main(n)

why this code work
if is odd and else is even

if n%2 != 0:
        print("Weird")
else:
    if n>=2 and n<=5:
        print("Not Weird")
    elif n>=6 and n<=20:
        print("Weird")
    else:
        print("Not Weird")

and my code not work

I’m sorry, I just don’t understand your question.

If your control structures don’t work as expected, do your debug printing. Add a print statement at the top of each branch, telling which branch is executed, then examine all the conditions up you that point closely if you end up in the wrong branch. (I can see errors in your conditions, but you’ll find them on your own. ;-))

@BeKingOrDiepjhtcss Given that you are asking for help for Hackerrank’s 30 Days of Code, I would suggest Googling for solutions (You Tube) but I pretty much gave you the answer above as I recognised it.

I think your failures are because you aren’t handling the other else cases.

def is_even(N):
    if (N % 2) == 0:
        return True
    else:
        return False
    

def is_weird(N):   
    if not is_even(N):
        print("Weird")
    elif is_even(N) and N in range(2, 6):
        print('Not Weird')
    elif is_even(N) and N in range (6,21):
        print('Weird')
    elif is_even(N) and N > 20:
        print ('Not Weird')
    else:
        print('Not Weird')
        
if __name__ == '__main__':
    N = int(input())
    is_weird(N)

Alright, so you have several things you’re doing that make it so you can’t figure this out:

  1. You write this whole thing then try to test it. Instead, write only the first part of the if-statement, confirm that works, then write the next part.
  2. You aren’t adding an else: at the end and this is why you MUST always add an else: in this situation.

Delete this code then do this to start:

def isEven(n):
  return n % 2 == 0

n = 10

if isEven(n):
  print("Is even")
else:
  print("ABORT FAIL!", n)

Do only that. Not anything else until that works. Next, put this in a main() function. Next, add the “and range(2, 6)” part of the first if. Run this 2-3 times after every change trying different values of n to confirm every single change works.

If you are not running your code 2-3 times per change then you are not doing it right. If you find that you blasted out 30 lines of code without running them then delete them. That’s not how to write code. Everyone who is any good at programming runs their code constantly and write their code in tiny steps confirming each one works.

If, for some reason, you think “real programmers blast out 100 lines of code and run it once” you are wrong. I’m a way better programmer than you and the person who wrote this weird challenge and I constantly run my code. I even have code that automatically runs my code on every change that’s how often I run it.

Now, fresh start. Delete this and do it again.

EDIT: Also, you HAVE TO PRINT. I say this in my course one million times, but you cannot know what is going on with code by reading it. YOU HAVE TO PRINT. Do this:

def isEven(n):
  print("isEven n=", n)
  return n % 2 == 0

n = 10

if isEven(n):
  print("Is even")
else:
  print("ABORT FAIL!", n)

See how I added a print that tells you what number you’re dealing with? If you did that at the top of your main and added an else: you’d find your error. You are never ever going to read the code and figure it out. Always print, constantly. Print and run your code constantly.

1 Like

Little off topic, but I just gotta say…thanks for this forum and taking the time to help newer folks.

I’ve started and stopped with this book on a couple occasions and get a little farther each time. Life and frustration get in the way. Whenever I’d go out and ask a question somewhere on the net, many people address you like you’ve got a third eye (or other appendage) growing out of your head.

This has been informative and your collective time and effort is greatly appreciated.

Thanks again…

Ryan

2 Likes

Well thanks. Actually @florian @gpkesley have been doing a great job holding down the fort for me while I work on the JavaScript course, so all the thanks should go to them more than me.

I do adopt a more “direct but not mean” approach in advice. I don’t think anyone is stupid, and never angry at them, but I do find if I’m overly polite they flat ignore what I say. There’s this balancing act where I have to make them read and actually pay attention, but can’t come off as “yelling” or angry. I think the best way I do that is to not say something is good or bad, but just say how to fix it or do it better. The only exception to this is I’ll say something is wrong because…well…that’s what the computer says and you can’t argue with it. The computer is definitely not nice.

You know, you should start a separate thread of “things that make me quit programming”. You might find other people either hit the same thing or have solutions to them.

1 Like

Hi @zedshaw, long time no see :slight_smile: Nice to see that this forum is still very lively.
That advice above was really one of the most valuable things I learned from you!!! Aside of all the other things. More often than not I think when I do programming: “Wow so cool, I really learned that from Zed and can expand on it here!” (last time it was with argparse…)

1 Like