0

Its a very noob question, I am trying my hands on python and going through this exercise right now. Their solution suggests it could actually be done without creating any function which makes it easier, however, I am still wondering why my function wouldn't work.

Here's what my code looks like:

    1 number = int(input("Please enter a number of your choice: "))
CC  2 def odd_or_even():
    3     if number % 2 == 0:
    4         print("You've entered an Even number :)")
    5     else:
    6         print("You've enterd an Odd number.")
    7
    8     return odd_or_even()

Also, you'll notice I have a warning on second line which reads:

oddOrEven.py|2 col 1 C| E302 expected 2 blank lines, found 0 [pep8]

in my vim. Can someone explain what exactly's wrong there?

martineau
  • 119,623
  • 25
  • 170
  • 301
Nishant
  • 31
  • 5
  • 4
    Your function recurses infinitely. – Barmar Jun 06 '18 at 22:05
  • I was able to fix it by removing "return" and simply calling "odd_or_even()". Is that the ideal solution? – Nishant Jun 06 '18 at 22:06
  • 1
    @Nishant: Far from ideal; for example, it should take `number` as a parameter, as opposed to using a global variable. – Scott Hunter Jun 06 '18 at 22:07
  • Can I add code in comments? I removed the global variable and made it look like "odd_or_even(int(input("Please enter a number of your choice: "))) ". Is that what you were referring to @Scott – Nishant Jun 06 '18 at 22:11
  • Look at @HunterM267's answer. – Scott Hunter Jun 06 '18 at 22:12
  • It's better to ask a new question—changing the code in an already posted question (as a result of other's comments) generally is not a good idea because it changes the question. – martineau Jun 06 '18 at 22:13

3 Answers3

0
  1. Your function odd_or_even calls itself at the last line, which means it will never return anything.
  2. That hasn't caused a problem (yet), because you never call odd_or_even from anywhere else.
Scott Hunter
  • 48,888
  • 12
  • 60
  • 101
0

You need to pass the number variable into your odd_or_even function. You may also want to call the function.

number = int(input("Please enter a number of your choice: "))   
odd_or_even(number) #call function with number variable

def odd_or_even(number):   #define function and pass number parameter                                                       
   if number % 2 == 0:                                                         
      print("You've entered an Even number :)")                               
   else:                                                                       
      print("You've enterd an Odd number.")                                                                                                           
   return #end function

expected two blank lines pep8 warning in python should answer your question relating to line 2.

HunterM267
  • 199
  • 1
  • 12
0

1st: I would write your function definition above the code that calls it.
2nd: In line 8, the statement: return odd_or_even() makes it a recursive function. It can be fixed by removing the call to your function.

Like this:

def odd_or_even(number):
     if number % 2 == 0:
          print("You've entered an Even number :)")
     else:
          print("You've enterd an Odd number.")
     return 

number = int(input("Please enter a number of your choice: "))
odd_or_even(number)
Community
  • 1
  • 1
SDET
  • 26
  • 2
  • Use formatting tools to make your post more readable. Code block should look like `code block`. Use **bold** *italics* if needed. – Morse Jun 06 '18 at 23:00