首页 > 解决方案 > Why is my python Mario program less comfortable ignoring the spaces?

问题描述

I'm working on cs50 pset6 python Mario, and it's supposed to take an int as input (the int is named height) and then make a mario pyramid of size(height), like this:

$ python mario.py
Height: 4
   #
  ##
 ###
####

But my program prints this:

~/ $ python mario.py
Height: 4
#
##
###
####

I don't know why. Here's my code:

from cs50 import get_int

height = get_int("Height: ")
while height < 1 or height > 8:
height = get_int("Height:")

for i in range(1, height+1):
    for j in range(height - i):
        print("", end = '')
    if i == 1:
        print("#")
    if i == 2:
        print("##")
    if i == 3:
        print("###")
    if i == 4:
        print("####")
    if i == 5:
        print("#####")
    if i == 6:
        print("######")
    if i == 7:
        print("#######")
    if i == 8:
        print("########")

If you find my bug, please tell me. Thanks.

标签: pythoncs50

解决方案


It looks like you are printing an empty string instead of white spaces:

print("", end = '')

Try this instead:

print(" ", end = '')

推荐阅读