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...
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
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...
On Python Write a function that accepts a relative file path as parameter and returns number...
On Python Write a function that accepts a relative file path as parameter and returns number of non-empty lines in the file. Test your function with the provided sample file studentdata.txt.
Write a function called fnReadInParameters() that takes as parameter(s): • a string indicating the name of...
Write a function called fnReadInParameters() that takes as parameter(s): • a string indicating the name of the parameter le This function should return a dictionary that stores all of the parameter le data of the SIR model in a format that we will describe further below.
Use Python Write a function that takes a mobile phone number as a string and returns...
Use Python Write a function that takes a mobile phone number as a string and returns a Boolean value to indicate if it is a valid number or not according to the following rules of a provider: * all numbers must be 9 or 10 digits in length; * all numbers must contain at least 4 different digits; * the sum of all the digits must be equal to the last two digits of the number. For example '045502226' is...
For python Write a new method for the Fraction class called mixed() which returns a string...
For python Write a new method for the Fraction class called mixed() which returns a string that writes out the Fraction in mixed number form. Here are some examples: f1 = Fraction(1, 2) print(f1.mixed()) # should print "1/2" f2 = Fraction(3, 2) print(f2.mixed()) # should return "1 and 1/2" f3 = Fraction(5, 1) print(f3.mixed()) # should return "5" def gcd(m, n): while m % n != 0: oldm = m oldn = n m = oldn n = oldm %...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT