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?
odd_or_even
calls itself at the last line, which means it will never return anything. odd_or_even
from anywhere else. 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.
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.
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)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.