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()
这是我的代码,将字母向右移动'数'次。 这是一个很长的啰嗦,但这是我找到如何做到这一点的唯一方法,没有很多我不理解的错误。我只是想知道它是否正常,只是想知道一旦字母表达到Z或z ok就会回绕。
答案 0 :(得分:0)
请你检查一下:
#!/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']
答案 1 :(得分:0)
你应该像这样使用
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()
答案 2 :(得分:0)
我可以通过您的代码找到的一个问题是环绕似乎没有多大意义。如果你对你的newLetter进行模数化,那么当你有26个移位时会遇到问题,并且由于你必须记住ascii值,所以它有点难以阅读。
我将尝试解释稍微改进的版本:
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
这是一个更简洁的版本,没有评论:
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))
答案 3 :(得分:0)
这是最可读的方式,IMO:
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