我试图在我的代码中构造一些求和金字塔,但是我无法在结束后打印任何东西=(在python 2.7中)
func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
var visibleRect = CGRect()
visibleRect.origin = myCollectionView.contentOffset
visibleRect.size = myCollectionView.bounds.size
let visiblePoint = CGPoint(x: visibleRect.midX, y: visibleRect.midY)
let visibleIndexPath: IndexPath = myCollectionView.indexPathForItem(at: visiblePoint)!
print(visibleIndexPath)
fruitArray.remove(at: visibleIndexPath.row )
myCollectionView.deleteItems(at: [visibleIndexPath])
myCollectionView.scrollToItem(at: IndexPath.init(row: visibleIndexPath.row-1, section: 0), at: UICollectionViewScrollPosition.centeredHorizontally, animated: false)
}
我得到的输出是:
from __future__ import print_function
import time
YEAR_STR= time.strftime('%Y')
MONTH_STR=time.strftime('%m')
num = 1
for i in range(0, 5):
num = 1
for j in range(0, i+1):
print("(ABC_"+YEAR_STR+MONTH_STR+str(num), end="+")
num = num + 1
print()
但预期的输出是:
(ABC_2017031+
(ABC_2017031+(ABC_2017032+
(ABC_2017031+(ABC_2017032+(ABC_2017033+
(ABC_2017031+(ABC_2017032+(ABC_2017033+(ABC_2017034+
(ABC_2017031+(ABC_2017032+(ABC_2017033+(ABC_2017034+(ABC_2017035+
依旧......
我无法在print语句中添加最后的)/ num 。可以这样做吗?
答案 0 :(得分:2)
你可以自己这样做:
只有当它不是正在打印的行中的最后一个字符时才会添加“+”
import time
YEAR_STR= time.strftime('%Y')
MONTH_STR=time.strftime('%m')
for i in range(0, 5):
num = 0
tmp = ""
for j in range(0, i+1):
num = num + 1
tmp += "ABC_"+YEAR_STR+MONTH_STR+str(num)
if (j < i):
tmp+="+"
print("(%s)/%d"% (tmp, num))
输出:
(ABC_2017031)/1
(ABC_2017031+ABC_2017032)/2
(ABC_2017031+ABC_2017032+ABC_2017033)/3
(ABC_2017031+ABC_2017032+ABC_2017033+ABC_2017034)/4
(ABC_2017031+ABC_2017032+ABC_2017033+ABC_2017034+ABC_2017035)/5
答案 1 :(得分:0)
for i in range(5):
print("+".join(["(ABC_"+YEAR_STR+MONTH_STR+")" for num in range(0, i+1)]), end="/{}\n".format(i+1))
选项实际上只是通过那种方式完成字符串。你真的想要这样的东西:
{{1}}