Printing the results of my function

python print variable
python print() function
python print function arguments
python print example
python print format
python 3 print format
python print arguments
print statement in python

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.")
    8     return odd_or_even()

Also, you'll notice I have a warning on second line which reads:|2 col 1 C| E302 expected 2 blank lines, found 0 [pep8]

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

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 :)")
          print("You've enterd an Odd number.")

number = int(input("Please enter a number of your choice: "))

What is the formal difference between "print" and "return"?, What is the difference between the print function and the return statement? Python | Output using print() function The simplest way to produce output is using the print() function where you can pass zero or more expressions separated by commas. This function converts the expressions you pass into a string before writing to the screen.

  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.

Python print() Function, the specified message to the screen, or other standard output device. The message can be a string, or any other object, the object will be converted into a string before written to the screen. Another benefit of print () being a function is composability. Functions are so-called first-class objects or first-class citizens in Python, which is a fancy way of saying they’re values just like strings or numbers. This way, you can assign a function to a variable, pass it to another function,

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 :)")                               
      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.

Python return statement, As you just saw, calling print() without arguments results in a blank line, which is a line comprised solely of the newline character. Don't confuse  Here are three ways to display multiple variable values on the same line in the Command Window. Concatenate multiple character vectors together using the [] operator. Convert any numeric values to characters using the num2str function. Use disp to display the result.

Your Guide to the Python print() Function – Real Python, First thing to note is that return and print are statements, not functions, but that is just In this example, we pass the results of a function to another function. Below are the top 5 most common reasons why formula text is shown in cell instead of its results. They are sorted by frequency with most popular at the top. They apply to Excel 2013 and Excel 2016. Make sure that there is an equal sign before the formula. Check that you typed formula with no quotes or apostrophes at beginning or end.

What's the difference between the print and the return functions , For example the math module contains a function called pow which takes two arguments, the base and the print("The result of", toSquare, "squared is", result)​. It is a generic function which means that new printing methods can be easily added for new class es.

6.2. Functions that Return Values, Thus actually two different things are printed in your command window: the results of evaluating that line, and; the output of the function. In this tutorial we look at functions to print values on R console or file.. The values are normally printed on the console since that is the default sink, but you can specify that the output be sent to a file by opening up a sink to the file. we will see an example of it in this tutorial.

  • Your function recurses infinitely.
  • I was able to fix it by removing "return" and simply calling "odd_or_even()". Is that the ideal solution?
  • @Nishant: Far from ideal; for example, it should take number as a parameter, as opposed to using a global variable.
  • 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
  • Look at @HunterM267's answer.
  • Use formatting tools to make your post more readable. Code block should look like code block. Use bold italics if needed.