如何在python中移动和循环字符

时间:2018-03-05 19:01:27

标签: python string mod

我想增加每个字符的ASCII值,如果值&gt;我想循环(环绕) Imports System.Runtime.InteropServices Public Class Form1 Private WithEvents Tmr As New Timer With {.Interval = 2000} Private Const MOUSEEVENTF_WHEEL As Integer = &H800 'The amount of movement is specified in mouseData. Private Const MOUSEEVENTF_HWHEEL As Integer = &H1000 'Not available for 2000 or XP <DllImport("user32.dll", EntryPoint:="mouse_event")> _ Private Shared Sub mouse_event(ByVal dwFlags As UInteger, ByVal dx As Integer, ByVal dy As Integer, ByVal dwData As Integer, ByVal dwExtraInfo As UInteger) End Sub Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Me.KeyPreview = True Tmr.Start() End Sub Private Sub Tmr_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles Tmr.Tick MouseScroll(False, 10) 'scrolls Down 10 wheel clicks 'MouseScroll(True, 1) 'scrolls Up 1 wheel click End Sub Private Sub MouseScroll(ByVal up As Boolean, ByVal clicks As Integer) If up Then mouse_event(MOUSEEVENTF_WHEEL, 0, 0, (clicks * 120), 0) Else mouse_event(MOUSEEVENTF_WHEEL, 0, 0, -(clicks * 120), 0) End If End Sub End Class

z

如果#small chars: for char in string.ascii_lowercase: print factName + '(' + char + ', ' + chr(((ord(char)+key) % ord('z'))) + ').' &gt;&gt; key = 3变为adwx不正确!

1 个答案:

答案 0 :(得分:2)

嗯,你减少了所有mod ord(&#39; z&#39;),好像字母表的内部值是从零开始的。正如您所发现的那样,不是的情况。相反,您需要进行溢出检查,然后减去数据集的宽度。基本逻辑是

if ord(chr) + key > ord('z'):
    result = ord(chr) + key - 26

这足以让你感动吗?

你也可以使用一个简单的翻译表,从旧的字母表中构建一个新的字母表:剪切前3个字符并将它们附加到翻译字符串的后面:

blank_code = 'abcdefghijklmnopqrstuvwxyz'
new_code = blank_code[-n:] + blank_code[:n]

这会给你一个相应的&#34;字母&#34;使用。另请参阅translate方法以快速使用这些方法。