Davis Thuy
Davis Thuy

Reputation: 5

Python printing none

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: #

Upvotes: 0

Views: 2974

Answers (2)

Imraaz Rally
Imraaz Rally

Reputation: 104

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

Upvotes: 0

Zizouz212
Zizouz212

Reputation: 4998

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.

Upvotes: 1

Related Questions