'Making a variable that includes other variables

I am new to coding and am struggling to make a variable that contains a lot of code. This is what I have been trying to do:

outputMSG = \nOvertime Hours:",format(OThours,',.2f'),'@ $' +format(OTrate, ',.2f'),'= $'\
                  +format(OTtotal,',.2f'),
            "\nRegular Hours:",format(regHours,',.2f'),'@ $' +format(rate,',.2f'),'= $'\
                  +format(regTotal,',.2f')

Whenever I print this, it does not format correctly and just shows all of the code above. I think my issue is using commas wrong because when tested on a smaller scale that was the issue, but I dont know how to fix it. Thanks in advance for any help.



Solution 1:[1]

The original code was missing some quotation marks. And it was not runnable code because of missing variables.

The following code is runnable with variables assigned constant values.

I hope this provides insight on providing Minimal Runnable Example for future questions.

OThours = 20
OTrate = 11.11
OTtotal = OThours * OTrate
regHours = 35
rate = 5.50
regTotal = regHours * rate

outputMSG = "Overtime Hours:" + format(OThours,',.2f') + '@ $' + format(OTrate, ',.2f') + '= $'\
                  + format(OTtotal,',.2f')\
            + "Regular Hours:" + format(regHours,',.2f') + '@ $' +format(rate,',.2f') + '= $'\
                  + format(regTotal,',.2f')

print(outputMSG)

Output:

Overtime Hours:20.00@ $11.11= $222.20Regular Hours:35.00@ $5.50= $192.50

Solution 2:[2]

Fixed equation:

outputMSG = "Overtime Hours:" + format(OThours,',.2f') + '@ $' + format(OTrate, ',.2f') + '= $'\
                  + format(OTtotal,',.2f')\
            + "Regular Hours:" + format(regHours,',.2f') + '@ $' +format(rate,',.2f') + '= $'\
                  + format(regTotal,',.2f')

KISS

keep it simple silly

Sometimes, code like this is hard to read, causing some syntax errors. Next time, try using more variables instead of directly embedding equations, or using classes.

Good Luck! ?

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Carl_M
Solution 2 cooprofessor