Python 插入字母和数字-将字母向右移动';编号';时代 def char(): 字母=str(输入(“输入字母表中的字母:”) x=int(输入(“将字母向右移动的输入值x次数:”) 新字母=ord(字母)+x 如果newLetter>90且newLetter122: 余数=新字母%122 newLetterI=97+余数 印刷品(chr(新字母I)) char()

Python 插入字母和数字-将字母向右移动';编号';时代 def char(): 字母=str(输入(“输入字母表中的字母:”) x=int(输入(“将字母向右移动的输入值x次数:”) 新字母=ord(字母)+x 如果newLetter>90且newLetter122: 余数=新字母%122 newLetterI=97+余数 印刷品(chr(新字母I)) char(),python,python-3.x,Python,Python 3.x,这是我的代码,它将字母向右移动了“多次”。 这是非常冗长的,但这是唯一的方法,我发现如何做到这一点,没有很多错误,我真的不明白。我只是想知道它是否正常,我只是想知道一旦字母到达Z或Z,包装是否正常。请检查一下: def char(): letter = str(input("Input Letter from alphabet: ")) x = int(input("Input Value to shift letter to the right x number of time

这是我的代码,它将字母向右移动了“多次”。
这是非常冗长的,但这是唯一的方法,我发现如何做到这一点,没有很多错误,我真的不明白。我只是想知道它是否正常,我只是想知道一旦字母到达Z或Z,包装是否正常。

请检查一下:

def char():
    letter = str(input("Input Letter from alphabet: "))
    x = int(input("Input Value to shift letter to the right x number of times: : "))
    newLetter = ord(letter) + x
    if newLetter > 90 and newLetter < 97:
        remainder = newLetter % 90
        newLetterI = 65 + remainder
        print(chr(newLetterI))
    elif newLetter > 122:
        remainder = newLetter % 122
        newLetterI = 97 + remainder
        print(chr(newLetterI))

char()
输出:

#!/usr/bin/python

def next_nchar(char, n):
    # 97, 122
    upper = None
    if char.isupper():
        upper = True
    lw = char.lower()
    start = ord(lw)
    num = ord(lw) + n
    if num > 122:
        ex = num - 122
    else:
        ex = None
    if not ex:
        r = range(start, num)
    else:
        r = range(start, 123) + range(97, 97+ex-1)
    if upper:
        return [chr(x).upper() for x in r]
    return [chr(x) for x in r]

for i in ['a', 'k', 'y', 'P']:
    print next_nchar(i, 5)

你应该用这个

['a', 'b', 'c', 'd', 'e']
['k', 'l', 'm', 'n', 'o']
['y', 'z', 'a', 'b', 'c']
['P', 'Q', 'R', 'S', 'T']
导入系统 def char(): 字母=sys.stdin.readline().strip() x=int(sys.stdin.readline()) oldLetter=ord(字母) 如果oldLetter>64且oldLetter<91: 如果(oldLetter+x)96和oldLetter<123:
如果(oldLetter+x)我可以从您的代码中找到一个问题,那就是概括似乎没有多大意义。例如,如果将新字母模化122,当移位为26时会遇到问题,而且由于必须记住ascii值,因此读取起来有点困难

我将尝试解释一个稍微改进的版本:

import sys
def char():
    letter = sys.stdin.readline().strip()
    x = int(sys.stdin.readline())
    oldLetter = ord(letter)
    if oldLetter > 64 and oldLetter < 91:
        if (oldLetter+x) <= 90:
            remainder = (oldLetter + x) % 65
            newLetter = 65 + remainder
        else:
            remainder = (oldLetter + x) % 90
            newLetter = 64 + remainder
        print(chr(newLetter))
    elif oldLetter > 96 and oldLetter < 123:
        if (oldLetter+x) <= 122:
            remainder = (oldLetter + x) % 97
            newLetter = 97 + remainder
        else:
            remainder = (oldLetter + x) % 122
            newLetter = 96 + remainder
        print(chr(newLetter))
char()
以下是一个更简洁的版本,没有任何注释:

letter = input("Enter a letter from the alphabet: ") # No need for str, it is already a string
shift = int(input("Enter your letter shift: "))

if letter.islower(): # Much better than worrying about ascii values
    initial = 'a'
else:
    initial = 'A'

letterIndex = (ord(letter) - ord(initial)) # So 'a' would have index 0, 'b' 1, 'z' 25
newIndex = (letterIndex + shift) % 26 # Making sure our index is from 0 to 25
newLetter = chr(ord(initial) + newIndex) # after 'a' or 'A', increment by newIndex
print newLetter

这是最具可读性的方法,IMO:

letter = input("Enter a letter from the alphabet: ")
shift = int(input("Enter your letter shift: "))

initial = 'a' if letter.islower() else 'A'

newIndex = (ord(letter) - ord(initial) + shift) % 26
print(chr(ord(initial) + newIndex))

可能是重复的谢谢。我看到了这个,但我不太明白它的代码。
import string

def shiftchar(char, n):
    if char.isupper(): 
        letters = string.uppercase # all uppercase letters in alphabetical order
    elif char.islower():
        letters = string.lowercase # all lowercase letters in alphabetical order
    try:
        initcharpos = letters.index(char)
        newchar = letters[(initcharpos + n) % 26]
        # get the letter n characters later, with wraparound
        return newchar
    except (NameError, ValueError):
        # NameError: char is mixed-case or non-English, so letters is never defined
        # ValueError: char not found in letters; probably a multi-char string
        raise TypeError("Expected a single English char as first argument")

print shiftchar('A', 5)   # F
print shiftchar('z', 1)   # a
print shiftchar('foo', 5) # TypeError