Question

In: Computer Science

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

Solutions

Expert Solution

  • Below is the detailed implementation of the above problem in PYTHON with code and output shown.
  • For better understanding please read the comments mentioned in the code.
  • In the code below every function of exercise 1 and exercise 2 are implemented and after implementation of functions , there is a piece of code for testing our functions, so it is working properly.
  • CODE:

#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

  • INPUT/OUTPUT:
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
  • Below are the screenshot attached for the code and input/output for better clarity and understanding.

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.


Related Solutions

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...
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...
Please convert this code written in Python to Java: import string import random #function to add...
Please convert this code written in Python to Java: import string import random #function to add letters def add_letters(number,phrase):    #variable to store encoded word    encode = ""       #for each letter in phrase    for s in phrase:        #adding each letter to encode        encode = encode + s        for i in range(number):            #adding specified number of random letters adding to encode            encode = encode +...
Using Python #Write a function called after_second that accepts two #arguments: a target string to search,...
Using Python #Write a function called after_second that accepts two #arguments: a target string to search, and string to search #for. The function should return everything in the first #string *after* the *second* occurrence of the search term. #You can assume there will always be at least two #occurrences of the search term in the first string. # #For example: # after_second("11223344554321", "3") -> 44554321 # #The search term "3" appears at indices 4 and 5. So, this #returns everything...
Creates a function called pick. The function receives a "string" representing one year (the variable with...
Creates a function called pick. The function receives a "string" representing one year (the variable with this "string" will be called uve) and a list containing "strings" representing bank accounts (call this bera list). <br> • Each account is represented by 8 characters. The format of each account number is "**-**-**", where asterisks are replaced by numeric characters. o For example, "59-04-23". • The two central characters of the "string" for each account represent the year the account was created....
C++ PLEASE Write a new program called Lab15A Write a void function named fillArray that receives...
C++ PLEASE Write a new program called Lab15A Write a void function named fillArray that receives an array of 10 integers as a parameter and fills the array by reading the values from a text file (Lab15A.txt). It should also print the values of the array all on the same line, separated by spaces. Write an int function named findLast that receives an array of 10 integers as a parameter and returns the last negative number in the array. Write...
Write a Python program: The function is named validity(). It receives 2 floating point parameters, min...
Write a Python program: The function is named validity(). It receives 2 floating point parameters, min and max, from the program that invoked it. The function asks the user to enter a float number. Using a while loop it checks whether the number is valid (between min and max, inclusive). If not valid, the while loop uses this statement to prompt for a new number: num = float (input (" Enter a number that is at least :"+ str(min) +...
In Python Write a function to read a Sudoku board from an input string. The input...
In Python Write a function to read a Sudoku board from an input string. The input string must be exactly 81 characters long (plus the terminating null that marks the end of the string) and contains digits and dots (the `.` character represents an unmarked position). The input contains all 9 rows packed together. For example, a Sudoku board that looks like this: ``` ..7 ... ... 6.4 ... ..3 ... .54 ..2 ... .4. ... 9.. ... ..5 385...
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...
Sovle with python 3.8 please. 1, Write a function called same_without_ends that has two string parameters....
Sovle with python 3.8 please. 1, Write a function called same_without_ends that has two string parameters. It should return True if those strings are equal WITHOUT considering the characters on the ends (the beginning character and the last character). It should return False otherwise. For example, "last" and "bask" would be considered equal without considering the characters on the ends. Don't worry about the case where the strings have fewer than three characters. Your function MUST be called same_without_ends. You...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT