'预期字符,但找到长度为#的字符串'加密程序不起作用 [英] 'expected a character, but string of length # found' Encryption Program not working

查看:170
本文介绍了'预期字符,但找到长度为#的字符串'加密程序不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的代码:

def Encryption(text):
   for I in text:
      string = ""
      ASCII = ord(text)
      Result = ASCII + Offset
      if Result > 126:
         Result -= 94
      else:
         Result = Result
      ResultASCII = chr(Result)
      string += ResultASCII

对于我的第一篇GCSE课程,我们必须编写一个加密程序.我们必须做的最后一部分是实际加密您的消息的部分.我使用了此代码,但是出现了以下错误:

For my first piece of GCSE coursework, we had to make an encryption program. The final part that we have to make is the part that actually encrypts your message. I've used this code, however it comes up with an error of:

TypeError: ord() expected a character, but string of length # found

如何获取它来检测字符串而不是字符?

How do I get it to detect a string instead of just a character?

推荐答案

ord参数不是字符串,必须为单个字符:

ord argument is not string, it must be single character:

>>> ord('a')
97

您可以尝试执行以下操作以遍历字符串:

you can try something like this to loop over string:

>>> a = 'hello'
>>> [ ord(x) for x in a ]
[104, 101, 108, 108, 111]

替换此:

ASCII = ord(text)

对此:

ASCII = ord(I)

这篇关于'预期字符,但找到长度为#的字符串'加密程序不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆