Kinh Nghiệm về How to return to a previous loop in python 2022
Họ tên bố(mẹ) đang tìm kiếm từ khóa How to return to a previous loop in python được Cập Nhật vào lúc : 2022-09-27 00:12:22 . Với phương châm chia sẻ Kinh Nghiệm về trong nội dung bài viết một cách Chi Tiết 2022. Nếu sau khi đọc tài liệu vẫn ko hiểu thì hoàn toàn có thể lại Comments ở cuối bài để Tác giả lý giải và hướng dẫn lại nha.Try nesting the first while loop inside of the second. It'll run your calculation code first, check to see if you'd like to do another, and then return to the top of the while True: loop to do another calculation.
Nội dung chính- Using breakUsing continuebreak and continue visualizedUsing break and continue in nested loops.Loop Control in while loopsLoops and the return statementHow do you go back a loop in Python?How do you call a former loop in Python?How do you go back to a specific line in Python?Can you return a for loop?
Like this:
while True: x, y = raw_input("Enter 2 numbers separated by a space.").split() answer = 0 #Enter the 2 numbers to be calculated. print "You have selected, A = "+ x + " and B = " + y + "." while int(y): if (not int(y) % 2 == 0): # this checks if y is even or odd answer = int(answer) + int(x) print "A = " + str(x) + " and B = " + str(y) + "." print "B is odd so we'll add A to the total." print "The running total is " + str(answer) + "." else: (int(y) % 2 == 0) print "A = " + str(x) + " and B = " + str(y) + "." print "B is even, so we'll ignore that number." x = int(x) * 2 y = int(y) / 2 print "The product is " + str(answer) + "." a = raw_input("Would you like to make another calculation? Y or N") if str(a) == "Y" or str(a) == "y": continue if str(a) == "N" or str(a) == "n": print "Thank you have a nice day!" break else: print "Invalid entry. Ending program." breakHope that helps
Depending on what you're trying to do, and where this particular code appears in your program, it's usually advised to wrap your code inside of functions. That way, it doesn't automatically run when you import your module. You have to call a function to make the code run.
If you want to make this an executable script, you'd want to wrap the main loop code in a if __name__ == '__main__: block so that it only executes if it's being executed directly.
e.g.:
def perform_calculation(): while True: x, y = raw_input("Enter 2 numbers separated by a space.").split() answer = 0 #Enter the 2 numbers to be calculated. print "You have selected, A = "+ x + " and B = " + y + "." while int(y): if (not int(y) % 2 == 0): # this checks if y is even or odd answer = int(answer) + int(x) print "A = " + str(x) + " and B = " + str(y) + "." print "B is odd so we'll add A to the total." print "The running total is " + str(answer) + "." else: (int(y) % 2 == 0) print "A = " + str(x) + " and B = " + str(y) + "." print "B is even, so we'll ignore that number." x = int(x) * 2 y = int(y) / 2 print "The product is " + str(answer) + "." def run_loop(): while True: perform_calculation() a = raw_input("Would you like to make another calculation? Y or N") if str(a) == "Y" or str(a) == "y": continue if str(a) == "N" or str(a) == "n": print "Thank you have a nice day!" break else: print "Invalid entry. Ending program." break if __name__ == '__main__': run_loop()break and continue allow you to control the flow of your loops. They’re a concept that beginners to Python tend to misunderstand, so pay careful attention.
Using break
The break statement will completely break out of the current loop, meaning it won’t run any more of the statements contained inside of it.
>>> names = ["Rose", "Max", "Nina", "Phillip"] >>> for name in names: ... print(f"Hello, name") ... if name == "Nina": ... break ... Hello, Rose Hello, Max Hello, Ninabreak completely breaks out of the loop.
Using continue
continue works a little differently. Instead, it goes back to the start of the loop, skipping over any other statements contained within the loop.
>>> for name in names: ... if name != "Nina": ... continue ... print(f"Hello, name") ... Hello, Ninacontinue continues to the start of the loop
break and continue visualized
What happens when we run the code from this Python file?
# Python file names.py names = ["Jimmy", "Rose", "Max", "Nina", "Phillip"] for name in names: if len(name) != 4: continue print(f"Hello, name") if name == "Nina": break print("Done!") ResultsSee if you can guess the results before expanding this section.
Using break and continue in nested loops.
Remember, break and continue only work for the current loop. Even though I’ve been programming Python for years, this is something that still trips me up!
>>> names = ["Rose", "Max", "Nina"] >>> target_letter="x" >>> for name in names: ... print(f"name in outer loop") ... for char in name: ... if char == target_letter: ... print(f"Found name with letter: target_letter") ... print("breaking out of inner loop") ... break ... Rose in outer loop Max in outer loop Found Max with letter: x breaking out of inner loop Nina in outer loop >>>break in the inner loop only breaks out of the inner loop! The outer loop continues to run.
Loop Control in while loops
You can also use break and continue in while loops. One common scenario is running a loop forever, until a certain condition is met.
>>> count = 0 >>> while True: ... count += 1 ... if count == 5: ... print("Count reached") ... break ... Count reachedBe careful that your condition will eventually be met, or else your program will get stuck in an infinite loop. For production use, it’s better to use asynchronous programming.
Loops and the return statement
Just like in functions, consider the return statement the hard kill-switch of the loop.
>>> def name_length(names): ... for name in names: ... print(name) ... if name == "Nina": ... return "Found the special name" ... >>> names = ["Max", "Nina", "Rose"] >>> name_length(names) Max Nina 'Found the special name'