Hello! First time posting here so bare with me with my formating and other stuff.

I've got a task were I'm going to create a caesar cipher. You should be able to write a sentance to the program and the shifting it should use. Then the letters should be shifted with the number which was typed in. The program should not shift spaces and periods and when the ascii code for a letter exceeds 122 it should start from 97 and add then shift from there. Also when the ascii code exceeds 90 but not over 96 it should start from 65 and add the shift from there. In this program i'm not allowed to use "def" , "continue" or "return", so just very basic coding. Here's the code i've got:

str = input("Write a sentence: ")
b = input("Write the shift: ")

i = 0

for i in range(len(str)):
    if ord(str[i]) == 46:
        print(end=".")
    elif ord(str[i]) == 32:
        print(end=" ")
    elif ord(str[i]) >= 90:
        print(end=chr(ord(str[i] ) -26 + int(b)))
    elif ord(str[i]) >= 123:
        print(end=chr(ord(str[i] ) -26 + int(b)))
    else:
        print(chr(ord(str[i]) + int(b)), end="")

The problem is that it now has started to make lower case letter upper case which is not good. Otherwise it's not giving any errors atm.

Recommended Answers

All 6 Replies

I would suggest that first you print "i" as it is probably not what you think it is. See Click Here "for loop and a string"

Second, you want to test for limits after the shift. You did not post any code to shift by "b".

To eliminate spaces, periods, etc. use (and this assumes that you don't want to shift numbers either)

if "a" <= character.lower() <= "z":
## shift by "b"

Where should i put in your if statement? After my first if statement or after one of the elif statements?

Neither, as your logic is not at all correct. If you aren't going to take my advice, i.e. print "i", then there is not much point in posting anything further.

Ok, i'm sorry. I guess i'm not that good at logical thinking. If I print out "i" I get the string printed out that was typed to the program. But I don't understand how that would help me. Isn't the string going to be looped through anyways?, and if so there's no need to print it out. And if I would make the string to numbers and then print out "i" it would give me the length of it but still, how does that help me?

When you say shift by "b", do you mean shift by the ascii code for the charachter or the letter? I don't really understand what you mean by that.

Your answer was very short so I didn't really understand it properly. I need a more thorough explaination. If you can't provide that, just don't even bother helping me. It's fine.

Printing i should not give you a string ...

mystr = "str is a function in Python and should not be used as a variable name"

for i in range(len(mystr)):
    print(i)

Also explore the string functions isalpha(), islower() and isupper().

I think I did something wierd before. It now prints out, if I type "Hello", 0,1,2,3 and 4 bellow each other. Is that correct so far?

I would just like to ask what you recommend when skipping certain characters in the ascii code. For example, I don't want the letters in the string to be shifted into characters from 91 to 96. If the ascii code, after the shifting has been added, exceeds 91 but is lower than 97 it should add the shifting from 65 instead and this should always be done.

Also, this Caesar cipher is a task I got where I can't use the string functions you mentioned. What I should use is mostly loops and other really basic functions. The functions: center(), .upper() and .lower() and so on are ok to use.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.