Why i getting None In addition to the Output? [duplicate] - python-3.x

This question already has answers here:
Why is "None" printed after my function's output?
(7 answers)
Closed 4 years ago.
I have defined a function as follows:
def lyrics():
print "The very first line"
print lyrics()
However why does the output return None:
The very first line
None

Because there are two print statements. First is inside function and second is outside function. When a function doesn't return anything, it implicitly returns None.
Use return statement at end of function to return value.
e.g.:
Return None.
>>> def test1():
... print "In function."
...
>>> a = test1()
In function.
>>> print a
None
>>>
>>> print test1()
In function.
None
>>>
>>> test1()
In function.
>>>
Use return statement
>>> def test():
... return "ACV"
...
>>> print test()
ACV
>>>
>>> a = test()
>>> print a
ACV
>>>

Because of double print function. I suggest you to use return instead of print inside the function definition.
def lyrics():
return "The very first line"
print(lyrics())
OR
def lyrics():
print("The very first line")
lyrics()

Related

How to get number values without double quotes in list using python

I'm having lists as shown below.
l = ['22','abc','znijh09nmm','928.2','-98','2018-01-02']
I want those to insert in MySQL using Python, but I want it to get the output as:
l = [22,'abc','znijh09nmm',928.2,-98,2018-01-02]
You could use something like this to convert integers and floats in the list.
mylist = ['22','abc','znijh09nmm','928.2','-98','2018-01-02']
def convert(value):
if str(value).isdigit():
return int(value)
else:
try:
float(value)
return float(value)
except ValueError:
return value
mylist = [convert(i) for i in mylist]
print(mylist)
[22, 'abc', 'znijh09nmm', 928.2, -98.0, '2018-01-02']

Function with input as a list that doesn't give me :takes 1 positional argument but 3 were given. Error

I need to create a function that allows for a list for the input, then breaks apart list and prints them each separately:
def clas(list_):
x=list_
for n in x:
print (x)
I had it working with something similar to this for the first part. clas('1','2','bob') output would be:
'1'
'2'
'bob'
There are several ways to do this, here are a couple:
def clas(my_list):
for item in my_list:
print(item)
the_list = [1, 2, 'Bob']
clas(the_list)
The print function itself is capable of handling this:
def clas(my_list):
print(*my_list, sep='\n')
If you really want to invoke it as:
clas(1, 2, 'Bob')
Then we can do:
def clas(*args):
for item in args:
print(item)
Or:
def clas(*args):
print(*args, sep='\n')

TypeError 'int'

I'm trying to make a function the returns all possible permutations for a list of numbers for example:
List= [1,2,3]
[[1,2,3], [1,3,2], [2,1,3], [2,3,1], [3,1,2],[3,2,1]]
however I keep running into the same error
TypeError: 'int' object is not callable
in this line
return permutations(result,variable,List,permutations)
the rest of the code is
def permutationsaux(List):
if List==[]:
return []
else:
return permutations([List],0,List,countpermutations(List))
def permutations(result,variable,List,permutations):
if len(result)==permutations:
return result
elif len(result[variable])==len(List):
result.append([])
variable=variable+1
return permutations(result,variable,List,permutations)
return permutations(result[variable]+reorderlist(List),variable+1,reorderlist(lista),permutations)
def countpermutations(List):
if List==[]:
return 1
return len(List)*countpermutations(List[1:])
def reorderlist(List):
temp=List[len(List)-2]
List[len(List)-2]=LIst[len(List)-1]
List[len(List)-1]=temp
return List
Your function "permutations" has an argument named "permutations". Guess which one is in scope within the function.

print recursive pattern without quotes in python

my code for pattern:
def pattern(n):
if n==1:
return '1'
else:
return pattern(n-int(n/2))*2+str(n)
print(pattern(n))
i need:
>>> pattern(1)
1
>>> pattern(2)
112
>>> pattern(4)
1121124
>>> pattern(8)
112112411211248
but i get:
>>> pattern(1)
'1'
>>> pattern(2)
'112'
>>> pattern(4)
'1121124'
>>> pattern(8)
'112112411211248'
i have tried a lot but nothing is working to get rid of those pesky quotes.
The quotes are from the REPL printing the representation of the result of the function call, which is a string. If you do not want the representation then just print the result explicitly instead.

Showing that function for IF statement does not work the same way as IF statement

This is a question for a homework problem that I cant figure out:
Question Beginning
Q3. Let's try to write a function that does the same thing as an if statement:
def if_function(condition, true_result, false_result):
"""Return true_result if condition is a true value, and false_result otherwise."""
if condition:
return true_result
else:
return false_result
This function actually doesn't do the same thing as an if statement in all cases. To prove this fact, write functions c, t, and f such that one of these functions returns the number 1, but the other does not:
def with_if_statement():
if c():
return t()
else:
return f()
def with_if_function():
return if_function(c(), t(), f())
Question End
Heres what I figured out:
with_if_statement() does not evaluate f() if c() is true, but with_if_function() evaluates all 3 before checking if c() is true or not.
So, I thought of assigning a global variable in c(), and changing its value in f()
heres my code (which does not work):
def c():
try:
global x
except NameError:
x=1
if x==1:
return True
else:
return False
def t():
if x==1:
return (1)
else:
return (0)
def f():
global x
x=2
if x==1:
return (1)
else:
return (0)
can anyone help me figure out the answer? Thanks..!
The global statement shouldn't throw a NameError (and so you won't run x=1 in c()). I would try rewriting your code without using exceptions, they won't be necessary to solve this and are making it more complicated than it needs to be. Using a global variable and having side effects in your functions is certainly the right track.
def if_function(condition, true_result, false_result):
"""Return true_result if condition is a true value, and
false_result otherwise.
>>> if_function(True, 2, 3)
2
>>> if_function(False, 2, 3)
3
>>> if_function(3==2, 3+2, 3-2)
1
>>> if_function(3>2, 3+2, 3-2)
5
"""
if condition:
return true_result
else:
return false_result
def with_if_statement():
"""
>>> with_if_statement()
1
"""
if c():
return t()
else:
return f()
def with_if_function():
return if_function(c(), t(), f())
The question requires that, write 3 functions: c, t and f such that with_if_statement returns 1 and with_if_function does not return 1 (and it can do anything else)
At the beginning, the problem seems to be ridiculous since, logically, with_if_statement and with_if_function are same. However, if we see these two functions from an interpreter view, they are different.
The function with_if_function uses a call expression, which guarantees that all of its operand subexpressions will be evaluated before if_function is applied to the resulting arguments. Therefore, even if c returns False, the function t will be called. By contrast, with_if_statement will never call t if c returns False (This paragraph from the UCB website).
def c():
return True
def t():
return 1
def f():
'1'.sort() # anything breaks the program is OK

Resources