In: Computer Science
Python program. from random import . Write a function called cleanLowerWord 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 class 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 # Converst 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. 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: rock beats scissors scissors beats paper paper beats rock 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"
#import module
import random
#function to convert uppercase letter to lowercase and return
it
def toLower(ch):
return chr(ord(ch)-ord('A')+ord('a'))
#function to check if the character is a lowercase or not
def isLower(ch):
if ch>='a' and ch<='z':
return True
else:
return False
#function to check if the character is a uppercase or not
def isUpper(ch):
if ch>='A' and ch<='Z':
return True
else:
return False
#function to convert all uppecase to lowercase in a string and
remove all other characters and return it
def cleanLowerWord(string):
#answer string
ans=""
#for all characters in string
for ch in string:
#if current character is lowercase
if isLower(ch):
ans+=ch
#if current character is uppercase
elif isUpper(ch):
ans+=toLower(ch)
#otherwise do not add any other character to answer and
continue
#return answer
return ans
#function to play rock Paper Scissors game
def rockPaperScissors():
#prompt user to enter his/her choice take input
userChoice=input("Enter your choice: ")
#cleanword if it contains any other character or uppercase
userChoice=cleanLowerWord(userChoice)
#find my choice by using random choice
myChoice = random.choice(("rock", "paper", "scissors"))
#print my choice
print("my choice is "+myChoice)
#if both player's choice are equal
if userChoice==myChoice:
print("it's a draw")
#otherwise follow the game rules and decide the winner
elif userChoice=="rock" and myChoice=="scissors":
print("user wins")
elif userChoice=="rock" and myChoice=="paper":
print("user loses")
elif userChoice=="scissors" and myChoice=="rock":
print("user loses")
elif userChoice=="scissors" and myChoice=="paper":
print("user wins")
elif userChoice=="paper" and myChoice=="rock":
print("user wins")
elif userChoice=="paper" and myChoice=="scissors":
print("user loses")
#testing cleanLowerWord()
print(cleanLowerWord("Hello, User 15!"))
#testing rockPaperScissors()
while True:
rockPaperScissors()
flag=input("Do you want to play again? ")
if flag=="no":
break
hellouser Enter your choice: ROCK! my choice is scissors user wins Do you want to play again? yes Enter your choice: SCIssors! my choice is paper user wins Do you want to play again? no
CODE and INPUT/OUTPUT
So if you still have any doubt regarding this solution please feel free to ask it in the comment section below and if it is helpful then please upvote this solution, THANK YOU.