奋斗在循环外打印一个for循环中的字符串列表和if循环

时间:2017-07-10 23:57:09

标签: python loops

我在for循环中的if语句中的for循环中有这个列表bravo_uncode_message=[(0000000),(0000001),(0000010),(0000011), ...],我无法从3个循环中得到它,我尝试了这个命令bravo_uncode_message.extend和{{1}但似乎没有工作

bravo_uncode_message.append

我得到的错误是import binascii response=raw_input("Decrypt a message or encrypt a message? D/E : ") if response== "E": message_to_encrypt=raw_input("Introduce message to encrypt: ") key= raw_input("Insert key:") abc=['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'] if any(word in key for word in abc): print 'Not a valid key...' elif len(key)!= 7: print 'Invalid key' else: key=key*len(message_to_encrypt) binary_message = bin(int(binascii.hexlify(message_to_encrypt), 16)) print binary_message binary_message= list(binary_message) del binary_message[0] print binary_message del binary_message[0] recovery_number = binary_message[0] print binary_message del binary_message[8 - 1::8] print binary_message binary_message = ''.join(map(str,binary_message)) print binary_message print key a = binary_message b = key xored_message= int(a,2) ^ int(b,2) print bin(xored_message)[2:].zfill(len(a)) print message_to_encrypt elif response== "D": encrypted_message = raw_input("Introduce message to decrypt: ") beta_uncode_message = [tuple(map(int, format(i, '07b'))) for i in range(128)] g=len(encrypted_message)/7 n=0 for i in beta_uncode_message: n=n+1 if n<129: alpha_uncode_message=beta_uncode_message[n-1]*g x=tuple(map(str,alpha_uncode_message)) alpha_uncode_message=x a=encrypted_message b=alpha_uncode_message b=''.join(map(str,b)) charlie_uncode_message = int(a, 2) ^ int(b, 2) bravo_uncode_message=bin(charlie_uncode_message)[2:].zfill(len(a)) new_list = [] q = 7 for start_index in range(0, len(bravo_uncode_message),q): new_list.extend(bravo_uncode_message[start_index:start_index + q]) new_list.append('0') bravo_uncode_message[:-1] bravo_uncode_message = ''.join(map(str, bravo_uncode_message)) binary_axis = '0b' bravo_uncode_message = binary_axis + bravo_uncode_message bravo_uncode_message.extend(bravo_uncode_message) k = int(bravo_uncode_message, 2) uncode_message = binascii.unhexlify('%x' % k) print uncode_message if n>129: break else: break print bravo_uncode_message

1 个答案:

答案 0 :(得分:0)

您重新定义了从bravo_uncode_messagelist的{​​{1}}类型,然后在其上调用了strextend()extend()不是list的方法。

str

此外,您还重新定义了从bravo_uncode_message = ''.join(map(str, bravo_uncode_message)) binary_axis = '0b' bravo_uncode_message = binary_axis + bravo_uncode_message bravo_uncode_message.extend(bravo_uncode_message) b的{​​{1}}类型,这可能在将来出现问题。

tuple

希望这会有所帮助。欢迎来到StackOverflow。