#Program with Output

x = ("Hello, I am x")
print(x)
Hello, I am x
  • This progame defines the variable x and then prints x
#Program with Input and Output
x = int(input("What's your favorite number? "))
print(x, "is a very cool number!")
What's your favorite number? 16
16 is a very cool number!
  • This code takes the users input, defines it as a interger, and prints a message
  • You type in your favorite integer and then it prints a message saying your favorite number is a very cool number
#Program with a List
fruit_list = ["Apple", "Banana"]

print(f'Current Fruits List {fruit_list}')

new_fruit = input("Please enter a fruit name:\n")

fruit_list.append(new_fruit)

print(f'Updated Fruits List {fruit_list}')

Current Fruits List ['Apple', 'Banana']
Please enter a fruit name:
Strawberry
Updated Fruits List ['Apple', 'Banana', 'Strawberry']
  • This code is making a list called ‘fruit_list’ and adding another fruit to it
  • It takes the users input and then adds that new fruit the user provided back into the list
  • It prints the original list and then the new updated list
#Program with a Dictionary
spbc = {
    "Lionel Messi" : "FC Barcelona",
    "Christiano Ronaldo" : "Real Madrid",
    "Neymar Jr" : "PSG"
}
print(spbc)
print(spbc["Lionel Messi"])
print(len(spbc))
{'Lionel Messi': 'FC Barcelona', 'Christiano Ronaldo': 'Real Madrid', 'Neymar Jr': 'PSG'}
FC Barcelona
3
  • This code defines a dictionary, spbc(Soccer players best club), and then proceeds to print it, find and print what Lionel Messis’s club was, and then print the length of the dictionary
#Program with Iteration
x = int(input("Pick a number lower of higher than 10: "))
if x > 10 or x < 10:
    print('Well done, you followed the simple rules')
elif x == 10:
    print("You couldn't follow simple directions, bad job")
Pick a number lower of higher than 10: 10
You couldn't follow simple directions, bad job
  • This code here grab an input from the user and determines whether or not the users input follows the criteria of the if statement
  • The user is told to choose a number lower or higher than 10, and if they do, they are rewarded with a message that tells them good job
  • If the user enters 10, then they are frowned upon and are told they couldn’t follow simple directions
#Program with a Function to perform mathematical and/or a statistical calculations, contains a condition
inp = input("Would you like to add, subtract, multiply, or divide two numbers today?")

#If the user decides they want to add two number, this if statement will run
if inp == "Add" or inp == "add":
    x = int(input("Pick the first number you would like to add: "))
    y = int(input("Pick the second number you would like to add: "))
    z = x + y
    print("Your answer is", z)
    
#If the user decides they want to subtract two number, this if statement will run
elif inp == "Subtract" or inp == "subtract":
    a = int(input("Pick the first number you would like to subtract: "))
    b = int(input("Pick the second number you would like to subtract: "))
    c = a - b
    print("Your answer is", c)
elif inp == "Multiply" or inp == "multiply":
    a = int(input("Pick the first number you would like to multiply: "))
    b = int(input("Pick the second number you would like to multiply: "))
    c = a * b
    print("Your answer will be", c)
elif inp == "Divide" or inp == "divide":
    a = int(input("Pick the first number you would like to divide: "))
    b = int(input("Pick the second number you would like to divide: "))
    c = a/b
    print("Your answer is", c)
else:
    print("Please follow the simple directions given")

Please follow the simple directions given
  • This code is a calculator and can add, subtract, multiply, and subtract
  • It grabs input from the user that asks what mathematical procedure they would like to execute and then it runs the if statement that matches the users request.
import random


class RockPaperScissors:
    """
    Class to handle an instance of a Rock-Paper-Scissors game
    with unlimited rounds.
    """

    def __init__(self):
        """
        Initialize the variables for the class
        """
        self.wins = 0
        self.losses = 0
        self.ties = 0
        self.options = {'rock': 0, 'paper': 1, 'scissors': 2}

    def random_choice(self):
        """
        Chooses a choice randomly from the keys in self.options.
        :returns: String containing the choice of the computer.
        """

        return random.choice(list(self.options.keys()))

    def check_win(self, player, opponent):
        """
        Check if the player wins or loses.
        :param player: Numeric representation of player choice from self.options
        :param opponent: Numeric representation of computer choice from self.options
        :return: Nothing, but will print whether win or lose.
        """

        result = (player - opponent) % 3
        if result == 0:
            self.ties += 1
            print("The game is a tie! You are a most worthy opponent!")
        elif result == 1:
            self.wins += 1
            print("You win! My honor demands a rematch!")
        elif result == 2:
            self.losses += 1
            print("Haha, I am victorious! Dare you challenge me again?")

    def print_score(self):
        """
        Prints a string reflecting the current player score.
        :return: Nothing, just prints current score.
        """
        print(f"You have {self.wins} wins, {self.losses} losses, and "
              f"{self.ties} ties.")

    def run_game(self):
        """
        Plays a round of Rock-Paper-Scissors with the computer.
        :return: Nothing
        """
        while True:
            userchoice = input("Choices are 'rock', 'paper', or 'scissors'.\n"
                               "Which do you choose? ").lower()
            if userchoice not in self.options.keys():
                print("Invalid input, try again!")
            else:
                break
        opponent_choice = self.random_choice()
        print(f"You've picked {userchoice}, and I picked {opponent_choice}.")
        self.check_win(self.options[userchoice], self.options[opponent_choice])


if __name__ == "__main__":
    game = RockPaperScissors()
    while True:
        game.run_game()
        game.print_score()

        while True:

            continue_prompt = input('\nDo you wish to play again? (y/n): ').lower()
            if continue_prompt == 'n':
                print("You are weak!")
                exit()
            elif continue_prompt == 'y':
                break
            else:
                print("Invalid input!\n")
                continue
You've picked rock, and I picked rock.
The game is a tie! You are a most worthy opponent!
You have 0 wins, 0 losses, and 1 ties.
You are weak!
You are weak!
Invalid input!

Invalid input!

Invalid input!

Invalid input!

Invalid input!

Invalid input!

Invalid input!

Invalid input!

Invalid input!

Invalid input!

Invalid input!

Invalid input!

Invalid input!

Invalid input!

Invalid input!

You are weak!
Invalid input!

Invalid input!

Invalid input!

Invalid input!

Invalid input!

Invalid input!
  • The code defines a text-based Rock-Paper-Scissors game where the player competes against a computer opponent.
  • It uses a class called RockPaperScissors to manage the game, keep track of wins, losses, and ties, and handle player and computer choices.
  • The game allows players to play multiple rounds and keeps a running score, providing an option to continue or exit after each round.
  • The computer’s choice is determined randomly using the random module, and the game logic is implemented to determine the winner of each round.