mjf8055 2021-04-08 13:01 采纳率: 100%
浏览 321
已采纳

用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.” 就行,不用画出来台阶了。

 

求助!应该怎么修改?

  • 写回答

4条回答 默认 最新

  • coagenth 2021-04-08 13:55
    关注

    不显示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)
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥200 csgo2的viewmatrix值是否还有别的获取方式
  • ¥15 Stable Diffusion,用Ebsynth utility在视频选帧图重绘,第一步报错,蒙版和帧图没法生成,怎么处理啊
  • ¥15 请把下列每一行代码完整地读懂并注释出来
  • ¥15 pycharm运行main文件,显示没有conda环境
  • ¥15 寻找公式识别开发,自动识别整页文档、图像公式的软件
  • ¥15 为什么eclipse不能再下载了?
  • ¥15 编辑cmake lists 明明写了project项目名,但是还是报错怎么回事
  • ¥15 关于#计算机视觉#的问题:求一份高质量桥梁多病害数据集
  • ¥15 特定网页无法访问,已排除网页问题
  • ¥50 如何将脑的图像投影到颅骨上