簡體   English   中英

用用戶控制的輸入在python中創建一個框

[英]Making a box in python with controlled inputs from user

我正在嘗試制作一個框,用戶可以在其中輸入寬度,高度,應使用哪種符號制作框以及填充(在框內)。 我是一個新的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#
####
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)

使用input("Enter number")獲取用戶輸入。 您應該首先在高度上循環,然后在寬度上循環。 要不使用換行符進行打印,請使用end=""作為要print的參數。 您使用了i而不是ba 我想就是這樣。 下次再問更具體的問題。

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#
#####

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM