Python Forum
How to print the output of a defined function
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to print the output of a defined function
#1
How to print the output of make_matrix function defined in the following code:

from typing import List, Callable
Matrix = List[List[float]]


def make_matrix(num_rows: int,
                num_cols: int,
                entry_fn: Callable[[int, int], float]) -> Matrix:
    """Returns a num_rows x num_cols matrix whose (i,j)-th entry is entry_fn(i, j) """
    return [[entry_fn(i, j)  # given i, create a list
            for j in range(num_cols)]  # [entry_fn(i, 0), ... ]
            for i in range(num_rows)]  # create one list for each i
Larz60+ write Sep-07-2022, 08:05 PM:
As mentioned by menator01 in next post:
Please post all code, output and errors (it it's entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.
Fixed for you this time.
Reply
#2
Please use tags when posting code.

To print out a return from a function you can do this

def myfunc(arg1, arg2, arg3):
    return f'Arg1 -> {arg1}, Arg2 -> {arg2}, List of args -> {arg3}'

print(myfunc('Argument 1', 'Argument 2',[1,2,3,4,5]))
Output:
Arg1 -> Argument 1, Arg2 -> Argument 2, List of args -> [1, 2, 3, 4, 5]
I welcome all feedback.
The only dumb question, is one that doesn't get asked.
My Github
How to post code using bbtags


Reply
#3
What's wrong with using print?
from typing import List, Callable

Matrix = List[List[float]]


def make_matrix(
    num_rows: int,
    num_cols: int,
    entry_fn: Callable[[int, int], float] = lambda *args: 0,
) -> Matrix:
    """Returns a num_rows x num_cols matrix whose (i,j)-th entry is entry_fn(i, j)"""
    return [
        [
            entry_fn(i, j) for j in range(num_cols)  # given i, create a list
        ]  # [entry_fn(i, 0), ... ]
        for i in range(num_rows)
    ]  # create one list for each i


print(make_matrix(3, 5))
Output:
[[0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0]]
Or are you looking for something pretty? This is easy:
for row in make_matrix(3, 5):
    print(row)
Output:
[0, 0, 0, 0, 0] [0, 0, 0, 0, 0] [0, 0, 0, 0, 0]
But it will likely be ragged.
for row in make_matrix(3, 5, lambda x, y: "X"*(x+y))):
    print(row)
Output:
['', 'X', 'XX', 'XXX', 'XXXX'] ['X', 'XX', 'XXX', 'XXXX', 'XXXXX'] ['XX', 'XXX', 'XXXX', 'XXXXX', 'XXXXXX']
There are packages that will print a pretty table.
from tabulate import tabulate

print(tabulate(make_matrix(3, 5, lambda x, y: "X" * (x + y)), tablefmt="fancy_grid"))
Output:
╒════╤═════╤══════╤═══════╤════════╕ │ │ X │ XX │ XXX │ XXXX │ ├────┼─────┼──────┼───────┼────────┤ │ X │ XX │ XXX │ XXXX │ XXXXX │ ├────┼─────┼──────┼───────┼────────┤ │ XX │ XXX │ XXXX │ XXXXX │ XXXXXX │ ╘════╧═════╧══════╧═══════╧════════╛
Or you could use numpy.
Reply
#4
Hi deanhystad,

Thank you very much for your response. How is the following response, for example?
from typing import List, Callable
Matrix = List[List[float]]


def make_matrix(num_rows: int,
                         num_cols: int,
                         entry_fn: Callable[[int, int], float]) -> Matrix:

      """Returns a num_rows x num_cols matrix whose (i,j)-th entry is entry_fn(i, j) """
     return [[entry_fn(i, j)  # given i, create a list
                for j in range(num_cols)]  # [entry_fn(i, 0), ... ]
                for i in range(num_rows)]  # create one list for each i


print("X=", make_matrix(2, 3, lambda i, j: input("X["+str(i)+', '+str(j)+"]= ") if i != j else 1))
Sample output:
Output:
X[0, 1]= 2 X[0, 2]= 3 X[1, 0]= 4 X[1, 2]= 5 X= [[1, '2', '3'], ['4', 1, '5']]
Of course, this is not what I want to do!
Larz60+ write Sep-08-2022, 02:35 PM:
Please post all code, output and errors (it it's entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.
Reply
#5
I might answer your question if you start wrapping posted code in Python tags so it can be read.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  print doesnt work in a function ony 2 312 Mar-11-2024, 12:42 PM
Last Post: Pedroski55
  Variable is not defined error when trying to use my custom function code fnafgamer239 4 601 Nov-23-2023, 02:53 PM
Last Post: rob101
  problem in output of a function akbarza 9 1,211 Sep-29-2023, 11:13 AM
Last Post: snippsat
  Printing the variable from defined function jws 7 1,328 Sep-03-2023, 03:22 PM
Last Post: deanhystad
  Python VS Code: using print command twice but not getting output from terminal kdx264 4 1,106 Jan-16-2023, 07:38 PM
Last Post: Skaperen
  How to print variables in function? samuelbachorik 3 916 Dec-31-2022, 11:12 PM
Last Post: stevendaprano
  Getting NameError for a function that is defined JonWayn 2 1,121 Dec-11-2022, 01:53 PM
Last Post: JonWayn
  How to output one value per request of the CSV and print it in another func? Student44 3 1,351 Nov-11-2022, 10:45 PM
Last Post: snippsat
Question Help with function - encryption - messages - NameError: name 'message' is not defined MrKnd94 4 2,912 Nov-11-2022, 09:03 PM
Last Post: deanhystad
  User-defined function to reset variables? Mark17 3 1,660 May-25-2022, 07:22 PM
Last Post: Gribouillis

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020