Concept explainers
Question: Hello, for the last line in the code, could anyone show me how to perform it using f' strings, instead of the format() method and join()method. I can use f strings to format the first half of the code,
print(f' {course:<8} {avg_grade:>4.1f}')
In the code, gradebook is the dictionary, course is the key, and grades is the value(grades is in the format of a list)
for course, grades in gradebook.items():
grades.sort() # sort the list of grades
sum_grades = sum(grades) # sum all grades for the course
count_grades = len(grades) # determine the number of grades in the list of grades
avg_grade = sum_grades / count_grades # calculate the average for the course
if avg_grade < lowest_avg[1]: # determine the course with the lowest average
lowest_avg = (course, avg_grade)
print("{:<8} {:>4.1f}% {:>25}".format(course, avg_grade, " ".join("{:>4}%".format(g) for g in grades)))
Trending nowThis is a popular solution!
Step by stepSolved in 3 steps
Thank you, I understand how the code is executed. The problem is that I'm only allowed to use either a for loop, or list comprehension to achieve the result. Is there any way to do so without implementing the "join" function or method, as I have not learned that in class yet.
Here is the exact requirements:
- use another for loop to print all grades in the grades list for the course. Use a format identifier to allow for printing of a right-justified grade with a width of 4 followed by a percent sign
Is there any way to only use f strings, for loops, and/or list comprehension to achieve the same result.
Thank you
Hello thank you for the solution. Is there also a way to format the list without using the .join method, and just using f' strings, perhaps in a list comprehension?
Thank you, I understand how the code is executed. The problem is that I'm only allowed to use either a for loop, or list comprehension to achieve the result. Is there any way to do so without implementing the "join" function or method, as I have not learned that in class yet.
Here is the exact requirements:
- use another for loop to print all grades in the grades list for the course. Use a format identifier to allow for printing of a right-justified grade with a width of 4 followed by a percent sign
Is there any way to only use f strings, for loops, and/or list comprehension to achieve the same result.
Thank you
Hello thank you for the solution. Is there also a way to format the list without using the .join method, and just using f' strings, perhaps in a list comprehension?
- This code is a part of a C dictionary. Please write the code for the below requirements. dict_get Next, you will implement: char* dict_get (const dict_t* dict, const char* key); This function goes through the list given by dict. If you use the above structure, this means starting at el = dict->head and checking each time whether the key at el is key; if it is not, we set el = el->next, until either key is found, or we reach el == NULL. To compare key with el->key, we need to compare one by one each character in key with those in el->key. Remember that strings in C are just pointers (memory addresses) to the first character in the string, so comparing el->key == key will not do what you want. So how do you even get the length of a string s? You would start at memory location s and advance as long as the byte at s (i.e. *s) is not the end-of-string marker (\0, the NULL character). This can get a bit messy, so luckily, you are allowed to use the string comparison…arrow_forwardWrite code that iterates through a dictionary represented by the variable my_dict and prints any values (not keys!) associated with keys beginning with the letter "k" (lowercase).arrow_forwardWrite a program that creates a dictionary containing Canadian provinces' abbreviations as the keys (e.g., 'on') and their long format as the values (e.g., 'Ontario'). Use the internet to get a list of provinces and their abbreviations. The program should then randomly quiz the user by displaying the abbreviation of the province and asking the user to enter the extended format for the abbreviation. The program should keep count of the number of correct and incorrect responses.arrow_forward
- Consider the following line of code: int[] somearray = new int[30]; Which one of the following options is a valid line of code for displaying the twenty-eighth element of somearray?arrow_forwardAs part of this assignment, the program that you will be writing will store current grades in a dictionary using course codes as keys and with values consisting of percent grades in lists. The main functions of this program are to print a student's gradebook, to drop the lowest grade in each course, print the student's gradebook again, drop the course with lowest average, and finally printing the student's gradebook again. This program requires a main function and a custom value-returning function. In the main function, code these basic steps in this sequence (intermediate steps may be missing): start with an empty dictionary that represents a gradebook and then use a while loop to allow the input of course codes from the keyboard. End the while loop when the user presses enter without entering data.within the while loop:for each course entered, use a list comprehension to generate five random integers in the range of 70 through 100. These random integers in a list represent the…arrow_forwardPlease answer the ques in python with showing the answers. Q: A car dictionary: a) Implement a dictionary car with the keys brand, model, and color. Give appropriate values for each key (you choose). b) Print each item in the dictionary (see example output) c) Add a key transmission to the dictionary with the value ‘automatic’. d) Print the value for the key transmission (use the get method). Output Example: subaru crosstrek sapphire blue automaticarrow_forward
- Focus on dictionary methods, use of functions, and good programming styleFor this assignment, you will create a glossary (dictionary) of technical terms and definitions. It will be set up as a Python dictionary structure. The file glossary_starter.py is a complete starter framework for the assignment. It includes some initial values for the dictionary. It is long because most of the code has already been written for you.Your task is to complete the five individual functions for adding and deleting terms, looking up terms, listing them, and printing out both the terms and definitions. These functions are all short, just a couple of lines, and use basic dictionary methods and techniques.arrow_forwardFor example, suppose the word “robot” is found in lines 7, 18, 94, and 138. The dictionary would contain an element in which the key was the string “robot”, and the value was a list containing the numbers 7, 18, 94, and 138. Once the dictionary is built, you should prompt the user for a word. If the word is found in the dictionary, then print out all the lines on which they are found in ascending order. The code for the print out is provided in the stub code. A stub code is provided for you. If you do not see it, click the "Load default template" button. # The get_word_dict function returns a dictionary containing# the words from line_list as keys, and their line numbers# as values.def get_word_dict(line_list):# Create a line counter.count = 0 # Create a dictionary to hold the words.word_dict = {} # Step through the list of lines.#######Add you code below############# ####################################### Return the dictionary.return word_dict #####DO NOT CHANGE ANYTHING…arrow_forwardOverview This is a review assignment where you will use multiple tools you have picked up so far throughout the course. If you're confused about how to do something make sure you look back at earlier assignments. In this program you will start by declaring an empty dictionary named population_dict. Then you will use a while-loop to prompt a user for a series of cities and their populations, storing the input in population_dict). After this you will prompt the user to decide what is the maximum population for a city to be considers a small down. Finally, you will open a file called populationdata.txt where you will write the results. Expected Output Example of standard out (the console): What is next city? (enter q to quit) Long Beach What is the population of Long Beach? 362000 What is next city? (enter q to quit) Los Angeles What is the population of Los Angeles? 382000000 What is next city? (enter q to quit) San Diego What is the population of San Diego? 136000000 What is next city?…arrow_forward
- Database System ConceptsComputer ScienceISBN:9780078022159Author:Abraham Silberschatz Professor, Henry F. Korth, S. SudarshanPublisher:McGraw-Hill EducationStarting Out with Python (4th Edition)Computer ScienceISBN:9780134444321Author:Tony GaddisPublisher:PEARSONDigital Fundamentals (11th Edition)Computer ScienceISBN:9780132737968Author:Thomas L. FloydPublisher:PEARSON
- C How to Program (8th Edition)Computer ScienceISBN:9780133976892Author:Paul J. Deitel, Harvey DeitelPublisher:PEARSONDatabase Systems: Design, Implementation, & Manag...Computer ScienceISBN:9781337627900Author:Carlos Coronel, Steven MorrisPublisher:Cengage LearningProgrammable Logic ControllersComputer ScienceISBN:9780073373843Author:Frank D. PetruzellaPublisher:McGraw-Hill Education