簡體   English   中英

Python If語句從不評估為True

[英]Python If Statement Never Evaluates to True

如果我的問題看似微不足道,我深表歉意。 我寧願在聊天室問這個; 但是,目前我的聲譽太低,因此我無法在Python聊天室中提出任何問題。 我目前正在上一堂課,學習Python,老師給了我們一些練習上的問題,以使我們迅速成長。 我正在構建的函數現在接受一個數字列表,並將其轉換為字符串。 我遇到的問題是我的if語句永遠不會評估為true。 我嘗試了幾種使用變量的方法,並添加了許多打印語句以查看它們是否應該相等,但無濟於事。 再次感謝。 我保證我只是在研究和嘗試了許多方法之后才問,但是現在很茫然……這是我的代碼:

def nlist2string(nlist):
    characters = ['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z']
    numbers = ['0','1','2','3','4','5','6','7','8','9','10','11','12','13','14','15','16','17','18','19','20','21','22','23','24','25']
    newList = []
    nListLen = len(nlist)         # var msgLen will be an integer of the length

    print 'Number list before conversion: ', nlist

    index = 0
    while index < nListLen:
        print 'Index at: ', nlist[index]
        num = nlist[index]
        print 'Is num equal to nlist indexed? ', num
        newNum = num % 26

        i = 0
        while i < 26:
            num1 = newNum
            num2 = numbers[i]
            print 'num1 = ', num1
            print 'num2 = ', num2
            if (num1 == num2):
                newList.append(characters[i])
                print 'Here is the current newList: ', newList
            else:
                print 'They never equal each other.'
            i = i + 1
        index = index + 1
    return newList

    numMessage = [28, 0, 33]
    convertedNumMsg = nlist2string(numMessage)
    print 'Number list after conversion: ', convertedNumMsg

您正在嘗試將整數與字符串進行比較,嘗試將numbers的定義更改為以下內容:

numbers = [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25]

或者, numbers = range(26)

當前,當比較num1num2 ,您將進行類似4 == '4' ,這將永遠不會成立:

>>> 4 == '4'
False

作為更改創建numbers列表的方式的替代方法,可以在比較之前將num2轉換為整數,或者將num1轉換為字符串,因此num1 == int(num2)str(num1) == num2

您有一個字符串列表,數字為0-25。
在Python中,字符串從不等於數字,因此num1 == num2始終為False。

所以應該

numbers = [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25]

更合適(並且可以工作)。

甚至更好

numbers = range(26)

如果不想編輯numbers的值,請使用以下條件:

if num1 == int(num2):

這會將num2轉換為整數,這就是您要執行的操作。
另外,在這種情況下,您可以使用map(內置函數) ,以提高可讀性,例如:

numbers = map(str, range(26))

上面的答案正確地解決了這個問題,但是作為一個一般提示:將值列表簡化為單個值時,請使用reduce函數。 我當然意識到這是一個學習練習,但是了解該工作的相關內置功能可能會很有用。 這會使您的函數更短:

def nlist2string(nlist):

    def convert_to_alpha(s):
        if isinstance(s,str): //check if s is already a string
            return s          //if it is, return it unchanged
        else:
            return str(unichr(s+97)) //otherwise, get the corresponding alphabet
                                     //and return that
    def reduce_func(x,y):
        //convert the numbers to alphabets
        //and join the two together
        return convert_to_alpha(x) + convert_to_alpha(y) 

    return reduce(reduce_func, nlist)

例如,輸出來自:

l = [7,4,11,11,14]
print nlist2string(l)

是字符串"hello"

reduce函數有兩個參數,一個用於將列表折疊為單個值的函數,以及一個列表。

作為reduce的更簡單示例:

function add(x,y):
    return x + y

print reduce(add, [1, 4, 3, 10, 5])
//Output: 23

在Python中,字符串文字和數字是對象的不同類型,並且比較不相等。

1 == "1"

返回False。

您正在遍歷數字字符串列表,但這不是實際數字列表。

您可以使用range()函數(這是內置的python)來生成數字列表,而無需手動輸入。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM