首页 > 解决方案 > 如何在python中的命令上使程序重新启动

问题描述

我有以下程序:

x = int(input('How many rows? - '))
y = int(input('How many max per row? - '))
z = int(input('How many min per row - '))
string = str('•')

lines = y # Makes a copy of y that can be seperatly changed

while True:

    if z > y: # Makes sure than "max per row" > "min per row"
        print('Please make sure that \"max per row\" is greater than \"min per row\"')
        break

    for n in range(int(x)): # Run the specified amount of times

        if int(lines) > int(z): # If 'max per row' < 'min per row'
            for more in range(int(y)): # Print '•'*'y' untill it reaches 'min per row'
                print(str(string)*int(lines))
                lines -= 1

        else:
            for less in range(int(y)): # Print '•'*'y' untill it reaches 'max per row'
                print(str(string)*int(lines))
                lines += 1

我要restart程序if z > y。目前我用一个函数停止程序break,我希望它能够re-run. (见第 12 行)

标签: python

解决方案


你可以创建一个函数

def main():
    x = int(input('How many rows? - '))
    y = int(input('How many max per row? - '))
    z = int(input('How many min per row - '))
    string = str('•')

    lines = y # Makes a copy of y that can be seperatly changed

    while True:

        if z > y: # Makes sure than "max per row" > "min per row"
            print('Please make sure that \"max per row\" is greater than \"min per row\"')
            break

        for n in range(int(x)): # Run the specified amount of times

            if int(lines) > int(z): # If 'max per row' < 'min per row'
                for more in range(int(y)): # Print '•'*'y' untill it reaches 'min per row'
                    print(str(string)*int(lines))
                    lines -= 1

            else:
                for less in range(int(y)): # Print '•'*'y' untill it reaches 'max per row'
                    print(str(string)*int(lines))
                    lines += 1

while True:
    main()

推荐阅读