#🔒 When I'm trying to run it, it stops running

26 messages · Page 1 of 1 (latest)

wet seal
#

import string

""" The function takes in a string, makes all characters lowercase, and removes all punctuation including space.
This function should be called by encrypt before encryption. """
def preprocess(plaintext):
translator=str.maketrans('','',string.punctuation)
plaintext=plaintext.translate(translator).lower()
return plaintext.lower().replace(" ", "")

""" Given the character 'ch' and an integer k,
representing the number of positions to shift this character,
return the result of shifting 'ch' by 'k' positions"""
def shift(ch,k):
return chr(97+( ord(ch)-97+k)%26) # equivalent to chr(ord('a')+( ord(ch)-ord('a')+k)%26)

""" Encrypt with Cesar cipher """
def encrypt(plaintext,k):

ciphertext = ''
plaintext = preprocess(plaintext)
for letter in plaintext:
ciphertext += shift (letter, k)
return ciphertext

""" Decrypt with Cesar cipher """
def decrypt(ciphertext,k):

plaintext = ""
for letter in ciphertext:
    plaintext += shift(letter, -k)
return plaintext
zinc hamletBOT
#

@wet seal

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

#

Hey @wet seal!

It looks like you're trying to paste code into this channel.

Discord has support for Markdown, which allows you to post code with full syntax highlighting. Please use these whenever you paste code, as this helps improve the legibility and makes it easier for us to help you.

To do this, use the following method:
```py
print('Hello, world!')
```

This will result in the following:

print('Hello, world!')```
You can **edit your original message** to correct your code block.
wet seal
#

`import string

""" The function takes in a string, makes all characters lowercase, and removes all punctuation including space.
This function should be called by encrypt before encryption. """
def preprocess(plaintext):
translator=str.maketrans('','',string.punctuation)
plaintext=plaintext.translate(translator).lower()
return plaintext.lower().replace(" ", "")

""" Given the character 'ch' and an integer k,
representing the number of positions to shift this character,
return the result of shifting 'ch' by 'k' positions"""
def shift(ch,k):
return chr(97+( ord(ch)-97+k)%26) # equivalent to chr(ord('a')+( ord(ch)-ord('a')+k)%26)

""" Encrypt with Cesar cipher """
def encrypt(plaintext,k):

ciphertext = ''
plaintext = preprocess(plaintext)
for letter in plaintext:
ciphertext += shift (letter, k)
return ciphertext

""" Decrypt with Cesar cipher """
def decrypt(ciphertext,k):

plaintext = ""
for letter in ciphertext:
    plaintext += shift(letter, -k)
return plaintext

`

manic minnow
#

which function is not running?

#

@wet seal

wet seal
#

the thing just turns off automatically

#

when i press run @manic minnow

manic minnow
#

its because u are not calling any function

wet seal
#

oh so what do i do then?

manic minnow
#

to call a function u need to write the name and its arguments

#

eg: encrypt(example, whatever value k is)

#

!function

zinc hamletBOT
#
Calling vs. referencing functions

When assigning a new name to a function, storing it in a container, or passing it as an argument, a common mistake made is to call the function. Instead of getting the actual function, you'll get its return value.

In Python you can treat function names just like any other variable. Assume there was a function called now that returns the current time. If you did x = now(), the current time would be assigned to x, but if you did x = now, the function now itself would be assigned to x. x and now would both equally reference the function.

Examples

# assigning new name

def foo():
    return 'bar'

def spam():
    return 'eggs'

baz = foo
baz() # returns 'bar'

ham = spam
ham() # returns 'eggs'
# storing in container

import math
functions = [math.sqrt, math.factorial, math.log]
functions[0](25) # returns 5.0
# the above equivalent to math.sqrt(25)
# passing as argument

class C:
    builtin_open = staticmethod(open)

# open function is passed
# to the staticmethod class
wet seal
#

oh so i put this in the end?

manic minnow
#

that depends on what function u want to use

#

because ur code has encrypt, decrypt , shift function

manic minnow
wet seal
#

this is what it wants me to do @manic minnow

manic minnow
#

As u can see here the instructions clearly mentions how to call ur functions

#

It used the encrypt function to encrypt the text and then used the output in decrypt function

wet seal
#

ohhh okay

#

thanks

zinc hamletBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.