My question was why the text "First one ended..." and "Second one ended..." appear when the first two functions are run separately, but not when the function combined() runs.
It looks like you're running your code at the interactive prompt (either in IDLE or on the command line). At the interactive prompt, when you run a function and don't assign its return value to anything, the return value is printed to the screen. If you do assign its return value to a variable, or if the function isn't top-level, the return value is not printed to the screen. For example:
>>> def foo():
return 3
>>> def bar():
foo()
>>> foo() # not assigned to anything -> prints return value to output
3
>>> bar() # not top-level -> doesn't print return value to output
>>> x = foo() # assigned to x -> doesn't print return value to output
This is a quirk of the interactive prompt. You will not get the same results if e.g. you add the line powers_of_9() to the end of the file and then run it. When you do that, your results look like this:
Powers function for 9 is:
Powers Function with 9 entered:
power of -3 = 0.0013717421124828533
power of -2 = 0.012345679012345678
power of -1 = 0.1111111111111111
power of 0 = 1
power of 1 = 9
power of 2 = 81
power of 3 = 729
As you can see, the return value of powers_of_9 is not printed.
In any case, the way you are using the return statement is not correct. You should use return only when you want to extract some information from a function for use elsewhere. For example, like this silly example:
def add(x, y):
return x+y
def print_two_plus_three():
result = add(2, 3)
print(result)
When you just want to display some information, print is what you should use.
Also, I would like to offer some commentary on your code. To achieve the results you want, your code should probably look something like this:
def powers_of_x(x):
print('Powers function with', x, 'entered:')
for n in range(-3, 4, 1):
print('power of', n, '=', x**n)
print('First one ended (using variables with defined range)')
def powers_of_9():
print('Powers function for 9 is:')
powers_of_x(9)
print('Second one ended (no passed variable here)')
def combined():
x = int(input('Enter value to start: '))
print('First, run powers_of_x function for entered value of:', x)
powers_of_x(x)
print('Second, run powers_of_9 function for powers of 9:')
powers_of_9()
print('All now finished')
- When you write
print('abc', 'def', 'ghi'), the printed string is abc def ghi. That is, each argument to print is separated by a space. You can alter this behavior by writing e.g. print('abc', 'def', 'ghi', sep='X'), where the sep keyword argument specifies what string should be used to separate arguments. As you might guess, this prints abcXdefXghi.
- As I commented, it is considered bad style to parenthesize
return values (unless you're returning a tuple, of course).
- It is also considered bad style to put a space between the function name and the list of arguments. i.e. use
print('foo'), not print ('foo').
- It is nice to put a space at the end of the argument to
input(), so that the inputted values are separated from the input prompt.
return 'some string'thanreturn ('some string').