Day 54 - Modules With Functions that Take Parameters and Return Values

We will begin with the rock, paper, scissors project, moving those custom funcctions to a module, and illustrating how import statements work using these functions.

Starter Code

import random

print("~"*60)
print("Rock, Paper Scissors!".center(60))
print("~"*60)
print("\n")

def compGuess():
    # Computer's Guess
    liChoices = ["r","p","s"]
    strCC = random.choice(liChoices)

    # User-friendly version
    if (strCC == "r"):
        strCCp = "rock"
    elif (strCC == "p"):
        strCCp = "paper"
    else:
        strCCp = "scissors"

    return (strCC, strCCp)

def userGuess():
    # User's Choice
    while True:
        strUC = input("Choose (r)ock, (p)aper, or (s)cissors: ").lower()

        if (strUC == "r" or strUC == "rock"):
            strUC = "r"
            strUCp = "rock"
            break
        elif (strUC == "p" or strUC == "paper"):
            strUC = "p"
            strUCp = "paper"
            break
        elif (strUC == "s" or strUC == "scissors"):
            strUC = "s"
            strUCp = "scissors"
            break
        else:
            print("\nThat isn't a valid choice.\nEnter r, p or s.\n")
    return (strUC, strUCp)

def whoWon(tUserGuess, tComputerGuess):

    strUC, strUCp = tUserGuess
    strCC, strCCp = tComputerGuess

    print(f"You entered {strUCp}")
    print(f"The computer chose {strCCp}")

    if (strCC == strUC):
        print("It's a TIE!")
        strReturn = "t"
    else:
        if strUC == "r":
            if strCC == "p":
                print("The computer wins!")
                strReturn = "c"
            else:
                # strCC == "s"
                print("You won!")
                strReturn = "u"

        elif strUC == "p":
            if strCC == "s":
                print("The computer wins!")
                strReturn = "c"
            else:
                # strCC == "r"
                print("You won!")
                strReturn = "u"

        else:
            # strUC == "s"
            if strCC == "r":
                print("The computer wins!")
                strReturn = "c"
            else:
                # strCC == "p"
                print("You won!")
                strReturn = "u"

    return strReturn

intCScore = 0
intUScore = 0

while True:
    while True:
        strWhoWon = whoWon(userGuess(), compGuess())

        if (strWhoWon == "c"):
            intCScore += 1
        elif (strWhoWon == "u"):
            intUScore += 1

        print(f"\nYou have won {intUScore}.")
        print(f"The computer has won {intCScore}.\n")

        if (intCScore == 2):
            print("The computer has won the match!")
            break
        elif (intUScore == 2):
            print("You have won the match!")
            break

    strAgain = input("Play again? (y/n): ")

    if (strAgain == "n"):
        break
    else:
        intCScore = 0
        intUScore = 0
        print("\nStarting a new game...\n")

print("\nThanks for playing my game.")

Finished Code

from rpsModules import *

print("~"*60)
print("Rock, Paper Scissors!".center(60))
print("~"*60)
print("\n")


intCScore = 0
intUScore = 0

while True:
    while True:
        strWhoWon = whoWon(userGuess(), compGuess())

        if (strWhoWon == "c"):
            intCScore += 1
        elif (strWhoWon == "u"):
            intUScore += 1

        print(f"\nYou have won {intUScore}.")
        print(f"The computer has won {intCScore}.\n")

        if (intCScore == 2):
            print("The computer has won the match!")
            break
        elif (intUScore == 2):
            print("You have won the match!")
            break

    strAgain = input("Play again? (y/n): ")

    if (strAgain == "n"):
        break
    else:
        intCScore = 0
        intUScore = 0
        print("\nStarting a new game...\n")

print("\nThanks for playing my game.")