使用来自用户的受控输入在python中创建一个框

时间:2016-10-11 21:18:38

标签: python

我正在尝试制作一个框,用户输入宽度,高度,框应该用什么样的符号和填充(在框内)。我是一个新的python编码器,所以任何建议都会很棒,但是新手级别的响应越多越好,所以我可以学习而不是跳过远程技术。

  def main():
        width = print(int("Please enter the width of the box: "))
        height = print(int("Please enter the height of the box: "))
        symbol =  print("Please enter the symbol for the box outline: ")
        fill = print("Please enter the symbol for the box fill: ")
        for a in range(width):
            for b in range(height):
                if i in #some condition here
                    print(symbol)
                else:
                    print(fill)
    main()

我的预计输入应为:

width: 4
height: 4
symbol: #
fill:1
####
#11#
#11#
####

3 个答案:

答案 0 :(得分:2)

def main():
    # input is your friend here 
    width = input("Please enter the width of the box: ")
    #width = print(int("Please enter the width of the box: "))
    # input etc.. 
    height = print(int("Please enter the height of the box: "))
    symbol =  print("Please enter the symbol for the box outline: ")
    fill = print("Please enter the symbol for the box fill: ")
    #since you'll be printing rows of text you should probably flip these loops
    for row in range(height):
    #for a in range(width): 
        for col in range(width):
        #for b in range(height):
            #   i ??? huh where did this come from ?
            #if i in [0, width-1] or [0, height-1]:
            # descriptive variables can only help
            if row in [0,height-1] or col in [0,width-1]:
                print(symbol)
            else:
                print(fill)

答案 1 :(得分:0)

使用input("Enter number")获取用户的输入。您应首先在高度上然后在宽度上循环。要在不使用换行符的情况下进行打印,请使用end=""作为print的参数。您使用i代替ba。这就是我想的。下次问更具体的问题。

答案 2 :(得分:0)

def main():
    width = int(input("Please enter the width of the box: "))
    height = int(input("Please enter the height of the box: "))
    symbol = input("Please enter the symbol for the box outline: ")
    fill = input("Please enter the symbol for the box fill: ")
    dictionary = []
    for row in range(height):
        for col in range(width):
            if row in [0, height-1] or col in [0, width-1]:
                dictionary.append(symbol)
            else:
                dictionary.append(fill)

    def slice_per(source, step):
        return [source[i::step] for i in range(step)]
    sliced = slice_per(dictionary, width)
    for x in range(len(sliced)):
        print("".join(sliced[x]), end="\n")
main()

输出 - 5,5,#,0

#####
#000#
#000#
#000#
#####