如何在多行上打印字符串列表?如何防止我的程序提前退出?

2024-04-18 11:40:15 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在编写一个项目,要求用户为一架10人的飞机填写座位。我有整个事情的代码,它运行的方式,我希望它,但有两个小细节,我无法解决我自己。首先是这架飞机最多能容纳10名乘客。我用了一系列的绳子来容纳所有不同的乘客。但是,由于我的设置方式,一旦输入第10名乘客,程序就会因我设置代码的方式而退出。(我确信使用循环会更容易,但我不知道怎么做)。一旦你看到下面的代码,你就会明白我的意思。输入第10位乘客后,我应该能够运行所有显示屏选项。你知道吗

第二,我需要能够将乘客列表显示为垂直显示列表,但是每次尝试使用拆分字符串列表时,它都会显示为新行上的每个字母。输出应如下所示: 乘客#1=姓名 乘客#2=姓名 等等

但是,mine显示为Passenger#1=Name Passenger#2=Name,依此类推。你知道吗

任何帮助解决这些问题将不胜感激。你知道吗

# Seating List

Seating_Chart = ["Seat #1 = Empty", "Seat #2 = Empty", "Seat #3 = Empty", "Seat #4 = Empty", "Seat #5 = Empty", "Seat #6 = Empty", "Seat #7 = Empty", "Seat #8 = Empty", "Seat #9 = Empty", "Seat #10 = Empty"]
Seating_Chart = ' '.join(Seating_Chart)

# Display Menu

Display_Menu = """1. Assign Seat.
2. Print Seat Map.
3. Print Boarding Pass.
-1. Quit"""
print(Display_Menu)
print()

# Gets User Input

User_Input = input("")

# If/Else Statements
while User_Input != "-1":
    if User_Input == "1":
        name = input("Please enter your first name: ")
        Seating_Chart = Seating_Chart.replace("Empty", name, 1)
        Split_List = Seating_Chart.split(" ")
        if "Empty" not in Seating_Chart:
            print("Next flight leaves in 3 hours.")
        print()
    elif User_Input == "2":
        print("***************************************")
        print(Seating_Chart)
        print("***************************************")
        print()
    elif User_Input == "3":
        print("""Type 1 to get Boarding Pass by Seat Number
Type 2 to get Boarding Pass by name""")
        print()
        User_Choice = input("")
        while User_Choice != "1" and User_Choice != "2":
            User_Choice = input("""Type 1 to get Boarding Pass by Seat Number
Type 2 to get Boarding Pass by name""")
            print()
        if User_Choice == "1":
            Seat_Number = int(input("What is the seat number: "))
            if Seat_Number > 10:
                print("Invalid number--no boarding pass found")
            print()
            print("======= Boarding Pass =======")
            print("     Seat #:", Seat_Number)
            print("     Passenger Name:", Split_List[((Seat_Number * 4) - 1)])
            print("=============================")
            print()
        elif User_Choice == "2":
            Passenger_Name = input("Enter passenger name: ")
            if Passenger_Name in Split_List:
                print(Split_List)
                Passenger_Name_Index = Split_List.index(Passenger_Name)
                print(Passenger_Name_Index)
                Passenger_Name_Method_Two = Split_List[Passenger_Name_Index]
                Seat_Index = Passenger_Name_Index - 2
                if Seat_Index >= 0 and Seat_Index <= 3:
                    Printed_Seat = 1
                elif Seat_Index >= 4 and Seat_Index <= 7:
                    Printed_Seat = 2
                elif Seat_Index >= 8 and Seat_Index <= 11:
                    Printed_Seat = 3
                elif Seat_Index >= 12 and Seat_Index <= 15:
                    Printed_Seat = 4
                elif Seat_Index >= 16 and Seat_Index <= 19:
                    Printed_Seat = 5
                elif Seat_Index >= 20 and Seat_Index <= 23:
                    Printed_Seat = 6
                elif Seat_Index >= 24 and Seat_Index <= 27:
                    Printed_Seat = 7
                if Seat_Index >= 28 and Seat_Index <= 31:
                    Printed_Seat = 8
                if Seat_Index >= 32 and Seat_Index <= 35:
                    Printed_Seat = 9
                if Seat_Index >= 36 and Seat_Index <= 39:
                    Printed_Seat = 10
                print()
                print("======= Boarding Pass =======")
                print("     Seat #:", Printed_Seat)
                print("     Passenger Name:", Passenger_Name_Method_Two)
                print("=============================")
                print()
            else:
                print("No passenger with that information could be found.")
    print(Display_Menu)
    User_Input = input("")
if User_Input == "-1":
    print("Have a nice day!")

Tags: andnameinputindexifchartemptyprint
1条回答
网友
1楼 · 发布于 2024-04-18 11:40:15

请看下面的解决方案 您可以使用计数器计算乘客数量并添加验证。 2) 您可以拆分列表并添加新行。你知道吗

Seating_Chart = ["Seat #1 = Empty", "Seat #2 = Empty", "Seat #3 = Empty", "Seat #4 = Empty", "Seat #5 = Empty", "Seat #6 = Empty", "Seat #7 = Empty", "Seat #8 = Empty", "Seat #9 = Empty", "Seat #10 = Empty"]
Seating_Chart = ' '.join(Seating_Chart)

# Display Menu

Display_Menu = """1. Assign Seat.
2. Print Seat Map.
3. Print Boarding Pass.
-1. Quit"""
print(Display_Menu)
print()

# Gets User Input

User_Input = input("")
count = 0
# If/Else Statements
while User_Input != "-1":
    if User_Input == "1":
        count = count + 1

        if (count <=10):
            name = input("Please enter your first name: ")
            Seating_Chart = Seating_Chart.replace("Empty", name, 1)
            Split_List = Seating_Chart.split(" ")
            if "Empty" not in Seating_Chart:
                print("Next flight leaves in 3 hours.")
            print()
        else:
            print("The plane cannot hold more passengers.")
    elif User_Input == "2":
        print("***************************************")

        for item in Seating_Chart.split("Seat #"):
            if (str(item)!=""):
                print ("Seat #"+str(item))


        print("***************************************")
        print()
    elif User_Input == "3":
        print("""Type 1 to get Boarding Pass by Seat Number
Type 2 to get Boarding Pass by name""")
        print()
        User_Choice = input("")
        while User_Choice != "1" and User_Choice != "2":
            User_Choice = input("""Type 1 to get Boarding Pass by Seat Number
Type 2 to get Boarding Pass by name""")
            print()
        if User_Choice == "1":
            Seat_Number = int(input("What is the seat number: "))
            if Seat_Number > 10:
                print("Invalid number no boarding pass found")
            print()
            print("======= Boarding Pass =======")
            print("     Seat #:", Seat_Number)
            print("     Passenger Name:", Split_List[((Seat_Number * 4) - 1)])
            print("=============================")
            print()
        elif User_Choice == "2":
            Passenger_Name = input("Enter passenger name: ")
            if Passenger_Name in Split_List:
                print(Split_List)
                Passenger_Name_Index = Split_List.index(Passenger_Name)
                print(Passenger_Name_Index)
                Passenger_Name_Method_Two = Split_List[Passenger_Name_Index]
                Seat_Index = Passenger_Name_Index - 2
                if Seat_Index >= 0 and Seat_Index <= 3:
                    Printed_Seat = 1
                elif Seat_Index >= 4 and Seat_Index <= 7:
                    Printed_Seat = 2
                elif Seat_Index >= 8 and Seat_Index <= 11:
                    Printed_Seat = 3
                elif Seat_Index >= 12 and Seat_Index <= 15:
                    Printed_Seat = 4
                elif Seat_Index >= 16 and Seat_Index <= 19:
                    Printed_Seat = 5
                elif Seat_Index >= 20 and Seat_Index <= 23:
                    Printed_Seat = 6
                elif Seat_Index >= 24 and Seat_Index <= 27:
                    Printed_Seat = 7
                if Seat_Index >= 28 and Seat_Index <= 31:
                    Printed_Seat = 8
                if Seat_Index >= 32 and Seat_Index <= 35:
                    Printed_Seat = 9
                if Seat_Index >= 36 and Seat_Index <= 39:
                    Printed_Seat = 10
                print()
                print("======= Boarding Pass =======")
                print("     Seat #:", Printed_Seat)
                print("     Passenger Name:", Passenger_Name_Method_Two)
                print("=============================")
                print()
            else:
                print("No passenger with that information could be found.")
    print(Display_Menu)
    User_Input = input("")
if User_Input == "-1":
    print("Have a nice day!")

相关问题 更多 >