Introduction to Interactive Programming in Python - Week 2
The second week of the course introduces the basic constructs of Python programming, including functions, logic and comparisons, and conditionals. We also build our first program this week: Rock, Paper, Scissors, Lizard, Spock!
In this variant of Rock, Paper, Scissors, popularized by the The Big Bang Theory, each weapon defeats the weapons it points to, and is defeated by weapons pointing to it.
Functions: A piece of code that you define and then execute later by calling it.
Comparisons: The six comparison operators allow you to compare two values and generate a Boolean value. In programming, a Boolean expression always evaluates to either True or False (or, equivalently, Yes/No, On/Off, etc.). The comparison operators are as follows:
Greater Than: >
Less Than: <
Greater Than or Equal To: >=
Less Than or Equal To: <=>
Equal To: ==
Not Equal To: !=
Conditionals: Oftentimes you'll want to change a program's behavior based on a given value or user input. To control the flow of your program, you can check a value in the program and then execute different lines of code based on that value. In Python, you can control the flow of programs using the following conditionals: if/elif/else.
Mini Project 2
This week's mini project is to create the game Rock, Paper, Scissors, Lizard, Spock that we can play against the computer. To do so, we need to write three functions: name_to_number, number_to_name, and rpsls.
name_to_number: Usually, but not always, functions take input parameters, also called arguments, and perform some type of operation or comparison to the parameters. This function takes as an argument "name", which is a string representation of either "rock", "Spock", "paper", "lizard", "scissors", or some other string. Using the if/elif/else conditionals, we want to return a number between 0 and 4 corresponding to each of the possible weapons. We also want to print a message to the user if the "name" argument isn't one of the five possible weapons. A function doesn't need to explicitly return anything. If the user doesn't return anything, the function returns None by default. Generally, ending a function with a return statement is useful if you want to use the value of the return statement in another function, like we do in the function rpsls.
number_to_name: This function is simply the opposite of name_to_number shown above.
rpsls: This function contains the main logic of this program. It takes as an argument the string representation of the player's choice. It computes the computer's choice by generating a random integer between 0 and 4. Note that this randomly generated integer can be converted to a string using the helper function number_to_name. Then the function computes the difference between the number corresponding to the player's choice and the number corresponding to the computer's choice. This difference is calculated modulo five, which means it's the remainder of the difference divided by 5. Finally, this function uses the if/elif/else conditionals to determine the winner and prints the winner.