我制作的程序有点问题。我正在让它显示钻石,但我遇到了问题,这是我的代码:
a = input("Enter width: ")
a = int(a)
b = a
for i in range(a):
i = i + 1
b = a - i
text = " " * b + " " + "* " * i
print(text[:-1])
for i in range(a):
i = i + 1
b = a - i
text = " " * i + " " + "* " * b
print(text[:-1])
感谢大家的帮助!这就是答案
最佳答案
那是因为print
不返回字符串,它返回None
。
>>> print(print("foo"))
foo
None
也许您想这样做:
text = " " * i + " " + "* " * b
print (text[:-1])
要更好地删除尾随空格,请使用 str.rstrip
:
>>> "foo ".rstrip()
'foo'
关于 str.rstrip
的帮助:
>>> print (str.rstrip.__doc__)
S.rstrip([chars]) -> str
Return a copy of the string S with trailing whitespace removed.
If chars is given and not None, remove characters in chars instead.
关于python - 删除 python 3 中的尾随空格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18182325/