Create a new program in Mu and save it as ps4.5.2.py and take the code below and fix it as indicated in the comments:
# Do not change the line of code below. It's at the top of # the file to ensure that it runs before any of your code. # You will be able to access french_dict from inside your # function. french_dict = {"me": "moi", "hello": "bonjour", "goodbye": "au revoir", "cat": "chat", "dog": "chien", "and": "et"} # Write a function called french2eng that takes in one string # parameter called sentence. french2eng should look at each # word in the sentence and translate it into French if it is # found in the dictionary, french_dict. If a word is not found # in the dictionary, do not translate it: use the original # word. Then, the function should return a string of the # translated sentence. # # You may assume that the sentence you're translating has no # punctuation. However, you should convert it to lower case # before translating. # # For example: # # french2eng("Hello it's me") -> "bonjour it's moi" # # Hint: Use .split() to get a list of strings representing # each word in the string, then use ' '.join to merge the # translated list back into one string. # # Hint 2: Remember, lists are mutable, so we can change # individual items in the list. However, to change an item # in a list, we must change it using its index. We can # write lines like my_words[1] = new_word. # # Hint 3: If you're stuck, try breaking it down into small # parts. How do you access an item from a list? How do you # look up a key in a dictionary? How do you change the # value of an item in a list? How do you check if a key is # in the dictionary? # Write your function here! # Below are some lines of code that will test your function. # You can change the value of the variable(s) to test your # function with different inputs. # # If your function works correctly, this will originally # print: bonjour it's moi print(french2eng("Hello it's me"))
french_dict = {"me": "moi", "hello": "bonjour",
"goodbye": "au revoir", "cat": "chat",
"dog": "chien", "and": "et"}
def french2eng(s):
s=s.split() #splitting the string to list of words
for i in range(0,len(s)): #accessing
each wprd in the list
key=s[i].lower()
#converting each word to lowercase
if key in
french_dict.keys(): #if word is in dictionary
s[i]=french_dict[key] #change the word in list to it's
corresponding value
s=" ".join(s) #join the list with a
space
return s #return the string
print(french2eng("Hello it's me"))
print()
print(french2eng("Dog and cat"))
Please give a like
Create a new program in Mu and save it as ps4.5.2.py and take the code below...
Create a new program in Mu and save it as ps4.5.1.py and take the code below and fix it as indicated in the comments: # Write a function called phonebook that takes two lists as # input: # # - names, a list of names as strings # - numbers, a list of phone numbers as strings # # phonebook() should take these two lists and create a # dictionary that maps each name to its phone number. For #...
Create a new program in Mu and save it as ps4.3.1.py and take the code below and fix it as indicated in the comments: # Write a function called grade_scantron. grade_scantron should # take as input two lists: answers and key. Each list contain # strings. Each string will be only one letter, a character # from A to E. grade_scantron should return how many questions # the student got "right", where a student gets a question # right if...
Create a new program in Mu and save it as ps4.2.2.py and take the code below and fix it as indicated in the comments: # Write a function called "in_parentheses" that accepts a # single argument, a string representing a sentence that # contains some words in parentheses. Your function should # return the contents of the parentheses. # # For example: # in_parentheses("This is a sentence (words!)") -> "words!" # # If no text appears in parentheses, return an...
Create a new program in Mu and save it as ps3.2.1.py and take the code below and fix it as indicated in the comments: hour = 3 minute = 45 # You may modify the lines of code above, but don't move them! # When you Submit your code, we'll change these lines to # assign different values to the variables. # Around Georgia Tech, there are plenty of places to get a # late night bite to eat. However,...
Create a new program in Mu and save it as ps4.5.3.py and take the code below and fix it as indicated in the comments: # In the Pokemon video game series, every Pokemon has six # stats: HP, Attack, Defense, Special Attack, Special Defense, # and Speed. # # Write a function called total_stats that will take as input # a list of dictionaries. Each dictionary will have seven # key-value pairs: # # - name: a Pokemon's name #...
Create a new program in Mu and save it as ps4.3.2.py and take the code below and fix it as indicated in the comments: # Write a function called find_max_sales. find_max_sales will # have one parameter: a list of tuples. Each tuple in the # list will have two items: a string and an integer. The # string will represent the name of a movie, and the integer # will represent that movie's total ticket sales (in millions # of...
Create a new program in Mu and save it as ps3.2.2.py and take the code below and fix it as indicated in the comments: egg = True milk = True butter = True flour = True # You may modify the lines of code above, but don't move them! # When you Submit your code, we'll change these lines to # assign different values to the variables. # Imagine you're deciding what you want to cook. The boolean # variables...
Create a new program in Mu and save it as ps4.2.1.py and take the code below and fix it as indicated in the comments: # Write function called third_character that accepts a # string as an argument and returns the third character # of the string. If the user inputs a string with fewer than # 3 characters, return "Too short". # Write your function here! # Below are some lines of code that will test your function. # You...
Create a new program in Mu and save it as ps4.3.3.py and take the code below and fix it as indicated in the comments: # Imagine you're writing some code for an exercise tracker. # The tracker measures heart rate, and should display the # average heart rate from an exercise session. # # However, the tracker doesn't automatically know when the # exercise session began. It assumes the session starts the # first time it sees a heart rate...
Create a new program in Mu and save it as ps4.4.1.py and take the code below and fix it as indicated in the comments: # Write a function called "save_leaderboard" that accepts a # single list of tuples as a parameter. The tuples are of the # form (leader_name, score) # The function should open a file called "leaderboard.txt", # and write each of the tuples in the list to a line in the file. # The name and score...