In: Computer Science
Python program. 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. 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:
As per our guidelines, we can answer only the first question. please ask the 2nd qtn seperately, if required....
for having any kind of doubt, please leave a comment in the comment box.... thank you...
Please check out the code with the output. In this python program, one function and the main was missing... After writing those, the final answer is
code....
def isLower(ch): #returns true of the character is in lower case else returns falls
return 'a' <= ch and ch <= 'z'
def isUpper(ch): #returns true of the character is in upper case else returns falls
return 'A' <= ch and ch <= 'Z'
def toLower(ch): #converts the character to lower case
if isUpper(ch): #checking if the is in upper case
return chr(ord(ch)-ord('A')+ord('a')) #if so,then converting to lower case
else:
return ch #else do nothing i.e. character is already in lower case
def toLowerStr(strn): #converts the whole string to lower case
lower = ""
for ch in strn: #for every character in the string strn
lower += toLower(ch)
return lower
def cleanLowerWord(strn): #ADDED
st="" #temporary string variable
for ch in strn:
if isUpper(ch) or isLower(ch): #i.e. if the character is Alphabet then add it to new string else ignore/delete
st+=ch
return toLowerStr(st) #calling toLower() and returning it
ch=input("Enter String : ")
print(cleanLowerWord(ch))
OUTPUT
import random
def isLower(ch): #returns true of the character is in lower case else returns falls
return 'a' <= ch and ch <= 'z'
def isUpper(ch): #returns true of the character is in upper case else returns falls
return 'A' <= ch and ch <= 'Z'
def toLower(ch): #converts the character to lower case
if isUpper(ch): #checking if the is in upper case
return chr(ord(ch)-ord('A')+ord('a')) #if so,then converting to lower case
else:
return ch #else do nothing i.e. character is already in lower case
def toLowerStr(strn): #converts the whole string to lower case
lower = ""
for ch in strn: #for every character in the string strn
lower += toLower(ch)
return lower
def cleanLowerWord(strn): #ADDED
st="" #temporary string variable
for ch in strn:
if isUpper(ch) or isLower(ch): #i.e. if the character is Alphabet then add it to new string else ignore/delete
st+=ch
return toLowerStr(st) #calling toLower() and returning it
while(1): #game loop
ch=input("Enter your choice : ") #rock/paper/scissors
ch=cleanLowerWord(ch) #calling function to lower case
myChoice = random.choice(("rock","paper","scissors")) #it will generate random string b/w these three
if ch==myChoice: #if both are same
print("Draw")
elif (ch=="rock" and myChoice=="scissors") or (ch=="scissors" and myChoice=="paper") or (ch=="paper" and myChoice=="rock"):
print("User wins")
else:
print("Computer wins")
c=input("Do you want to continue? yes/ no ") #asking user
c=cleanLowerWord(c)
if c=="no":
break
output