如果我的问题看起来微不足道,我道歉。我宁愿在聊天室里问这个问题;但是,我现在的名声太低了,所以我不能在Python聊天室问任何事情。我目前正在为一节课学习Python,老师给了我们一些练习题,让我们开始练习。我正在构建的函数现在接受一个数字列表并将其转换为字符串。我现在的问题是,我的if语句的计算结果从来都不是真的。我尝试了几种处理变量的方法,并添加了许多print语句,看看它们是否应该相等,但都没有效果。再次提前感谢。我保证我只是在研究和尝试了很多方法之后才问的,但现在我不知所措…以下是我的代码: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