Hướng dẫn python string repeat

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

How to Loop Back to the Beginning of a Program in Python?

Here, we will see how to loop back to the beginning of the program in Python. In other words, the program’s control is at some point other than the beginning, and we want the program to start from the top again. Consider the figure below to understand this concept.

Hướng dẫn python string repeat

Loop back in Python

In this post, we will talk about two approaches.

1. Using  a Loop

We can loop back to the start by using a control flow statement, i.e., a while statement. To do that, wrap the complete program in a while loop that is always True.

Moreover, add a continue statement at a point where you want to start the program from the beginning. You also need to add some code such as a break statement to terminate your program.

Otherwise, the program will run infinitely, and we never desire that.

How to loop back in Python 2

Suppose we have a program that takes the distance and time from the user and calculates the speed.

distance =  float(input("Enter the distance in kilometers: "))
time = float(input("Enter the time in hours: "))
speed = distance/time
print("Speed is:", speed,"kph")

Now, we want to start from the beginning if the user wants to perform another calculation. To do that, we add a while statement at the top.

We also use a continue statement to restart if the user enters yes. If the user wants to quit, the continue statement will not run, and the program will terminate. Consider the code below that implements this.

while True:
  distance =  float(input("Enter the distance in kilometers: "))
  time = float(input("Enter the time in hours: "))
  speed = distance/time
  print("Speed is:", speed,"kph")
  check = input("Do you want to quit or start again? enter Y to restart or another key to end: ")
  if check.upper() == "Y": #go back to the top
    continue    
  print("Bye...")
  break #exit

Looping back in Python Output

2. Using a Function

We can also loop back to the beginning by using a function. Instead of wrapping the whole code in a while loop, we create a function and put our program there. If the user wants to continue, we will call the procedure again. Otherwise, we will exit the program.

Consider the same example implemented using a function.

def repeat():

  distance =  float(input("Enter the distance in kilometers: "))

  time = float(input("Enter the time in hours: "))

  speed = distance/time
  
  print("Speed is:", speed,"kph")

  check = input("Do you want to quit or start gain, enter Y to restart or another to end ?: ")

  if check.upper() == "Y": #loop back to the start

  repeat()
  print("Bye...")

  exit() #exit the program



repeat()

Output

Looping back in Python result of function approach

Read about ways to loop back to the beginning of a program in Python.

Hey guys! It’s me, Marcel, aka Maschi. I earn a full-time income online and on MaschiTuts I gladly share with you guys how I stay on top of the game! I run several highly profitable blogs & websites and love to speak about these project whenever I get a chance to do so. I do this full-time and wholeheartedly. In fact, the moment I stopped working an 8-to-5 job and finally got into online business as a digital entrepreneur, is problably one of the best decisions I ever took in my life. And I would like to make sure that YOU can get on this path as well! Don’t let anyone tell you that this can’t be done. Sky’s the limit, really…as long as you BELIEVE in it! And it all starts right here..at Maschituts!

Is there a repeat function in Python?

In repeat() we give the data and give the number, how many times the data will be repeated. If we will not specify the number, it will repeat infinite times. In repeat(), the memory space is not created for every variable.

How do you repeat 3 times in Python?

The Python for statement iterates over the members of a sequence in order, executing the block each time. Contrast the for statement with the ''while'' loop, used when a condition needs to be checked each iteration or to repeat a block of code forever. For example: For loop from 0 to 2, therefore running 3 times.