close

How do I repeat the program in python

Hello Guys, How are you all? Hope You all Are Fine. Today We Are Going To learn about How do I repeat the program in python in Python. So Here I am Explain to you all the possible Methods here.

Without wasting your time, Let’s start This Article.

Table of Contents

How do I repeat the program in python?

  1. How do I repeat the program in python?

    Put the whole code in a while True loop and at the end ask the user if they want to repeat. If not, break the loop.

  2. repeat the program in python

    Put the whole code in a while True loop and at the end ask the user if they want to repeat. If not, break the loop.

Method 1

A good way of organizing your code is to put your main program into a function called main() or similar:

def main():
    sentence = input("Please enter sentence(s): ")
    num_words = len(sentence.split(' '))

    counter = 0
    for x in sentence:
        if x in "!?.":
            counter += 1

    print("There are", counter, "sentences and", num_words, "words.")

Then, underneath this, you write your code for repeating the function:

while True:
    main()
    if input("Repeat the program? (Y/N)").strip().upper() != 'Y':
        break

Method 2

Put the whole code in a while True loop and at the end ask the user if they want to repeat. If not, break the loop.

Something like this:

while True:
    sentence=input("Please enter sentence(s)")
    words = sentence.split()
    number_of_words = len(words)
    counter=0
    for x in sentence:
        if x in "!?.":
            counter=counter+1
    print("There is "+str(counter)+" sentences and " + str(number_of_words) + "words")
    if input('Do you want to repeat(y/n)') == 'n':
        break

Summery

It’s all About this issue. Hope all Methods helped you a lot. Comment below Your thoughts and your queries. Also, Comment below which Method worked for you? Thank You.

Also, Read