Why is the print statement printing None at the end
Question:
def empty(new):
new1=''
for i in new:
new1=i+new1
print(new1)
print(new1)
new='First question'
print(new)
print(empty(new))
output:
First question
F
iF
riF
sriF
tsriF
tsriF
q tsriF
uq tsriF
euq tsriF
seuq tsriF
tseuq tsriF
itseuq tsriF
oitseuq tsriF
noitseuq tsriF
noitseuq tsriF
None
Question:
why do i get none at the end???
Answers:
I think what you want is to replace print(empty(new))
with empty(new)
. In the first case you are printing the return value of the call to the function empty
, which in this case is None
because the function doesn’t have a return
statement.
You can either add a return
to the function:
def empty(new):
new1=''
for i in new:
new1=i+new1
print(new1)
return new1
new='First question'
print(new)
print(empty(new))
Or call function not in print
:
def empty(new):
new1=''
for i in new:
new1=i+new1
print(new1)
print(new1)
new='First question'
print(new)
empty(new)
def empty(new):
new1=''
for i in new:
new1=i+new1
print(new1)
print(new1)
new='First question'
print(new)
print(empty(new))
output:
First question
F
iF
riF
sriF
tsriF
tsriF
q tsriF
uq tsriF
euq tsriF
seuq tsriF
tseuq tsriF
itseuq tsriF
oitseuq tsriF
noitseuq tsriF
noitseuq tsriF
None
Question:
why do i get none at the end???
I think what you want is to replace print(empty(new))
with empty(new)
. In the first case you are printing the return value of the call to the function empty
, which in this case is None
because the function doesn’t have a return
statement.
You can either add a return
to the function:
def empty(new):
new1=''
for i in new:
new1=i+new1
print(new1)
return new1
new='First question'
print(new)
print(empty(new))
Or call function not in print
:
def empty(new):
new1=''
for i in new:
new1=i+new1
print(new1)
print(new1)
new='First question'
print(new)
empty(new)