2015-02-11 22 views
-3

我有一個測驗,每次他們回答正確的答案時都會增加積分(呃,它的確如此),我想檢查用戶是否已收集超過0分。檢查用戶是否從我的測驗中收集了任何積分

這裏的代碼,我要檢查,如果用戶已經超過零點(它不工作,它只是一片全碼):

def end_of_quiz(): 
    global score 
    if score > "0" : 
     print("Well Done, Your score is:") 
     print(score) 
    else: 
     print("Sorry, you didn't get any points, you shall try again!") 

我應如何改變它,使其工作

+0

比較它應該是'總分> 「0」'或'得分> 0'? '「0」是字符串文字,'0'是數字0 ... – 2015-02-11 18:19:44

+0

是否有區別?我認爲它應該是'score>「0」' – Deimantas 2015-02-11 18:20:40

+0

您發佈的代碼片段至少有三個不同的問題。但更重要的是,如果用戶的積分多於0,你實際上希望做什麼? – aestrivex 2015-02-11 18:20:48

回答

2

您正在使用string literal for zero, "0",而不是數字零(0)進行比較。

這將計算爲False所有數字:

>>> -1 > "0" 
False 
>>> -10**10 > "0" 
False 
>>> 10**10 > "0" 
False 
>>> 1 > "0" 
False 
>>> 1 > 0 
True 

所以取而代之,改變你的方法和數值爲零

def end_of_quiz(): 
    global score 
    if score > 0: 
     print("Well Done, Your score is:") 
     print(score) 
    else: 
     print("Sorry, you didn't get any points, you shall try again!") 
相關問題