我创建了一个代码(应该)能够将字符串转换为ascii'代码'。但是,它并没有输出我想要的东西。这是有关的代码:
if Code_Decode=='C':
print("What is your 'Shift number'?")
SNum=int(input("> "))
print("What is your message?")
msg=input("> ")
code=[ord(c) for c in msg]
new_code=[x+(SNum) for x in code]
print(','.join(str(x) for x in code))
else:
print("What is your 'Shift number'?")
SNum=int(input("> "))
print("Type in your ASCII values, separated by a comma")
a = [int(x) for x in input().split(",")]
ans=[x-(SNum) for x in a]
ans2=[chr(i) for i in ans]
print(' '.join(str(x) for x in ans2))
用户应输入一个' Shift Number',然后输入一个字符串,并将其转换为ascii,并将Shift Number添加到每个单独的值。
但是,当我运行代码时,不会添加班次编号。忽略if和else行。
示例:(这是我运行代码时会发生的情况)
Do you want to Create(C) a message, or Decode(D) a message? (C/D)> C
What is your 'Shift number'?
> 2
What is your message?
> Hello World
72,101,108,108,111,32,87,111,114,108,100
Again? Y/N > Y
Do you want to Create(C) a message, or Decode(D) a message? (C/D)> D
What is your 'Shift number'?
> 2
Type in your ASCII values, separated by a comma
72,101,108,108,111,32,87,111,114,108,100
F c j j m U m p j b
正如您所看到的,该程序输出了一些混乱的字母。我该如何解决?
答案 0 :(得分:0)
Wrong: print(','.join(str(x) for x in code))
Right: print(','.join(str(x) for x in new_code))