2

I'm a beginner in python and taking a course on it. I've been tasked with making a caesar cipher where I can input the alphabet used. For this I can't use ord() or list() or any imported functions, only basic python. I've got it to work for one letter but I can't seem to figure out how to make it work for more than one letter. Any help would be greatly appreciated!

def cypher(target, alphabet, shift):
    
    for index in range( len(alphabet)):
        if alphabet[index] == target:
           x = index + shift
           y =  x % len(alphabet)
                  
    return (alphabet[y])
Asocia
  • 5,935
  • 2
  • 21
  • 46
808Blues
  • 87
  • 1
  • 4
  • 1
    How are list() and ord() more advanced Python over len() and range()? – Thomas Weller Feb 10 '21 at 18:03
  • We can't do your homework for you, but perhaps this will help: Give an example of how your function will be called. If you can then describe in words how you would solve the problem (basically, describe your algorithm), then it should be easy to convert that into code. – coreyp_1 Feb 10 '21 at 18:04
  • Would you mind providing a [mre], i.e. a full example which also calls the cypher() function? Please specify the actual and expected output. I can't get the meaning of `target` and `shift`. – Thomas Weller Feb 10 '21 at 18:04
  • So far ive been using the target as the plain text that i want to shift, so if i were to call it it would be print(cypher('e','abcdefg,5)) where it would shift the e 5 spots to the right – 808Blues Feb 10 '21 at 18:48

2 Answers2

0

I see this is your first question. Thanks for asking.

I think what you want your code to encrypt a full length script using the function you built above. So, what your function does is it takes a letter as target and shifts it.

This can easily be applied to a string by iterating over its elements.

I have provided the correct implementation for your query with some tweaks here :

alphabet = "abcdefghijklmnopqrstuvwxyz"

def cypher(target, shift):
    for index in range(len(alphabet)):
        if alphabet[index] == target:
            x = index + shift
            y =  x % len(alphabet)
            return (alphabet[y])


string = "i am joe biden"
shift = 3 # Suppose we want to shift it for 3
encrypted_string = ''
for x in string:
    if x == ' ':
        encrypted_string += ' '
    else:
        encrypted_string += cypher(x, shift)

print(encrypted_string)

shift = -shift # Reverse the cypher
decrypted_string = ''
for x in encrypted_string:
    if x == ' ':
        decrypted_string += ' '
    else:
        decrypted_string += cypher(x, shift)

print(decrypted_string)
Suraj Upadhyay
  • 475
  • 3
  • 9
0

There are many ways to accomplish such a thing, but you probably want a form of

  • get the input values
  • construct your mapping and store it (perhaps in a dictionary at first for some sort of per-value comparison or directly as a str.maketrans table)
  • take a look to see that it's what you want and expect
  • perform the cipher operation
  • give back the results (showing the original string on the line above or below the result can help spot issues!)

Here's a complete example for a very boring Caesar Cipher

source_string = input("source string: ").upper()
cut_position  = int(input("rotations: "))

# available as string.ascii_uppercase
source_values = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"

# create a mapping
mapping = source_values[cut_position:] + source_values[:cut_position]

# display mapping
print("using mapping: {}".format(mapping))

# build a translation table
table = str.maketrans(source_values, mapping)

# use your translation table to rebuild the string
resulting_string = source_string.translate(table)

# display output
print(resulting_string)

Providing a mapping and lowercasing text is left as an exercise

ti7
  • 16,375
  • 6
  • 40
  • 68