Hướng dẫn how to loop back to a certain line in python - cách lặp lại một dòng nhất định trong python

Tôi đã tạo một menu lựa chọn yêu cầu bạn nhập lệnh bạn muốn chạy, tôi đã thực hiện điều này bằng cách sử dụng các câu lệnh hỏi lệnh nào để chạy. Đây là mã tôi có vào lúc này (hiện tại tôi không có gì làm điều này):

# Asks which command you want to run.
word = input("Which command? ")

# Runs the command/s.

if word == "info":
    print("Info command.")

elif word == "replace":
    print("Replace command.")

elif word == "ping":
    print("Ping command")

else:
    print("Command not found")

Sẽ thật tuyệt vời nếu ai đó có thể giúp đỡ, cảm ơn.

Hỏi ngày 28 tháng 12 năm 2012 lúc 18:37Dec 28, 2012 at 18:37

Hướng dẫn how to loop back to a certain line in python - cách lặp lại một dòng nhất định trong python

1

Xin lỗi nếu điều này là quá nhiều, nhưng bạn có thể muốn xem xét đưa nó vào một chức năng để làm điều gì đó như thế này:

def main():
    # Asks which command you want to run.
    word = input("Which command? ").strip().lower()

    # Runs the command/s.

    if word == "info":
        print("Info command.")

    elif word == "replace":
        print("Replace command.")

    elif word == "ping":
        print("Ping command")

    elif word == "quit":
        return False

    else:
        print("Command not found")
    return True

while main():
    pass

Đây sẽ giống như while True sau đó

def main():
    # Asks which command you want to run.
    word = input("Which command? ").strip().lower()

    # Runs the command/s.

    if word == "info":
        print("Info command.")

    elif word == "replace":
        print("Replace command.")

    elif word == "ping":
        print("Ping command")

    elif word == "quit":
        return False

    else:
        print("Command not found")
    return True

while main():
    pass
0

Đã trả lời ngày 28 tháng 12 năm 2012 lúc 18:44Dec 28, 2012 at 18:44

Hướng dẫn how to loop back to a certain line in python - cách lặp lại một dòng nhất định trong python

jackcogdilljackcogdilljackcogdill

4.7623 huy hiệu vàng27 Huy hiệu bạc47 Huy hiệu đồng3 gold badges27 silver badges47 bronze badges

0

Hãy thử gói mã của bạn trong một khối

def main():
    # Asks which command you want to run.
    word = input("Which command? ").strip().lower()

    # Runs the command/s.

    if word == "info":
        print("Info command.")

    elif word == "replace":
        print("Replace command.")

    elif word == "ping":
        print("Ping command")

    elif word == "quit":
        return False

    else:
        print("Command not found")
    return True

while main():
    pass
1. Điều này sẽ lặp lại mã vô thời hạn.

Để đọc thêm, hãy thử điều này

Đã trả lời ngày 28 tháng 12 năm 2012 lúc 18:40Dec 28, 2012 at 18:40

1

Câu trả lời được đề xuất

Lời khuyên của Wooeee là tại chỗ, theo như nó đi. Tuy nhiên, nếu bạn đang cố gắng lặp lại một cái gì đó mà không biết phải lặp lại bao nhiêu lần, những gì bạn muốn là một vòng lặp

def main():
    # Asks which command you want to run.
    word = input("Which command? ").strip().lower()

    # Runs the command/s.

    if word == "info":
        print("Info command.")

    elif word == "replace":
        print("Replace command.")

    elif word == "ping":
        print("Ping command")

    elif word == "quit":
        return False

    else:
        print("Command not found")
    return True

while main():
    pass
2.

pagechoice = '1'
while pagechoice != '0':
    pagechoice = input("Please choose one:") …

Nhảy để đăng

Tất cả 4 câu trả lời

Hướng dẫn how to loop back to a certain line in python - cách lặp lại một dòng nhất định trong python

woooee814 gần một maven đăng 814 Nearly a Posting Maven

8 năm trước

Sử dụng chức năng http://www.tutorialspoint.com/python/python_funces.htmm và gọi nó nhiều lần như bạn muốn

import time

def print_something(ctr):
    choices = ["zero", "one", "two", "three", "four",
               "five", "six"]
    if ctr < len(choices):
        print(" This is page", choices[ctr])
    else:
        print("Sorry, but what you have typed in is invalid,\nPlease try again.")

ctr=1
print_something(ctr)
time.sleep(1.0)
ctr = 6
print_something(ctr)
time.sleep(1.0)
ctr = 10
print_something(ctr)

Được chỉnh sửa 8 năm trước bởi Woooee

Schol-R-LEA1,446 Commie Mutant Faitor có poster nổi bật 1,446 Commie Mutant Traitor Featured Poster

8 năm trước

Lời khuyên của Wooeee là tại chỗ, theo như nó đi. Tuy nhiên, nếu bạn đang cố gắng lặp lại một cái gì đó mà không biết phải lặp lại bao nhiêu lần, những gì bạn muốn là một vòng lặp

def main():
    # Asks which command you want to run.
    word = input("Which command? ").strip().lower()

    # Runs the command/s.

    if word == "info":
        print("Info command.")

    elif word == "replace":
        print("Replace command.")

    elif word == "ping":
        print("Ping command")

    elif word == "quit":
        return False

    else:
        print("Command not found")
    return True

while main():
    pass
2.

pagechoice = '1'
while pagechoice != '0':
    pagechoice = input("Please choose one:")

    #Now it will determine which page you choose.

    if pagechoice == '1':
        print(""" This is page one """)
    elif pagechoice == '2':
        print(""" This is page two """)
    elif pagechoice == '3':
        print(""" This is page three """)
    elif pagechoice == '4':
        print(""" This is page four """)
    elif pagechoice == '5':
        print(""" This is page five """)
    elif pagechoice == '6':
        print(""" This is page six """)
    else:
        print("Sorry, but what you have typed in is invalid,\nPlease try again.")

Nhảy để đăng

for x in ['a', 'd', 'm', 'e', 'h']:
    print(x)

Tất cả 4 câu trả lời

woooee814 gần một maven đăng

Hướng dẫn how to loop back to a certain line in python - cách lặp lại một dòng nhất định trong python

8 năm trước

Sử dụng chức năng http://www.tutorialspoint.com/python/python_funces.htmm và gọi nó nhiều lần như bạn muốn
i.e Call that function and have 1 correct 'yes' responce and an else 'no responce'.

Được chỉnh sửa 8 năm trước bởi Woooee

Schol-R-LEA1,446 Commie Mutant Faitor có poster nổi bật 1,446 Commie Mutant Traitor Featured Poster

8 năm trước

Sử dụng chức năng http://www.tutorialspoint.com/python/python_funces.htmm và gọi nó nhiều lần như bạn muốn

def print_something(ctr):
    if ctr == '1':
        print("This is a valid entry.")
    else:
        print("Sorry, but what you have typed in is invalid,\nPlease try again.")

Được chỉnh sửa 8 năm trước bởi Woooee

woooee814 gần một maven đăng

8 năm trước

Sử dụng chức năng http://www.tutorialspoint.com/python/python_funces.htmm và gọi nó nhiều lần như bạn muốn

Được chỉnh sửa 8 năm trước bởi Woooee

Làm thế nào để bạn quay trở lại một dòng cụ thể trong Python?

Python không cho phép bạn quay lại một số dòng cụ thể và ngay cả khi nó đã xảy ra, bạn không nên tận dụng khả năng đó vì nó dẫn đến các chương trình không thể giải quyết được.Thay vào đó, hãy học cách sử dụng các chức năng và cấu trúc mã của bạn để các chức năng có ý nghĩa., and even if it did, you should not take advantage of that capability because it results in unmaintainable programs. Instead, learn how to use functions and structure your code so that functions make sense.

Làm thế nào để bạn tiếp tục một dòng nhất định trong Python?

Sử dụng dấu gạch chéo ngược (\) làm ký tự tiếp tục dòng trong Python, dấu gạch chéo ngược (\) là ký tự tiếp tục dòng.Nếu một dấu gạch chéo ngược được đặt ở cuối một dòng, người ta coi là dòng được tiếp tục trên dòng tiếp theo. In Python, a backslash ( \ ) is a line continuation character. If a backslash is placed at the end of a line, it is considered that the line is continued on the next line.

Làm thế nào để bạn quay lại một bước trong Python?

Hoạt động "Quay trở lại một bước" của bạn là những gì thường được gọi là GOTO, có lẽ là hoạt động bị ghét nhất trong bất kỳ ngôn ngữ lập trình nào.Nó không được hỗ trợ trong Python, bạn phải sử dụng các phương pháp khác như vòng lặp.Bạn quay lại bằng cách sử dụng các vòng lặp, giống như những vòng trong khi bạn có.You go back by using loops, like those while loops you have.