0

I tried the below code for a normal sample problem, but my program doesn't go into the 'if' condition?

bus_list=[1,2,3,4,5,6,7,8]
seat_list = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20]
bus_num= input("enter the bus number\n")
print(bus_num)
if bus_num in bus_list:
    print("available seats are \n" +seat_list)
    seat_take=input("enter you seat number\n")
    if seat_take in seat_list:
        print("seat available")
    else:
        print("sorry! seat taken")
else:
    print("bus doesn't exists")
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
Rahul Dhiman
  • 21
  • 1
  • 1
  • 3

2 Answers2

2

input() returns a string, you need to convert it to an integer first:

bus_num= input("enter the bus number\n")

becomes:

bus_num = int(input("enter the bus number\n"))
Crowman
  • 25,242
  • 5
  • 48
  • 56
0

Change this

bus_num = input("enter the bus number\n")

to

bus_num = int(input("enter the bus number\n"))

Otherwise you'll have this problem

>>> '5' in [1,2,3,4,5]
False

>>> 5 in [1,2,3,4,5]
True
Cory Kramer
  • 114,268
  • 16
  • 167
  • 218
  • bus_num = int(input("enter the bus number\n")) didnt worked. it showed me error that is Traceback (most recent call last): File "C:\Users\RAHUL\Desktop\python progrms\sample.py", line 6, in print("available seats are \n" +seat_list) TypeError: Can't convert 'list' object to str implicitly – Rahul Dhiman Oct 04 '14 at 17:09
  • 1
    That's a totally different error, completely unrelated to the issue in your question. Replace `+` with `,` and get yourself a Python book. – Crowman Oct 04 '14 at 17:11