簡體   English   中英

Python 錯誤:“中斷在循環之外”

[英]Python error: “break is outside the loop”

這是一個在線預訂電影票的程序。 output 顯示“break is outside the loop”。 我應該改變什么來糾正這個?

viewers = []
class TenetMovie:
def __init__(self,movie_name):
    self.movie_name=movie_name


    def ticket_buyer(self):
        print('at any point if you want to exit press "q"') 
        your_name = input("your name : ")
        if your_name == 'q':
            break
        no_of_tickets = input("how many tickets : ")
        if no_of_tickets == 'q':
            break
        seats = input("select the seats : ")
        if seats == 'q':
            break
        viewers.extend((self.movie_name,your_name,no_of_tickets,seats))
    


    for viewer in viewers:
        print("\nMovie : "+self.movie_name)
        print("name : "+your_name)
        print("Number of Tickets : "+no_of_tickets)
        print("Seat Number : "+seats)
        break



movie_tickets=TenetMovie(input("Which movie would you like to watch : "))
movie_tickets.ticket_buyer()

當你想退出 function 時,使用return not break

您必須使用return not break因為您沒有使用循環,即 function。

viewers = []
class TenetMovie:
    def __init__(self,movie_name):
        self.movie_name=movie_name


    def ticket_buyer(self):
        print('at any point if you want to exit press "q"') 
        your_name = input("your name : ")
        if your_name == 'q':
            return
        no_of_tickets = input("how many tickets : ")
        if no_of_tickets == 'q':
            return
        seats = input("select the seats : ")
        if seats == 'q':
            return
        viewers.extend((self.movie_name,your_name,no_of_tickets,seats))
    


        for viewer in viewers:
            print("\nMovie : "+self.movie_name)
            print("name : "+your_name)
            print("Number of Tickets : "+no_of_tickets)
            print("Seat Number : "+seats)
            break



movie_tickets=TenetMovie(input("Which movie would you like to watch : "))
movie_tickets.ticket_buyer()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM