“Python Project” Kod odpowiedzi

projekty Pythona

# How to Create Caesar Cipher Using Python
import string
import sys

# The word to be encoded shifts by 5 to the right, while the word to be decoded shifts by 5 to the left.
shift = 5

print(' Caesar Cipher '.center(40, '*'))
choices = ['e', 'd']
user_choice = input('Do you wish to [e]ncode, [d]ecode, or quit (any other letter)?: ').lower()

if user_choice not in choices:
    print('Program closed.')
    sys.exit()

word = input('Enter the word: ')


# ENCODING FUNCTION
def encode_words(words, shifts):
    """This encodes a word using Caesar cipher."""

    # Variable for storing the encoded word.
    encoded_word = ''

    for i in words:

        # Check for space and tab
        if ord(i) == 32 or ord(i) == 9:
            shifted_word = ord(i)

        # Check for punctuations
        elif i in string.punctuation:
            shifted_word = ord(i)

        # Check if the character is lowercase or uppercase
        elif i.islower():
            shifted_word = ord(i) + shifts

            # Lowercase spans from 97 to 122 (decimal) on the ASCII table
            # If the chars exceeds 122, we get the number it uses to exceed it and add to 96 (the character before a)
            if shifted_word > 122:
                shifted_word = (shifted_word - 122) + 96

        else:
            shifted_word = ord(i) + shifts

            # Uppercase spans from 65 to 90 (decimal) on the ASCII table
            # If the chars exceeds 90, we get the number it uses to exceed it and add to 64 (the character before A)
            if shifted_word > 90:
                shifted_word = (shifted_word - 90) + 64

        encoded_word = encoded_word + chr(shifted_word)

    print('Word:', word)
    print('Encoded word:', encoded_word)


# DECODING FUNCTION
def decode_words(words, shifts):
    """This decodes a word using Caesar cipher"""

    # Variable for storing the decoded word.
    decoded_word = ''

    for i in words:

        # Check for space and tab
        if ord(i) == 32 or ord(i) == 9:
            shifted_word = ord(i)

        # Check for punctuations
        elif i in string.punctuation:
            shifted_word = ord(i)

        # Check if the character is lowercase or uppercase
        elif i.islower():
            shifted_word = ord(i) - shifts

            # If the char is less 122, we get difference subtract from 123 (the character after z)
            if shifted_word < 97:
                shifted_word = (shifted_word - 97) + 123

        else:
            shifted_word = ord(i) - shifts

            # If the char is less 65, we get difference and subtract from 91 (the character after Z)
            if shifted_word < 65:
                shifted_word = (shifted_word - 65) + 91

        decoded_word = decoded_word + chr(shifted_word)

    print('Word:', word)
    print('Decoded word:', decoded_word)


def encode_decode(words, shifts, choice):
    """This checks if the users want to encode or decode, and calls the required function."""

    if choice == 'e':
        encode_words(words, shifts)
    elif choice == 'd':
        decode_words(words, shifts)


encode_decode(word, shift, user_choice)
OHIOLee

projekty Pythona

# How to code a simple rock, paper, scissors game on Python
import random

your_input = input("Enter a choice (r = rock, p = paper, s = scissors): ")
user = ""
if your_input.lower() == "r":
    user = "rock"
if your_input.lower() == "p":
    user = "paper"
if your_input.lower() == "s":
    user = "scissors"

possible = ["rock", "paper", "scissors"]
computer = random.choice(possible)

print(f"\nYou chose {user}, computer chose {computer}.\n")

if user == computer:
    print(f"Both players selected {user}. It's a tie!")
elif user == "rock":
    if computer == "scissors":
        print("Rock smashes scissors! You win!")
    else:
        print("Paper covers rock! You lose!")
elif user == "paper":
    if computer == "rock":
        print("Paper covers rock, You win!")
    else:
        print("Scissors cuts paper! You lose!")
elif user == "scissors":
    if computer == "paper":
        print("Scissors cuts paper! You win!")
    else:
        print("Rock smashes scissors! You lose!")
OHIOLee

projekty Pythona

# How to send emails with python using SMTP
# import SMTP library into your project using the command below

import smtplib

# Assuming you created two new Gmail and yahoo email accounts, create a connection to the Gmail email server by using the code below.

connection = smtplib.SMTP("smtp.gmail.com")

# Create variables to hold your email credentials

my_email = "[email protected]"
my_password = "passwaord"

# Secure your connection.
# It's important that you secure your connection to the Gmail mail servers.
# Securing your connection to the servers will prevent unauthorized access to your email in the event it's intercepted in transit.

# This is done by calling the tls function on your connection.

connection.starttls()

# The tls function is an inbuilt method that encrypts your email data sent via the connection established to email servers.
# The method is drawn from transport layer security protocol designed to provide communications security over a network.

#log in to your email account
# Run the code below to log in. The log-in method takes two parameters to facilitate a successful log-in: your email and password.
# Make sure there are no typos on the email and password held on the variables we created earlier.

connection.login(user=my_email, password=my_password)

# Sending the email.
# We are going to call the sendmail method on our connection. The method takes up 3 parameters :
# The sending address.
# The recipient's address. ( Avoid typos while typing it out to avoid errors.)
# The message with its subject and its body. Run the following command.

connection.sendmail(from_addr=my_email, to_addrs="[email protected]", msg=" Subject: My_subject \n\n My message body"

# Note: The subject and the body are separated using back slashes with an n to create new lines between them.
# Close the connction.
# Close the connection to the Gmail mail servers.

connection.close()
OHIOLee

Python Project

print("Write whatever you want")
Dangerous Dormouse

Odpowiedzi podobne do “Python Project”

Pytania podobne do “Python Project”

Więcej pokrewnych odpowiedzi na “Python Project” w Python

Przeglądaj popularne odpowiedzi na kod według języka

Przeglądaj inne języki kodu