Asserting a function that prints an array [duplicate] - python

This question already has answers here:
Why is "None" printed after my function's output?
(7 answers)
Closed 6 years ago.
I am trying to test this function
def print_board(M):
for i in range (M.shape[0]):
line=''
for j in range (M.shape[1]):
line+=str(int(M[i,j]))
print(line)
I created a new file to test it and imported my file and the function array but I am not too sure on how I can test it since I don't return it, I tried this :
assert(print_board(array([[1,1,1],[0,0,0],[1,1,1]],dtype='bool')) == '''111 000 111''')
but got
AssertionError

What you could do is redirect Python standard output (where print sends it) to a string, and then compare that string.
See this question for an explanation of how to do that.

Related

What is the difference between list1[0] and print(list1[0]) in python? [duplicate]

This question already has answers here:
In Python if you return a string,it will be displayed with quotes around it but If you print the string, it will not be shown with quotes. why? [duplicate]
(2 answers)
In Python what is it called when you see the output of a variable without printing it?
(3 answers)
Closed 2 years ago.
What is the difference between 'python' & python?
We get outputs in different forms for accessing the elements of a list.
Print statement returns output without single quote.
This is the behavior of the python REPL - when using python interactively, the returned value of whatever statement you run (as long as it is not None) gets printed to the console.
Since list1[0] returns the string 'python' and there's no more statement after that, the string gets printed. If you're running the same line in a script (not interactively), then it wouldn't print anything.
print() specifically prints to the console, and will do so regardless of whether python is running in interactive mode or not. It also formats the output to be printed - it doesn't print "what the element is", it prints "what you tell it to print". Which is why the single-quotes aren't there. print() doesn't need to say that it's a string.

This python string sorting works but it shouldn't. Should it? [duplicate]

This question already has answers here:
Python Sort() method [duplicate]
(3 answers)
Closed 2 years ago.
Taking an intro to python course, the following code sorts the string words appropriately, but the function does not return sortString, so I'm not understanding why the output is correct. Can you please help me understand?
def sort_words(string):
splitString = string.split()
sortString = splitString.sort()
return splitString
print(sort_words('python is pretty cool'))
Python .sort() returns None, much like print() does. list.sort() works in place - meaning the list is sorted without needing to assign it to another variable name.

New to Python, trying to do print statements. College textbook is super short not being helpful [duplicate]

This question already has answers here:
How can I print variable and string on same line in Python? [duplicate]
(18 answers)
Closed 2 years ago.
Ok so I'm working on a problem in my python class. I've gotten most of it figured out aside from print statements. I assigned the arguements correctly (I think) and am just trying to get the text to print correctly on the terminal side. What am I doing wrong here?
here is what I currently have
is the example that mine is supposed to look similar to
you could use f string print statements too like this:
print(f"distance in knots is: {distance_in_knots}")

re.sub and re.subn [duplicate]

This question already has answers here:
How to check if re.sub() has successfully replaced in python? [duplicate]
(2 answers)
Python: how to substitute and know whether it matched
(3 answers)
Closed 2 years ago.
I want to use re.subn in my program to get no of times changes are made but I can't figure it out how to write the code. I have written the program with re.sub and it works fine but If I use (re.sub(r'\bi\b','I',contents)) in this program it shows this error.
TypeError: write() argument must be str, not int
import re
with open ('Sample.txt','r') as rf:
contents=rf.read()
change=(re.sub(r'\bi\b','I',contents))
with open ('Sample2.txt','w') as wf:
wf.writelines(change)
Can someone please guide how can I use re.subn to get the count.
Thank you.

How to get help on methods in Python? [duplicate]

This question already has answers here:
python: how to get information about a function?
(5 answers)
Closed 8 years ago.
whenever I use help() on a method I get a weird looking output.
For example, help(obj.readline) gives me the following output:
Help on built-in function readline:
readline(...)
That's all i get. I don't know how to interpret it.Why there is no description about the method? Can somebody please explain to me what's going on?
Is this weird output?
>>> f = open('data.txt')
>>> help(f.readline)
Help on built-in function readline:
readline(...)
readline([size]) -> next line from the file, as a string.
Retain newline. A non-negative size argument limits the maximum
number of bytes to return (an incomplete line may be returned then).
Return an empty string at EOF.

Categories

Resources