用Python画台阶

 

Please write a program that creates steps. You are expected to take in a single positive integer which will be used as the number of steps in your staircase. The program only accepts integers greater than 0 and less than 500. If 0 is entered a message stating "Your staircase has no steps." and if the user enters a value greater than or equal to 500, a message stating "I can't build a staircase that tall." For all other values not within the valid range or not integers an "Invalid staircase size provided." will be displayed to the user. The program will always run only once for each user input. 

One thing to note, the messages should be the return string from your createSteps() function and printed from the calling function.

Additional Requirements:

1. The very first step in the stair case will be left aligned and for each subsequent level the step will move above and to the right of the prior step. 

2. There are no spaces after the right of any of the steps.

3. The bottom most row ends without a new line.

请编写一个创建步骤的程序。你需要使用一个正整数作为楼梯的步数。这个程序只接受大于0小于500的整数。如果输入0,则显示“你的楼梯没有台阶”,如果用户输入大于或等于500的值,则显示“我不能建那么高的楼梯”。对于所有其他不在有效范围内或不是整数的值,将向用户显示“提供的无效楼梯大小”。对于每个用户输入,程序总是只运行一次。

需要注意的一点是,消息应该是createSteps()函数的返回字符串,并从调用函数打印出来。

附加要求:

1. 楼梯格中的第一个步骤将向左对齐,对于后续的每一个级别,该步骤将移动到前一个步骤的上方和右侧。

2. 任何台阶右边都没有空格。

3.最底部的行结束时没有新的行。

 

 

Sample Output:

理想的结果


''' This functions asks the user for the number of steps
they want to climb, gets the value provided by the user
and returns it to the calling function'''
def getUserInput():

    try:

        stepsStr = input('How many steps do you want to move? ')
        steps = int(stepsStr)
        if steps == 0:
            print("Your staircase has no steps.")
        if steps >= 500:
            print("I can't build a staircase that tall.")
        if steps < 0:
            print('Invalid staircase size provided.')
    except IOError:
        print('Invalid staircase size provided.')

    return steps



''' This function takes the number of steps as an unput parameter,
creates a string that contains the entire steps based on the user input
and returns the steps string to the calling function
'''
def createSteps(stepCount):
    if stepCount==1:
        for i in reversed(range(1,stepCount+1)):
            return(" "*(( i * 2) - 2)+ '\n'+'+-+'+" " * ((i * 2) - 2)+ '\n'+'| |'+" " * ((i * 2) - 2)+ '\n'+'+-+')

    elif stepCount != 0:
        print(" " * ((stepCount * 2) - 2), end = '')
        print('+-+')

        for i in reversed(range(1, stepCount + 1)):
            print(" " * ((i * 2) - 2), end = '')
            print('| |')
            print(" " * ((i * 2) - 4), end = '')
            if i == 1:
                print('+-+')
            else:
                print('+-+-+')


'''Within this condition statement you are to write the code that
calls the above functions when testing your code the code below this
should be the only code not in a function and must be within the if
statement. I will explain this if statement later in the course.'''
if __name__ == "__main__":
    steps = getUserInput()
    createSteps(steps)

这是我写的,但是输入1的时候没反应是怎么回事?还有>=500的时候也不太对,应该只说“I can't build a staircase that tall.” 就行,不用画出来台阶了。

 

求助!应该怎么修改?

不显示step1,是因为只有return没有print; steps>=500时显示,是因为elif stepCount != 0这个条件设定不准确,应该界定清楚,是从1到499。其他不作图,只显示提示信息。这样改:

''' This functions asks the user for the number of steps
they want to climb, gets the value provided by the user
and returns it to the calling function'''
def getUserInput():
    stepsStr = input('How many steps do you want to move? ')
    steps = int(stepsStr)    
    if steps == 0:
        print("Your staircase has no steps.")
    elif steps >= 500:
        print("I can't build a staircase that tall.")           
    elif steps<0: 
        print('Invalid staircase size provided.')
    return steps
 
 
''' This function takes the number of steps as an unput parameter,
creates a string that contains the entire steps based on the user input
and returns the steps string to the calling function
'''
def createSteps(stepCount):
    if stepCount==1:
        for i in reversed(range(1,stepCount+1)):
            print(" "*(( i * 2) - 2)+ '\n'+'+-+'+" " * ((i * 2) - 2)+ '\n'+'| |'+" " * ((i * 2) - 2)+ '\n'+'+-+')
 
    elif 1<stepCount <500:
        print(" " * ((stepCount * 2) - 2), end = '')
        print('+-+')
 
        for i in reversed(range(1, stepCount + 1)):
            print(" " * ((i * 2) - 2), end = '')
            print('| |')
            print(" " * ((i * 2) - 4), end = '')
            if i == 1:
                print('+-+')
            else:
                print('+-+-+')
    else:
        pass
 
'''Within this condition statement you are to write the code that
calls the above functions when testing your code the code below this
should be the only code not in a function and must be within the if
statement. I will explain this if statement later in the course.'''
if __name__ == "__main__":
    steps = getUserInput()
    createSteps(steps)

如果有更好的答案可以无视我写的,但是请保留def getUserInput(): 和 def createSteps(stepCount): 还有 

if __name__ == "__main__": 这三个function,谢谢你!

我想请教一个问题啊,直接对输入的数取整,然后判断,如果是1到500之间的非整数就实现不了报错的目的吧?

您好,我是有问必答小助手,您的问题已经有小伙伴帮您解答,欢迎您加入CSDN!

目前问答VIP仅需29元,即可享受5次/月 有问必答服务,了解详情>>>https://vip.csdn.net/askvip?utm_source=1146287632