0

So I have assignment that I completed but there's one last step where the print just says none. Here's my code

#Copy the definition of function print_chars below
def print_chars(multiples, char):
    print_chars= multiples* char
    print (print_chars)

#Copy the definition of function sum_arithmetic_seq below
def sum_arithmetic_seq(n):
    return n* (n+1)//2


#Copy the definition of function factorial below
import math
def factorial(n):
    return math.factorial(n)

#Here's my program
for N in range(1,7,1):
    print(N)
    print('sum:', print_chars(sum_arithmetic_seq(N) ,'*'))
    print('factorial:', print_chars(factorial(N),'#'))

The output would end up like this (I'm just going to put part of it because it's long.)

1
*
sum: None
#
factorial: None

How it's supposed to be:
1
sum: *
factorial: #
Davis Thuy
  • 5
  • 2
  • 5

2 Answers2

1

Print_chars doesn't return anything. Make it return what you are printing so that you can use it's output. In your last print, it can't utilize the value because there is nothing there. Change print to return to fix it.

Zizouz212
  • 4,908
  • 5
  • 42
  • 66
0

Working Solution :
for N in range(1,7,1): print(N) print('sum:', end=" ") print_chars(sum_arithmetic_seq(N) ,'*')) print('factorial:', end=" ") print_chars(factorial(N),'#'))

Reason for unintended format:
The problem is, you're print_chars function already has a print statement inside the function. What this means is,

when you make the call statement,

print('sum:', print_chars(sum_arithmetic_seq(N) ,'*'))

The function print_chars will be evaluated first which will print *{n times} and then after the function executes "Sum:" get's printed. Which is why the start * get printed before "sum"

To avoid this,


1. Separate the print statement to "sum" and "factorial" beforehand

2. simply use print_chars(.....) don't have a print(print_char)) then you're asking for a return value to be printed. which is not returned
3. Or simply change the print (print_chars) to a return(print_chars) inside the function

Imraaz Rally
  • 104
  • 1
  • 7
  • Values written to sys.stdout can't be used... There's nothing wrong with printing, but Python needs to use the value later, hence the need for a return statement. – Zizouz212 Apr 10 '15 at 02:48
  • You can have print and return statements in the same function. – Zizouz212 Apr 10 '15 at 02:49
  • Yes, I agree with you. I posted the solution like this so that he understands why the **** printed before "sum" and the "NONE" was printing. Mainly, to illustrate the functions inside the function call must be executed first. – Imraaz Rally Apr 10 '15 at 02:50
  • I updated my comment to also illustrate that you can simply change the print() to return(). thank you – Imraaz Rally Apr 10 '15 at 02:54