Note that there are some explanatory texts on larger screens.

plurals
  1. POCaesar cipher fails when output character is beyond 'z'
    text
    copied!<p>Just to be clear, I am asking this because I have tried it for about 1.5 hours and can't seem to get any results. I am not taking a programming class or anything but I have a lot of free time this summer and I am using a lot of it to learn python from this book. I want to know how I would complete this problem. </p> <p>The problem asks for you to create a program which runs a "caesar cipher" which shifts the ascii number of a character down by a certain key that you choose. For instance, if I wanted to write sourpuss and chose a key of 2, the program would spit out the ascii characters all shifted down by two. So s would turn into u, o would turn into q (2 characters down the ascii alphabet...). </p> <p>I could get that part by writing this program.</p> <pre><code>def main(): the_word=input("What word would you like to encode? ") key=eval(input("What is the key? ")) message="" newlist=str.split(the_word) the_word1=str.join("",the_word) for each_letter in the_word1: addition=ord(each_letter)+key message=message+chr(addition) print(message) main() </code></pre> <p>Running this program, you get the following:</p> <pre><code>What word would you like to encode? sourpuss What is the key? 2 uqwtrwuu </code></pre> <p>Now, the next question says that an issue arises if you add the key to the ascii number and that results in a number higher than 128. It asks you to create a program that implements a system where if the number is higher than 128, the alphabet would reset and you would go back to an ascii value of 0. </p> <p>What I tried to do was something like this: </p> <pre><code>if addition&gt;128: addition=addition-128 </code></pre> <p>When I ran the program after doing this, it didn't work and just returned a space instead of the right character. Any ideas? </p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload