title: Function That Receives and Rotates Character - Caesar Cipher
body: <p>I'm trying to create a function</p>
  
  <pre><code>rotate_character(char, rot)
  </code></pre>
  
  <p>that receives a character, "char" (a string with a length of 1), and an integer "rot". The function should return a new string with a length of 1, which is the result of rotating char by rot number of places to the right.</p>
  
  <p>So an input of "A" for char and "13" for rot would return</p>
  
  <pre><code>N
  </code></pre>
  
  <p>(with A having an initial value of 0, and B having an initial value of 1, etc). Capitalization should be maintained during rotation. </p>
  
  <p>I already created a function that returns the position of a letter in the alphabet by using a dictionary:</p>
  
  <pre><code>letter = input("Enter a letter: ")
  
  def alphabet_position(letter):
      alphabet_pos = {'A':0, 'a':0, 'B':1, 'b':1, 'C':2, 'c':2, 'D':3,
                      'd':3, 'E':4, 'e':4, 'F':5, 'f':5, 'G':6, 'g':6,
                      'H':7, 'h':7, 'I':8, 'i':8, 'J':9, 'j':9, 'K':10,
                      'k':10, 'L':11, 'l':11, 'M':12, 'm':12, 'N': 13,
                      'n':13, 'O':14, 'o':14, 'P':15, 'p':15, 'Q':16,
                      'q':16, 'R':17, 'r':17, 'S':18, 's':18, 'T':19,
                      't':19, 'U':20, 'u':20, 'V':21, 'v':21, 'W':22,
                      'w':22, 'X':23, 'x':23, 'Y':24, 'y':24, 'Z':25, 'z':25 }
      pos = alphabet_pos[letter]
      return pos  
  </code></pre>
  
  <p>I figure that I can use this function to get the initial value of (char) before rotation. </p>
  
  <pre><code>def rotate_character(char, rot)
      initial_char = alphabet_position(char)
      final_char = initial_char + rot
  </code></pre>
  
  <p>But my problem is that, if initial_char + rot is greater than 25, I need to wrap back to the beginning of the alphabet and continue counting. So an input of "w" (initial value of 22) + an input of 8 for rot should return</p>
  
  <pre><code>e
  </code></pre>
  
  <p>How do I say this using python? </p>
  
  <pre><code>if final_char &gt; 25, start at the beginning of the list and continue counting
  </code></pre>
  
  <p>And do I necessarily need to use the dictionary that I created in the alphabet_position function? <a href="https://stackoverflow.com/questions/41007646/python-function-that-receives-letter-returns-0-based-numerical-position-within">It was also suggested</a> that I find the character number by using Python's built-in list of letters, like this:</p>
  
  <pre><code>import string
  
  letter = input('enter a letter: ')
  
  def alphabet_position(letter):
      letter = letter.lower()
      return list(string.ascii_lowercase).index(letter)
  
  return(alphabet_position(letter))
  </code></pre>
  
  <p>I'm not sure which one of these is the better option to go with when you have to wrap while you're counting. Thanks for your help / suggestions! </p>
  
  <p><strong>EDIT</strong>:</p>
  
  <p>Now my code looks like this:</p>
  
  <pre><code>letter = input("enter a letter")
  rotate = input("enter a number")
  
  def rotate(letter, rotate):
      letter = letter.lower()
      return chr((ord(letter) + rotate - 97) % 26 + 97)
  
  print(rotate(letter))
  </code></pre>
  
  <p><strong>EDIT 2</strong>: </p>
  
  <pre><code>def rotate(letter, number):
      letter = letter.lower()
      shift = 97 if letter.islower() else 65
      return chr((ord(letter) + number - shift) % 26 + shift)
  
  letter = input('Enter a letter: ')
  number = int(eval(input('Enter a number: ')
  print(rotate(letter, number))
  </code></pre>
  
  <p>gave me a ParseError: "ParseError: bad input on line 8" (the print line)</p>