如果我的问题看似微不足道,我道歉.我宁愿在聊天室里问这个问题; 但是,目前我的声誉太低了,所以我无法在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 …Run Code Online (Sandbox Code Playgroud) 最终我将能够在聊天室中发布这样的简单问题,但是现在我必须发布它.我仍在努力解决Python中的比较问题.我有一个包含我从文件中获取的字符串的列表.我有一个函数,它接受单词列表(以前从文件创建)和一些'密文'.我试图使用Shift Cipher破解密文.我的问题与比较整数是一样的.虽然我可以看到在尝试使用print语句进行调试时,我的密文将被转移到单词列表中的单词,但它永远不会计算为True.我可能正在比较两种不同的变量类型,或者/ n可能会抛弃比较.对于今天的所有帖子感到抱歉,我今天正在做很多练习问题,为即将到来的作业做准备.
def shift_encrypt(s, m):
shiftAmt = s % 26
msgAsNumList = string2nlist(m)
shiftedNumList = add_val_mod26(msgAsNumList, shiftAmt)
print 'Here is the shifted number list: ', shiftedNumList
# Take the shifted number list and convert it back to a string
numListtoMsg = nlist2string(shiftedNumList)
msgString = ''.join(numListtoMsg)
return msgString
def add_val_mod26(nlist, value):
newValue = value % 26
print 'Value to Add after mod 26: ', newValue
listLen = len(nlist)
index = 0
while index < listLen:
nlist[index] = (nlist[index] + newValue) …Run Code Online (Sandbox Code Playgroud)