2014-10-04 40 views
0

我試着下面的代碼爲正常的示例問題,但我的程序不會進入'如果'條件?我不能去'如果'的條件

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

「爲什麼我不能找到一個完整的整數列表一個字符串?」 – vaultah 2014-10-04 17:01:41

+0

Python 2或Python 3?在Python 3中,你將有一個* string *值'bus_num',而不是一個整數。 – 2014-10-04 17:03:03

回答

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 
+0

bus_num = int(輸入(「輸入總線號碼\ n」))沒有工作。它顯示錯誤是Traceback(最近一次調用最後一次): 文件「C:\ Users \ RAHUL \ Desktop \ python progrms \ sample.py」,第6行,在 print(「available seats are \ n」+ seat_list) TypeError:不能將'list'對象隱式轉換爲str – 2014-10-04 17:09:33

+1

這是完全不同的錯誤,與您的問題中的問題完全無關。用''替換'+',併爲自己創建一本Python書。 – 2014-10-04 17:11:56

+0

謝謝Paul Griffihs :) – 2014-10-04 17:16:21

2

input()返回一個字符串,你需要將其轉換爲整數第一個:

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

變爲:

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

不僅是正確的代碼,也是一個解釋。 – 2014-10-04 17:04:17