CS50 马里奥程序(一对金字塔),为什么我会得到额外的空间等?

问题描述 投票:0回答:2

我想询问用户金字塔的高度。对于 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 "" 

尽管输出看起来像我想要的,但它没有通过检查。看起来输出中有一些不需要的空格,但我无法弄清楚在哪里。

python cs50
2个回答
0
投票

诊断会告诉您出了什么问题。它需要比您在哈希值之前打印的内容少一个空格,并且之后没有空格(它们是不必要的,因为默认情况下屏幕已经是空白的)。

然而,额外空间的原因并不简单。您会发现

" " * 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
有意义的部分,因此根据定义,这或多或少是您应该拥有的“功能”。

演示:https://ideone.com/RS7IPq

在您的作业之外,更好的设计是接受数字作为命令行参数,而不是需要交互式提示,但我们现在不去那里。


0
投票

考虑创建一个函数来生成金字塔的字符串表示形式,而不是直接打印,因为这使得单元测试变得更加容易,因为您不需要模拟

sys.stdout
(yikes😬):

"""Command line utility for printing a Mario-style pyramid for cs50."""


def get_int_input(prompt: str, minimum: int = 0, maximum: int = 100) -> int:
  """Repeatedly asks user for input with given prompt until within required range.

  Args:
    prompt: The message shown to the user asking them to input a value.
    minimum: The minimum integer value allowed to be entered. Defaults to 0.
    maximum: The maximum integer value allowed to be entered. Defaults to 100.

  Returns:
    An integer value provided by the user within the range: [minimum, maximum].
  """
  while True:
    try:
      x = int(input(prompt))
      if minimum <= x <= maximum:
        return x
    except ValueError:
      print('Error: Enter an integer, try again...')


def get_mario_pyramid(height: int, gap: int = 2) -> str:
  """Generates a string representation of a Mario-style pyramid for a specified height.

  Args:
    height: The height of the pyramid, which determines the number of rows.
    gap: The gap between the two halves of the pyramid. Defaults to 2.

  Returns:
    A string representing the pyramid, with each row separated by a newline character.
  """
  pyramid = []
  for row in range(1, height + 1):
    spaces = ' ' * (height - row)
    hashes = '#' * row
    pyramid.append(f'{spaces}{hashes}{" " * gap}{hashes}')
  return '\n'.join(pyramid)


def main() -> None:
  """Entry point for the command line tool."""
  height = get_int_input(prompt='Height: ', minimum=1, maximum=8)
  print(get_mario_pyramid(height))


if __name__ == '__main__':
  main()
© www.soinside.com 2019 - 2024. All rights reserved.