2014-02-08 18 views
-1

下面是代碼:爲什麼我的代碼跳到最後並忽略我的代碼的一部分?

print "Welcome to the Database!" 

print "Simply type the correponding number to start!" 

print """ 

    1. Add a Student... 

    2. Search for a Student... 

    3. Edit a Student's Information... 

    4. Delete a Student... 

    5. Exit... 
""" 

datab = raw_input("What would you like to do?") 

if datab == 1: 
    dtabs = open("database.txt", "w") 
    que = raw_input("What's the student's name?") 
    dtabs.write(que) 
    dtabs.close() 

    ttrgrp = raw_input("Which tutor group do they belog to?") 

當我在終端運行它,它打印什麼會我喜歡做的,但那麼一旦輸入取在程序關閉。

+2

'raw_input'返回非int的字符串。 –

回答

5

raw_input()返回一個字符串,但是您正在比較一個整數。比較時,Python會不會把字符串:

>>> '1' == 1 
False 

比較對這裏的字符串:

if datab == '1': 

,因爲這簡化了驗證。

0

如果語句被跳過,那麼中的內容都將被跳過,因爲數據庫的整數形式永遠不會等於1。 raw_input()將返回值字符串。將您的if語句更改爲接受1作爲字符串:

if datab == '1': 
    #do something 
相關問題