我不能去'如果'的条件

时间:2014-10-04 17:00:35

标签: python

我尝试使用以下代码来解决正常的样本问题,但我的程序并没有进入' if'条件?

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")

2 个答案:

答案 0 :(得分:2)

input()返回一个字符串,您需要先将其转换为整数:

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

变为:

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

答案 1 :(得分:0)

更改此

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

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

否则你会遇到这个问题

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

>>> 5 in [1,2,3,4,5]
True