Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Problem 1 The list inp is a list of integers. Write a while loop that does the following: Iterates over the list inp. If the

Problem 1
The list inp is a list of integers. Write a while loop that does the following:
Iterates over the list inp.
If the number is greater than 255, terminate the loop.
If the number is less than 33 or greater than 126, print the skip message shown.
Use the chr function to convert the number to an ASCII printable character.
Catenate(join) the character to the string variable message.
Print the message.
You will need to use an IF-ELIF-ELSE statement.
inp =[71,111,130,32,66,108,117,127,101,33,256,71,111,1032,66,108,117,127,101]
# write your code below
Expected Output:
Skipped 130
Skipped 32
Skipped 127
The message is GoBlue!
Problem 2
The list type has a method called pop which removes and returns an element from a list using a index passed to the method. For example, given:
lst =['a','b','c']
c = lst.pop(0) # return the first element 'a' in the list, remove it from lst.
# at this point, c has the value 'a' and lst has the elements ['b','c']
c = lst.pop(0) # return the first element 'b' in the list, remove it from lst.
# at this point, c has the value 'b' and lst has the elements ['c']
Write code that builds a list of things from elements of the list things.
Do the following:
Initialize a variable to an empty list
Iterate continuously using while:
Pop the first element in the list things.
If element has a value of 'none' or 'None'
Terminate the loop
Append the element to the list.
Print the list
things =['books','movies','none','fitness','yoga','running']
# write your code below
Expected Output:
['books', 'movies']
Problem 3
Write a code that calculates the largest, smallest and average score in the set of entered scores.
Do the following:
Initialize a variable to an empty list. This variable will hold the scores.
Initialize a counter variable.
Accept the number of scores from the user.
Iterate continuously:
If the counter is less than the number of scores:
Accept a score from the user.
Append the score to the scores list.
increment the counter.
Otherwise:
Calculate the largest score (you may use a function).
Calculate the smallest score (you may use a function).
Terminate the loop
Calculate the average score.
Print the average, smallest, and largest score as shown below.
Expected Output:
Given the input below:
How many scores are you entering? 5
Add another number: 10
Add another number: 15
Add another number: 18
Add another number: 13
Add another number: 6
The average score is: 12.4
The smallest score is: 6
The largest score is: 18
Problem 4
Complete the function called gcf that calculates the Greatest Common Factor(Divisor) given two integers. The function accepts two integers x and y as parameters and returns the GCF using the Euclidean algorithm below. You need to use a while loop.
Step 1: r = x % y
Step 2: x = y
Step 3: y = r
Step 4: Repeat Steps 1 through 3 while y is greater than 0
Step 5: GCF = x
Write code that iterates over the list number_pairs (you may use a for statement) and calls the function gcf with each pair and prints the output shown below.
# write the function below
# Do not change the line below
number_pairs =[(12,24),(24,48),(17,37)]
# write your code below
Expected Output:
The greatest common factor of (12,24) is 12
The greatest common factor of (24,48) is 24
The greatest common factor of (17,37) is 1
Problem 5
The problem requires using the function computeScrabbleScore developed in an earlier problem set. Copy the code for the function to the cell below.
Write code that repeatedly prompts the user for a word until the sentinel "None" or "none" is entered. In the loop, call computeScrabbleScore with the word entered then add the word and its value as a key-value pair into the dictionary scrabbleWords. Print scrabbleWords.
# Copy the function computeScrabbleScore here
# write your code below
Expected Output:
Given the input below:
Please input a word, enter 'None' or 'none' to cancel: Hello
Please input a word, enter 'None' or 'none' to cancel: World
Please input a word, enter 'None' or 'none' to cancel: None
The expected output is:
{'hello': 8, 'world': 9}
Problem 6
Alter the above code so that it iterates over the list things and builds a list of tuples where the first element of the tuple is the list element and the second the Scrabble value of the element. Use the while statement to iterate continuously over things stopping when the list element has a value of 'none' or 'None'. Print the list after the loop terminates.
Use the list pop method to extract elements from things.
things =['books','movies','fitness','yoga','running','None','a','b']
# Copy the function computeScrabbleScore here
# write your code below
Expected Output:
[('books',11),('movies',15),('fitness',10),('yoga',8),('running',8)]
Problem 7
Write code that divides each value in the list dividend by eac

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions