简体   繁体   English

如何在Python中组合变量以形成函数?

[英]How do you combine variables to form function in python?

ypos = 1
ypos2 = ypos-1
xpos = 1
xpos2 = 2

Using these variables, that are changed each time the code loops, how do I get it to print the result below? 使用这些变量,这些变量在每次代码循环时都会更改,我如何获取它以在下面显示结果?

=B(ypos)-B(ypos2)

Every time the code loops the ypos increases by one and therefore the ypos2 increases by one 每次代码循环,ypos增加1,因此ypos2增加1。

so as the code loops the results should be 1) B1-B0 2) B2-B1 3) B3-B2 etc. 因此代码循环的结果应该是1)B1-B0 2)B2-B1 3)B3-B2等。

Any help is much appreciated, and if you don't understand the question let me know, I wasn't quite sure how to explain it.(PS I am using python2.7) 非常感谢您的帮助,如果您不明白这个问题,请告诉我,我不太确定该如何解释。(PS我正在使用python2.7)

Thanks, Anthony 谢谢安东尼

print('B({})-B({})'.format(ypos, ypos2))

or 要么

print('B(' + str(ypos) + ')-B(' + str(ypos2) + ')')

These should work in Python 2.7 or Python 3.x. 这些应该可以在Python 2.7或Python 3.x中使用。

This should be it: 应该是这样:

print 'B({})-B({})'.format(ypos, ypos2)

For python2.7 prints are without parentheses, unlike in python3. 与python3不同,对于python2.7,打印没有括号。 Check this to verify link 选中此以验证链接

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

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