pom*_*eii 0 python comparison loops nested
alphabet =["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"]
def decoder(input):
inputlist = list(input)
inputlength = len(input)
alphabetlength = len(alphabet)
result = "Decoded Sentence: "
for x in range(inputlength):
for y in range(alphabetlength):
if inputlist[x] is alphabet[y]:
print ("hi")
if y == 24:
result += "a"
if y == 25:
result += "b"
else:
result += alphabet[y+2]
if inputlist[x] is "(":
result += "("
if inputlist[x] is ")":
result += ")"
if inputlist[x] is ".":
result += "."
if inputlist[x] is " ":
result += " "
return result
Run Code Online (Sandbox Code Playgroud)
我的代码应该增加一个句子的字母表2. ex:a-> c,l-> n我把print("hi")语句检查if语句是否曾被评估为真但它从来没有.有人可以告诉我为什么吗?
is检查对象标识.由于您似乎正在测试两个字符串是否具有相同的值(不是同一个对象),因此您可以更好地服务==.例如:
if inputlist[x] == alphabet[y]
Run Code Online (Sandbox Code Playgroud)
您也可以为其他if语句进行相同的更新.