开发者

Python 3 Caesar cipher, help please

hi im trying to create a Caesar cipher using Python 3, the question is in the text, chapter 5 question 7, I have this so far but i keep getting this error message when i try to run the program and cant figure out why.

program:

  def main():
    print("This program executes a Caesar cipher for a string")

   word = input("Please enter word: ")

   key = input("Please enter the number of positions in the alphabet you wish to apply: ")

   message=""
   for ch in word开发者_JAVA百科:
      word= chr(ord(ch)+key)
      newWord =(message + word)
   print (newWord)

main()

Error:

Traceback (most recent call last):
  File "/Users/krissinger/Documents/programing /my graphics/delete.py", line 14, in <module>
    main()
  File "/Users/krissinger/Documents/programing /my graphics/delete.py", line 10, in main
    word= chr(ord(ch)+key)
TypeError: unsupported operand type(s) for +: 'int' and 'str'


word= chr(ord(ch)+key)

ord(ch) gives the integer value for that chr. Adding it with a string gives a TypeError, since the + operator is used differently for integers and strings. If you want key to be an int, you must do:

key = int(input("....")

Then, you can add them together.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜