0

I am currently writing a ferry ticket reservation program that has a function to view the seatings (I'm using a list). The code below is going to be a function in my program. When running the code, when I input 1 it prints my list but if I input any other value it asks for another input.

For example if I input 2, the program asks for another input and if I input the same value (2) it will print what I want. But if I input a different value such as 3 when it asks the second time, it will ask for a third input. If my third input is the same as my second input, it will print what I want BUT if I input a different value, it will start to print and reach an error.

I have all 3 ferry seatings in one list so I wrote ferry1, ferry2, etc. just for testing purposes.

Bellow is my whole code. Sorry for the poor formatting of this post as it is my first complex program as well as my first time using a forum. Why is this happening and how can I solve this problem? Thanks in advance!

seatings = [  ["ferry1", "B2", "B3", "B4", "B5"],
           ["B6", "B7", "B8", "B9", "B10"],
           ["E1", "E2", "E3", "E4", "E5"],
           ["E6", "E7", "E8", "E9", "E10"],
           ["E11", "E12", "E13", "E14", "E15"],
           ["E16", "E17", "E18", "E19", "E20"],
           ["E21", "E22", "E23", "E24", "E25"],
           ["E26", "E27", "E28", "E29", "E30"],
           ["E31", "E32", "E33", "E34", "E35"],
           ["E36", "E37", "E38", "E39", "E40"],
           ["ferry2", "B2", "B3", "B4", "B5"],
           ["B6", "B7", "B8", "B9", "B10"],
           ["E1", "E2", "E3", "E4", "E5"],
           ["E6", "E7", "E8", "E9", "E10"],
           ["E11", "E12", "E13", "E14", "E15"],
           ["E16", "E17", "E18", "E19", "E20"],
           ["E21", "E22", "E23", "E24", "E25"],
           ["E26", "E27", "E28", "E29", "E30"],
           ["E31", "E32", "E33", "E34", "E35"],
           ["E36", "E37", "E38", "E39", "E40"],
           ["ferry3", "B2", "B3", "B4", "B5"],
           ["B6", "B7", "B8", "B9", "B10"],
           ["E1", "E2", "E3", "E4", "E5"],
           ["E6", "E7", "E8", "E9", "E10"],
           ["E11", "E12", "E13", "E14", "E15"],
           ["E16", "E17", "E18", "E19", "E20"],
           ["E21", "E22", "E23", "E24", "E25"],
           ["E26", "E27", "E28", "E29", "E30"],
           ["E31", "E32", "E33", "E34", "E35"],
           ["E36", "E37", "E38", "E39", "E40"] ]

import datetime
today = datetime.date.today()

def getchoice():
        ch=int(input("Please enter ferryID: "))
        return ch

if (getchoice()== 1):
        ID=1
        a=0
        b=2
        c=10
elif (getchoice()==2):
        ID=2
        a=10
        b=12
        c=20
elif (getchoice()==3):
        ID=3
        a=20
        b=22
        c=30

print("-"*35)
print("Ferry ID:", ID, "   ", "Date:",today)
print("-"*35)
print("Business Class")
for item in ferry[a:b] :
        print( "-"*33, "\n",
               item[0], " "*( 3-len( item[0] ) ),
               ":", item[1], " "*( 3-len( item[1] ) ),
               ":", item[2], " "*( 3-len( item[2] ) ),
               ":", item[3], " "*( 3-len( item[3] ) ),
               ":", item[4], " "*( 3-len( item[4] ) ))
print("-"*33)
print("Economy Class")
for item in ferry[b:c]:
        print( "-"*33, "\n",
               item[0], " "*( 3-len( item[0] ) ),
               ":", item[1], " "*( 3-len( item[1] ) ),
               ":", item[2], " "*( 3-len( item[2] ) ),
               ":", item[3], " "*( 3-len( item[3] ) ),
               ":", item[4], " "*( 3-len( item[4] ) ))
print("-"*33)
Jay Man
  • 31
  • 6
  • Another question is I feel like the printing of the seating list is extremely slow, is there anyway to speed it up? – Jay Man Jun 16 '18 at 18:01
  • I think this will be of help: https://stackoverflow.com/questions/23294658/asking-the-user-for-input-until-they-give-a-valid-response – Mr. T Jun 16 '18 at 18:10
  • I don't think so. The post in the link is talking about stopping the program from crashing when the user enters an invalid data but my main concern is my function is getting called twice if I'm not entering 1. Thanks anyway! – Jay Man Jun 16 '18 at 18:18
  • 1
    You can follow the [program execution on Python tutor](http://pythontutor.com/visualize.html#mode=edit). Maybe this will convince you that you should restructure your input routine. Hint: You call getchoice() more than necessary. – Mr. T Jun 16 '18 at 18:32
  • Dude that hint helped A TON. problem solved. Thanks a lot man! – Jay Man Jun 17 '18 at 03:37

0 Answers0