如果我的问题看起来微不足道,我深表歉意。我宁愿在聊天室里问这个;但是,我现在的名声太低了,所以我无法在 Python 聊天室问任何问题。我目前正在为一堂课学习 Python,老师给了我们一些练习题来帮助我们快速学习。我现在正在构建的函数需要一个数字列表并将其转换为字符串。我遇到的问题是我的 if 语句永远不会评估为真。我尝试了几种处理变量的方法,并添加了许多打印语句以查看它们是否应该相等,但无济于事。再次提前感谢。我保证我只是在研究和尝试了很多方法后才问,但现在不知所措......这是我的代码:
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