使用ord和chr转换字母

腔室

我正在尝试做一个函数,将每个单词中的每个字母按值向右移,这些单词将来自列表,我将使用编写代码的“ open”函数将其打开,在这里我面临一些困难是我的代码

def test():
value=eval(input("Value here!"))
with open ("word-text.txt","r") as f:
    for ord in (f):
        print (ord)
        for chr in ord:
            print (chr)
            #nice=(chr[len(ord)+value])
        ''.join([chr(ord(i)+2) for i in s])
        print (i)

这是我得到的输出

Value here!2
apples

a
p
p
l
e
s


Traceback (most recent call last):
  File "<pyshell#54>", line 1, in <module>
    test()
  File "/Users/user/Desktop/Outlook(1)/lab6.py", line 59, in test
    ''.join([chr(ord(i)+2) for i in s])
NameError: global name 's' is not defined

我有以下单词的列表苹果橘子西瓜奇异果西葫芦胡萝卜秋葵墨西哥胡椒辣椒黄瓜香蕉以及使用此代码时

ef test():
value=eval(input("Value here!"))
with open ("word-text.txt","r") as f:
    for ord in (f):
        print (ord)
        for chr in ord:
            print (chr)
            nice=(chr[len(ord)+value])
            print (nice)

我得到这个输出

Value here!2
apples

a
Traceback (most recent call last):
  File "<pyshell#56>", line 1, in <module>
    test()
  File "/Users/user/Desktop/Outlook(1)/lab6.py", line 58, in test
    nice=(chr[len(ord)+value])
IndexError: string index out of range
>>> 

我在这里做错了什么?以及最适合使用的代码,以使其正常工作

谢谢

jrd1

尽管您的方法总体上是正确的,但正如其他人所指出的那样,存在一些问题。由于您似乎难以理解该怎么做以及如何解决该问题,因此我整理了您的代码(在此处和此处进行了一些编辑),并大量记录了我对代码的编辑,以便您可以理解该方法应为:

def test():
    value=input("Value here!") #eval is bad AND dangerous practice. Avoid using it.
    with open ("word-text.txt","r") as f:
        #At this point you can do one of two things:
        #1) read the entire file line by line
        #2) read the entire file into a list, and loop/iterate over that list
        #In this example, I'll read the file line by line:
        nice_strings = []
        for line in f:
            line = line.strip() #remove any extraneous whitespace from the line (e.g. spaces and newlines)
            nice_str_chars = []
            #At this point, you want to read EACH character in the line
            #N.B.: ord is the name of a built-in function. Use another name like `char` (below):
            for char in line:
                #Since you're in this loop, you are dealing with EACH individual character of each line
                #And, as you want to "shift" the character by some number of places
                #Python doesn't allow you to natively shift strings by arbitrary amounts,
                #so, the only way we can do so is to convert EACH character into its numeric representation.
                #In order to do so, you have to use the `ord` function which converts characters into
                #their respective integer representation.
                int_of_char = ord(char)
                #At this point, the character has been converted to it's integer equivalent
                #NOW, you can "shift" it. In other words, add the value to the `int_of_char`:
                int_of_char += value #update the value of the variable
                #At this point, the value has been modified, and the INTEGER value of the character has been
                #shifted. However, we don't want the shifted INTEGER value: we want the shifted CHARACTER value.
                #So, how do we convert integers to characters? Python has a builtin function called `chr` which
                #does exactly that.
                #So, let's convert the shifted INTEGER to a shifted CHARACTER using `chr`:
                nice_char = chr(int_of_char)
                #print it out the original and modified characters so you can see what's in the string (this step is optional)
                print char, '=>', nice_char
                #At this point we've successfully shifted the character. However, since this is just A character of the original
                #string, we need a way to save the updated characters as a string. There are a number of ways to do this.
                #For simplicity, we can use a list `nice_str_chars`:
                nice_str_chars.append(nice_char)
            #At this point we've looped over all the characters in ONE LINE, and we've calculated and stored the shifted
            #characters for that same line. However, since we've stored the characters in a list: `nice_str_chars`, we
            #have to convert it to a string. We can do that using the `join` method on an empty string, with 
            #`nice_str_chars` as the list (as we want to join the characters of the list into a string):
            nice_str = ''.join(nice_str_chars)
            #And, since you want to shift ALL the lines, you ought to append the corrected line to a list that can later be
            #returned:
            nice_strings.append(nice_str)
            #you can print it out if necessary:
            print line, '=>', nice_str, '\n'
        #At this point, you've successfully looped over the entire file, shifted all lines by a user entered integer amount,
        #now, all you have to do is return it, or do something else with them (i.e. write the updated strings to another file).
        #That part, I'll leave up to you.
        return nice_strings

这种方法的唯一问题是它不是圆形的(即-它不会循环返回:例如,z移位3不会变成c),因为值将超过其各自字符集的上限(如OUTPUT中所示)以下部分)。而且,如果输入的值非常大(即大于您正在使用的字符集的范围,则将变得乱码)。

但是,我不知道这是否是您的问题的要求(这可能不是您所需要的(因为您基本上按照先前的问题所建议的那样对密码进行了原始加密)),但是我还是想提一提。

输出:

Value here!2
a => c
p => r
p => r
l => n
e => g
s => u
apples => crrngu 

o => q
r => t
a => c
n => p
g => i
e => g
s => u
oranges => qtcpigu 

w => y
a => c
t => v
e => g
r => t
m => o
e => g
l => n
o => q
n => p
watermelon => ycvgtognqp 

k => m
i => k
w => y
i => k
kiwi => mkyk 

z => |
u => w
c => e
c => e
h => j
i => k
n => p
i => k
zucchini => |weejkpk 

c => e
a => c
r => t
r => t
o => q
t => v
carrot => ecttqv 

o => q
k => m
r => t
a => c
okra => qmtc 

j => l
a => c
l => n
a => c
p => r
e => g
n => p
o => q
jalapeno => lcncrgpq 

p => r
e => g
p => r
p => r
e => g
r => t
pepper => rgrrgt 

c => e
u => w
c => e
u => w
m => o
b => d
e => g
r => t
cucumber => ewewodgt 

b => d
a => c
n => p
a => c
n => p
a => c
banana => dcpcpc 

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

等价于python的ord()和chr()吗?

来自分类Dev

用chr和ord大写每个单词

来自分类Dev

如何仅在A到Z和0到9的情况下使用ORD和CHR?

来自分类Dev

如何仅在A到Z和0到9的情况下使用ORD和CHR?

来自分类Dev

我如何将chr转换为ord?- Python

来自分类Dev

用多个字符转换chr()/ ord()

来自分类Dev

使用字典转换字母

来自分类Dev

尝试在Python中不使用'ord()'转换空格

来自分类Dev

ord(c) 和 chr(n) 有什么作用,这段代码输出什么,为什么?我主要不明白下面的代码

来自分类Dev

chr(13)和chr(10)之间的区别

来自分类Dev

将Ord限制为仅字母

来自分类Dev

使用ord函数(ord(B [0])-ord('0'))

来自分类Dev

使用ord函数(ord(B [0])-ord('0'))

来自分类Dev

将QBasic CHR $(13)+ CHR $(10)转换为C#

来自分类Dev

Haskell 中的 ord 和 complex

来自分类Dev

使用星号显示和发现字母

来自分类Dev

使用jQuery更改字母和单词的间距

来自分类Dev

仅使用数字,数字字母,字母数字和字母的组合对数组排序

来自分类Dev

如何使用Javascript将字母转换为数字?

来自分类常见问题

在Python中使用字典将数字转换为字母

来自分类Dev

使用希伯来字母将数字转换为字符串

来自分类Dev

如何使用.join()将字母转换为字符串

来自分类Dev

在Python中使用字典将数字转换为字母

来自分类Dev

您将如何创建不需要的模块,例如chr,ord,inject等标准方法?

来自分类Dev

使用For循环将数字转换为字母并输出字母平方

来自分类Dev

无法使用指针将小写字母转换为大写字母

来自分类Dev

Python转换字母数字

来自分类Dev

遗失字母,基础转换

来自分类Dev

Python转换字母数字

Related 相关文章

热门标签

归档