India Languages, asked by bkharathik, 10 months ago

Write a python program to generate the ticket numbers for specified number of passengers traveling in a flight as per the details mentioned below:
The ticket number should be generated as airline:src:dest:number
where

Consider AI as the value for airline
src and dest should be the first three characters of the source and destination cities.
number should be auto-generated starting from 101
The program should return the list of ticket numbers of last five passengers.
Note: If passenger count is less than 5, return the list of all generated ticket numbers.
Sample Input Expected Output
airline = AI
source = Bangalore
destination = London
no_of_passengers = 10
output= ['AI:Ban:Lon:106', 'AI:Ban:Lon:107', 'AI:Ban:Lon:108', 'AI:Ban:Lon:109', 'AI:Ban:Lon:110']
airline = BA
source = Australia
destination = France
no_of_passengers = 2
output= ['BA:Aus:Fra:101', 'BA:Aus:Fra:102']

Answers

Answered by fiercespartan
14

First, to get this working, we will need to take inputs right?

airline, source, destination, no_of_passengers = input(), input()[:3], input()[:3], int(input())

Ok, so there might be a lot of questions for you on this part. If we read the question carefully, we are asked to print the first 3 letters of source and destination and [:3] is a string and list command which takes the first 3 letters of the input typed in by the users.

Now that we have all of the inputs taken, now let's jump right ahead to the fun part.

Our output format should be like this according to the question:

airline:source:destination:ticket_number

We have everything except the ticket_numbers. These have to be auto-generated and the one thing we can use in a situation like this is the range() function in python.

Let me summarize how the range() function in python works.

If we type in range(10), the outputs would be: 0,1,2,3,4,5,6,7,8,9

If we notice, the number 10 is not printed. So, in range function, the max number is not printed. In this case, the number 10 is not printed.

For the problem we have, the ticket numbers are printed from 101 and 101+no_of_passengers. That is our max limit.

Before we get it all together, let's make an empty list:

tickets = []

And now, we do everything and just add in tickets and print them accordingly.

Now, this is the part where we generate the tickets:

for ticket_number in range(101,101+no_of_passengers+1):

   tickets.append('%s:%s:%s:%d'%(airline,source,destination,ticket_number))

After this, if we print out list, we get our desired outcome. But, we need to check if they are greater than or less than 5 and for that, we use a if-else loop.

Now, here is the if-else loops:

if no_of_passengers > 5:

   print(tickets[-5:]) -- This prints  out the last five passengers.

else:

   print(tickets)

And... you are all set!

Here is the final code:

tickets = []

airline, source, destination, no_of_passengers = input('Enter the airline:'), input('Enter the source:')[:3], input('Enter the destination')[:3], int(input('Enter the total number of passengers:'))

for ticket_number in range(101,101+no_of_passengers):

   tickets.append('%s:%s:%s:%d'%(airline,source,destination,ticket_number))

if no_of_passengers > 5:

   print(tickets[-5:])  

else:  

   print(tickets)

Cheers! :)

Answered by dirali3010
4

Answer:

x = int(input('Enter a number: '))

airline = 'AL1'

src = input('Enter source: ')

dest = input('Enter Destination: ')

number = 101

for i in (0,x):

   lst=[]

   for number in range (101,101+x):

       lst.append((airline, src[0:3], dest[0:3], number))

       number = number + 1

if (x>5):

   print(lst[-5:])

else:

   print(lst)

Explanation:

The airline number is same for all the passengers. The source and destination is also same. Ask the user to number the source and the destination for the airline 'AL1'. The passenger number starts with 101 ie number = 101. for run a loop from 101 to 101+x , add the passengers in a list, increment the number.

To print the last five passengers, run an if else loop

Similar questions