Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to print multiple return values from a function?

import base64 as b64
from cryptography.fernet import Fernet
def encrypt(data):
    key = Fernet.generate_key()
    keyb64 = b64.urlsafe_b64encode(key).decode('utf-8')
    cipher = Fernet(key)
    encd_data = cipher.encrypt(data.encode())
    encd_datab64 = b64.urlsafe_b64encode(encd_data).decode('utf-8')
    return encd_datab64, keyb64
print('Your data:' + encrypt(input('what to encrypt\n')))

I want it to print out:

Your code: (encd_datab64), your key: (keyb64)

In the code above, it creates an error that says you cannot separate a tuple, but I don’t know what a tuple is. If I delete the 'Your data:' + part of the code, it just gives encrypted data and text.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

I understand your requirement. It seems you want to print the encrypted data and the key in a specific format. You can achieve this by modifying the print statement to include the formatted strings. Here’s the updated code:

import base64 as b64
from cryptography.fernet import Fernet

def encrypt(data):
    key = Fernet.generate_key()
    keyb64 = b64.urlsafe_b64encode(key).decode('utf-8')
    cipher = Fernet(key)
    encd_data = cipher.encrypt(data.encode())
    encd_datab64 = b64.urlsafe_b64encode(encd_data).decode('utf-8')
    return encd_datab64, keyb64

encrypted_data, key = encrypt(input('Enter data to encrypt: '))
print(f'Your code: {encrypted_data}, your key: {key}')

In this code, I’ve used f-strings (formatted strings) to include the values of encrypted_data and key in the print statement. This way, it prints the desired output in the format you specified.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading