我的代码无效,请帮忙!我有一个项目应该编程,我只是想不通。它给了我这个错误:
Traceback (most recent call last):
File "C:/Python27/breakingcodeccc.py", line 258, in <module>
decodeorencode()
File "C:/Python27/breakingcodeccc.py", line 14, in decodeorencode
encode ()
File "C:/Python27/breakingcodeccc.py", line 61, in encode
for letters in secretmessage:
TypeError: 'bool' is not interable
我已经尝试了一切来实现这个目标。当我推送解码它只是重复,当我编码它甚至不编码。顺便说一下,我是新手。
print "Hey there user"
global message
def decodeorencode():
valid_answer = False
while valid_answer == False:
message = str(raw_input('would you like to encode or decode? '))
if message.lower() == "encode" :
encode()
elif message.lower() == "decode":
print "We will now decode your message!"
response = str(raw_input( 'do you know the key? '))
if response in (['yes', 'Yes', 'y', 'Y']):
decode()
elif response in (['no', 'No', 'n', 'N']):
random_decode()
alphabetupper = ['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']
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"]
dmessage = []
emessage = []
rdmessage = []
def encode ():
print "We will now encode your message!"
secretmessage = str(raw_input("Please enter your message now."))
valid_anwser = True
keyy = int(raw_input("Please pick a key between 1 to 26"))
secretmessage = secretmessage.isupper()
for letters in secretmessage:
one_letter = letters
if one_letter.isalpha() == True:
number = alphabetupper.index(one_letter) + keyy
if number <= 26:
letter = alphabetupper[number]
emessage.append(letter)
break
else:
letter = number % 26
letter = alphabetupper[letter]
emessage.append(letter)
break
elif letters.isalpha == False:
emessage.append(letters)
break
return emessage
def decode():
valid_answer = True
secretmessage = str(raw_input("Please enter your message now."))
keyy = int(raw_input("Please pick a key between 1 to 26"))
keyy = - keyy
for letters in secretmessage:
if letters.isalpha()== True and letters.isupper() == True:
number = alphabetupper.index(letters)+ keyy
if number <= 26:
letter = alphabetupper[number]
dmessage.append(letter)
break
else:
letter = number%26
letter = alphabetupper[letter]
dmessage.append(letter)
break
elif letters.isalpha() == True and letters.islower() == True:
number = alphabet.index(letters) + keyy
if number <= 26:
letter = number
letter = alphabet [letter]
dmessage.append(letter)
break
else:
letter = number % 26
letter = alphabet[letter]
dmessage.append(letter)
break
elif letters.isalpha == False:
rdmessage.append(letter)
return dmessage
def random_decode ():
valid_answer = True
secretmessage = str(raw_input("Please enter your message now."))
valid_answer = False
keyy = 1
while valid_answer == False:
keyy = - keyy
for letters in secretmessage:
if letters.isalpha() == True and letters.isupper() == True:
number = alphabetupper.index(letters)+ keyy
if number <= 26:
letter = number
rdmessage.append(letter)
key += 1
break
else:
letter = number % 26
letter = alphabetupper[letter]
rdmessage.append(letter)
key += 1
break
elif letters.isalpha() == True and letters.islower() == True:
number = alphabet.index(letters)+ keyy
if number <= 26:
letter = number
rdmessage.append(letter)
key += 1
break
else:
letter = number % 26
letter = alphabet[letter]
rdmessage.append(letter)
key += 1
break
elif letters.isalpha == False:
rdmessage.append(letters)
return rdmessage
decodeorencode()
答案 0 :(得分:1)
这一行
secretmessage = secretmessage.isupper()
正在将您的消息转换为布尔值(true或false),这是isupper()
函数的返回值。您可能只想将upper()
转换为大写
答案 1 :(得分:1)
错误消息告诉您确切的错误; secretmessage
是布尔值(即True
或False
),您无法迭代它。错误源于这一行:
secretmessage = secretmessage.isupper()
str.isupper()
会返回True
或False
。而不是这个,你应该:
secretmessage = secretmessage.upper()
答案 2 :(得分:0)
secretmessage.upper()而不是.isupper()isupper()问一个问题,答案是真还是假。