2014-04-04 141 views
0

我是一個非常新的Python用戶(2.7),一直在學習Python Python The Hard Way課程,直到第37章,並決定閱讀其他一些學習資料並重新閱讀再次基礎知識,並在那裏做練習。我一直在讀通過這樣的:Python if語句工作不正常

http://anh.cs.luc.edu/python/hands-on/3.1/handsonHtml/ifstatements.html

,我只是這樣做:

3.1.4.1。研究生練習
寫一個program.baby.py,提示學生有多少學分。打印他們是否有足夠的學分來畢業。 (在芝加哥洛約拉大學需要120個學分畢業。)

,這是我的代碼:

print "How many credits do you currently have: " 
credits = raw_input("> ") 
if credits >= 120: 
    print "You have graduated!" 
else: 
    print "Sorry not enough credits" 

但沒有母校什麼號碼我輸入它只是給「對不起,沒有足夠的學分」作爲每次回答,爲什麼?我嘗試過移動一些東西,使它>而不是> =但沒有任何工作。林肯定這是愚蠢的簡單我想念,但我不能弄明白。

我已經在LPTHW課程中做過其他幾個if語句練習,並且從未遇到過問題。

+0

它讀取輸入爲字符串將其轉換爲int。 –

回答

4

raw_input()返回一個字符串:

>>> credits = raw_input("> ") 
> 150 
>>> type(credits) 
<type 'str'> 

你需要將其轉換爲int

credits = int(raw_input("> ")) 
+2

可以幫助提及'try/except'。 – tijko

0

在你的代碼,在if語句您比較一個str類型與int類型。所以它沒有按照你的推測工作。投creditint

print "How many credits do you currently have: " 
credits = raw_input("> ") 
credits = int(credits) 
if credits >= 120: 
    print "You have graduated!" 
else: 
    print "Sorry not enough credits"