A201 Introduction to Programming 1

A201 Homework 7

Due Date: Tuesday, October 10, 2023.

Goal: to use functions in Python.

Make sure to complete Lab 7 before doing the homework.

Create a Python script file called hw7.py. Add your name at the top as a comment, along with the class name and date. Both exercises should be in this file, with a comment before each of them to mark it. Then import the module random:

from random import *

Ex. 1.

Write a function called cleanLowerWord with the header

def cleanLowerWord(word):

that receives a string as a parameter and returns a new string that is a copy of the parameter where all the lowercase letters are kept as such, uppercase letters are converted to lowercase, and everything else is deleted. For example, the function call

cleanLowerWord("Hello, User 15!")

should return the string "hellouser".

For this, you can start by copying the following functions discussed in the lecture into your file:

# Checks if ch is a lowercase letter.
def isLower(ch):
    return 'a' <= ch and ch <= 'z'

# Checks if ch is an uppercase letter.
def isUpper(ch):
    return 'A' <= ch and ch <= 'Z'

# Converts ch to a lowercase letter if it's an uppercase letter,
# and returns it unchanged if not.
def toLower(ch):
    if isUpper(ch):
        return chr(ord(ch)-ord('A')+ord('a'))
    else:
        return ch

# Converts all uppercase letters in strn into lowercase and 
# leaves everything else unchanged.
def toLowerStr(strn):
    lower = ""
    for ch in strn:
        lower += toLower(ch)
    return lower

Then you can write the new function in a similar way to the function toLowerStr but where you test if the character is a lowercase letter, uppercase, and other. Lowercase letters are just added, uppercase letters are converted to lowercase and then added, and everything else is not added to the new string.

Ex. 2. Rock - Paper - Scissors

Write a program that plays the rock-paper-scissors game with the user.

a. For this, write a function that plays one R-P-S game with the user, called rockPaperScissors, taking no parameters, with the header

def rockPaperScissors():

This function should ask the user to enter their choice. Input this choice into a variable userChoice, then call the function cleanLowerWord defined at exercise 1 and assign the result back to the same variable.

After that, declare a variable n and assign to it randint(1,3). Then assign either "rock", "paper", "scissors" to a variable myChoice by doing

myChoice = choice(("rock", "paper", "scissors"))

Note that choice is another function from the module random.

Then you need a conditional that compares the user choice with my choice. If the two of them are equal, then print that it's a draw. Otherwise (use an elif) if the user choice is "rock" and my choice is "scissors", print that the user wins. The rules are:

b. Write a piece of code at the end to test the function. Make this game play in a loop where after each run of the game, you ask the user if they want to play again, input the answer as "yes" or "no", and keep going while the answer is not "no"

Homework Submission

Upload the files lab7.py and hw7.py in Canvas, Assignments, Homework 7.