简体   繁体   English

制作一个包含其他变量的变量

[英]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.在此先感谢您的帮助。

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: Output:

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

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!祝你好运!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM