0
votes

I am using parameterized functions in python. And I am passing some string value and access its value.

This is how my function definition looks like:

import constants

def print_error_table(self, header1, errormsg, icon):
        # for header1
        print(constants.GREEN)
        print(constants.REPORT_HEADER_ERR)
        print(constants.DATE_TIME)
        print(constants.SPACE)
        start = "|   |           "+constants.ICON_BOX

        header1_count = len(header1)

        available_space_for_first_part = 37 - (len(start) + header1_count)

        s = ""
        print(constants.REPORT_ROOF)
        print(constants.REPORT_COLUMNS)
        print(constants.REPORT_FLOOR)
        print(constants.REPORT_MIDDLE)
        print(start),
        print(" "+constants.NC+header1+constants.GREEN),

        for i in range(available_space_for_first_part):
             s += " "

        print(s),
        print("|"),

        # right part
        end = "                "
        end2 = "|    |"
        s2 = ""
        icon_count = len(icon)
        available_space_for_second_part = 31 - (len(end) + icon_count)
        print(end),
        print(icon),
        for i in range(available_space_for_second_part):
            s2 += " "
        print(s2),
        print(end2)

        print(constants.REPORT_SHORT_HORIZONTAL_LINE)
        print(constants.REPORT_MIDDLE_NO_MIDDLE_SEPTUM)


        # print(len(constants.SPACE)) # 84

        # print first 40 characters
        start = "|   |"
        print(start),
        s3 = ""

        for i in range(12):
            print(" "),
        print(constants.RED),
        msg = "1.) " + errormsg,
        print(""+msg), # this is where my error is getting

        print(constants.GREEN),

        # print(constants.RED+constants.ICON_CROSS+msg+constants.GREEN),

        for i in range(12, 57 - len(msg)-1):
            s3 += " "
        print(s3),
        print("|    |"),
        print("")
        print(constants.REPORT_MIDDLE_NO_MIDDLE_SEPTUM)
        print(constants.REPORT_MIDDLE)
        print(constants.REPORT_FLOOR)
        print(constants.REPORT_FOOTER)

And this is my other python file where I am calling this function from.

error_message = "\"etcd\" is impaired\n"
print_error_table(self, "ETCD", error_message, constants.ICON_CROSS)

And I get this error:

File "/home/jananath/Desktop/python-script/2/bitesizetrouble/report_error.py", line 57, in print_error_table print(""+msg), TypeError: cannot concatenate 'str' and 'tuple' objects

The problem is the value I am passing (i.e error_message) is not passing as a string it is some kind of an altered text for some reason.

I am telling this because, in above (first) command, where it says print(""+str(msg)), instead of this, when I try print(msg), it gives some weird output like this.

(`"etcd" is impaired\n',)

You can see two parenthesis are there on each side. Where does it come from and why I cannot concatanate the string I am passing to the function with another string (i.e print(""+str(msg))

update: I am using , to stop print() printing a new line. This is the way I should do that in python 2.7.5

1
I notice that a lot of your code's statements end with a comma. If you're under the impression that it behaves like a semicolon in languages such as C or javascript, that's not the case. A trailing comma typically turns the object preceding it into a tuple. Try deleting all of them, in particular the one at the end of msg = "1.) " + errormsg,, and see if that helps.Kevin
If you are only just learning Python, you should probably abandon Python 2 and concentrate on learning the currently recommended and supported version of the language, which is Python 3.tripleee
@Kevin I am using , to stop print() printing a new line. This is the way I should do that in python 2.7.5Jananath Banuka
@tripleee, no I am using python 2.7.5 for reason. I can't change back to the newest version.Jananath Banuka
The trailing comma is specific to print but you have sprinkled it in a lot of places where it doesn't do that. It creates a tuple and now you are annoyed that you have a tuple.tripleee

1 Answers

3
votes

Your code is full of commas and you put one more in the wrong place.

You create a tuple with one element by adding a comma to the end of string assignment. See:

>>> foo = "bar"
>>> print(foo)
bar
>>> foo = "bar",
>>> print(foo)
('bar',)

I would recommend you to start with Python tutorial at python.org (https://docs.python.org/3/tutorial/index.html). Seriously. And do not learn Python 2, it's no more. Seriously.