Question

In: Computer Science

Python program. Write a function called cleanLowerWord that receives a string as a parameter and returns...

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:

  • 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"

Solutions

Expert Solution

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


Related Solutions

Python program.  from random import . Write a function called cleanLowerWord that receives a string as a...
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...
In Python Create a function called ????. The function receives a "string" that represents a year...
In Python Create a function called ????. The function receives a "string" that represents a year (the variable with this "String" will be called uve) and a list containing "strings" representing bank accounts (call this list ????). • Each account is represented by 8 characters. The format of each account number is "** - ** - **", where the asterisks are replaced by numeric characters. o For example, “59-04-23”. • The two central characters of the "string" of each account...
Write a Python function that takes a list of integers as a parameter and returns the...
Write a Python function that takes a list of integers as a parameter and returns the sum of the elements in the list. Thank you.
Write a Python function that takes a list of integers as a parameter and returns the...
Write a Python function that takes a list of integers as a parameter and returns the sum of the elements in the list. Thank you.
In PYTHON: Write a function that receives a sentence and returns the last word of that...
In PYTHON: Write a function that receives a sentence and returns the last word of that sentence. You may assume that there is exactly one space between every two words, and that there are no other spaces at the sentence. To make the problem simpler, you may assume that the sentence contains no hyphens, and you may return the word together with punctuation at its end.
Python: Write a function that receives a one dimensional array of integers and returns a Python...
Python: Write a function that receives a one dimensional array of integers and returns a Python tuple with two values - minimum and maximum values in the input array. You may assume that the input array will contain only integers and will have at least one element. You do not need to check for those conditions. Restrictions: No built-in Python data structures are allowed (lists, dictionaries etc). OK to use a Python tuple to store and return the result. Below...
Write a function named "characters" that takes a string as a parameter and returns the number...
Write a function named "characters" that takes a string as a parameter and returns the number of characters in the input string
Write a function called remove_punct() that accepts a string as a parameter, removes the punctuation (',',...
Write a function called remove_punct() that accepts a string as a parameter, removes the punctuation (',', '!', '.') characters from the string, and returns the number of punctuation characters removed. For example, if the string contains ['C', 'p', 't', 'S', ',', '1', '2', '1', '.', 'i', 's', 'f', 'u', 'n', '!', '\0'], then the function should remove the punctuation characters. The function must remove the characters by shifting all characters to the right of each punctuation character, left by one...
Python Programcomplete theprocessString(string)function. This function takesin a string as a parameter and prints the...
Python Program complete theprocessString(string)function. This function takes in a string as a parameter and prints the average number of characters per word in each sentence in the string. Print the average character count per word for each sentence with 1 decimal precision(see test cases below).-Assume a sentence always ends with a period (.)or when the string ends. -Assume there is always a blank space character(" ")between each word. -Do not count the blank spaces between words or the periods as...
Write a recursive function in python called make_palindrome that takes a sequence as a parameter and...
Write a recursive function in python called make_palindrome that takes a sequence as a parameter and returns a new sequence that is twice the length of the parameter sequence but that contains the contents of the original in palindrome form. For example, if the sequence "super" is passed into the function, the function will return "superrepus".
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT