我想询问用户金字塔的高度。对于 4 的高度,我想打印:两组金字塔,由两个空格分隔。
# #
## ##
### ###
#### ####
我有以下代码:
def get_int(prompt):
while True:
try:
value = int(input(prompt))
if 1 <= value <= 8:
return value
except ValueError:
pass
def main():
height = get_int("Height: ")
for row in range(height):
spaces = height - row - 1
hashes = row + 1
print(" " * spaces, "#" * hashes, end="")
print(" ", end="")
print("#" * hashes, " " * spaces)
main()
输出:
sentimental-mario-more/ $ python mario.py
Height: 8
# #
## ##
### ###
#### ####
##### #####
###### ######
####### #######
######## ########
sentimental-mario-more/ $ check50 cs50/problems/2024/x/sentimental/mario/more
Connecting.......
Authenticating...
Verifying......
Preparing.....
Uploading.......
Waiting for results............................
Results for cs50/problems/2024/x/sentimental/mario/more generated by check50 v3.3.11
:) mario.py exists.
:) rejects a height of -1
:) rejects a height of 0
:( handles a height of 1 correctly
expected ""# #"", not "" # # ""
:( handles a height of 2 correctly
expected "" # #"\n"## ...", not "" # # "\n" ..."
:( handles a height of 8 correctly
expected "" # #"\...", not "" # # ..."
:( rejects a height of 9, and then accepts a height of 2
expected " # #\n## ##\...", not " # # \n ## ..."
:) rejects a non-numeric height of "foo"
:) rejects a non-numeric height of ""
尽管输出看起来像我想要的,但它没有通过检查。看起来输出中有一些不需要的空格,但我无法弄清楚在哪里。
诊断会告诉您出了什么问题。它需要比您在哈希值之前打印的内容少一个空格,并且之后没有空格(它们是不必要的,因为默认情况下屏幕已经是空白的)。
然而,额外空间的原因并不简单。您会发现
" " * 0
产生一个空格,而不是零。解决这个问题的方法是打破空格的打印,并使其成为有条件的。
def get_int(prompt):
while True:
try:
value = int(input(prompt))
if 1 <= value <= 8:
return value
except ValueError:
pass # or perhaps continue
def pyramids(height):
for row in range(height):
spaces = height - row - 1
hashes = row + 1
# conditional spaces
if spaces:
print(" " * spaces, end="")
print("#" * hashes, end="")
print(" ", end="")
print("#" * hashes) # no trailing spaces
def main():
height = get_int("Height: ")
pyramids(height)
main()
我还将金字塔打印重构为一个单独的函数;这是这里唯一对需要此功能的脚本中的
import
有意义的部分,因此根据定义,这或多或少是您应该拥有的“功能”。
在您的作业之外,更好的设计是接受数字作为命令行参数,而不是需要交互式提示,但我们现在不去那里。