Divide05
Divide05

Reputation: 27

How to make a program restart on command in python

I have the following program:

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

I want to restart the program if z > y. Currently I stop the program with a break function, I wish for it to re-run. (see line 12)

Upvotes: 1

Views: 107

Answers (3)

Divide05
Divide05

Reputation: 27

I fixed my issue by putting the check for if z <= y: inside a loop that breaks if it's true nad continues to ask if it's false. Also improved code by ading a sleep function.

from time import sleep

while True:
    y = int(input('How many max per row? - '))
    z = int(input('How many min per row - '))

    if z <= y:
        break

    print('Please make sure that "max per row" is greater than "min per row"')

string = ('•')

lines = y # Makes a copy of y that can be seperatly changed
lines = int(lines)
runTimes = y - z

while True: # Just keeps making waves
 
    if lines > z: # If 'max per row' > 'min per row'
        for more in range(runTimes): # Print '•'*'y' untill it reaches 'min per row'
            finalString = string*lines
            print(finalString)
            lines -= 1
            sleep(0.035)
            
    else:
        for less in range(runTimes): # Print '•'*'y' untill it reaches 'max per row'
            finalString = string*lines
            print(finalString)
            lines += 1
            sleep(0.035)

Upvotes: 0

inof
inof

Reputation: 495

I assume this is what you want to do:

while True:
    x = int(input('How many rows? - '))
    y = int(input('How many max per row? - '))
    z = int(input('How many min per row - '))

    if z <= y:
        break

    print('Please make sure that "max per row" is greater than "min per row"')

string = '•'

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

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

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

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

PS: You don’t have to use str() if a value already is a string. Similarly for int(). You already convert to integer at the very beginning with int(input(...)), so the variables already contain integer values, so you don’t have to use int() again later on.

Upvotes: 0

George Imerlishvili
George Imerlishvili

Reputation: 1957

you can create a function

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()

Upvotes: 2

Related Questions