{"id":191804,"date":"2025-02-16T07:53:58","date_gmt":"2025-02-16T07:53:58","guid":{"rendered":"https:\/\/learnexams.com\/blog\/?p=191804"},"modified":"2025-02-16T07:54:04","modified_gmt":"2025-02-16T07:54:04","slug":"python","status":"publish","type":"post","link":"https:\/\/www.learnexams.com\/blog\/2025\/02\/16\/python\/","title":{"rendered":"Python"},"content":{"rendered":"\n<p>Python:<\/p>\n\n\n\n<p>import random<br>from tabulate import tabulate<\/p>\n\n\n\n<h1 class=\"wp-block-heading\">select the marks<\/h1>\n\n\n\n<p>gradePoints = {&#8220;A&#8221;:4,&#8221;B&#8221;:3,&#8221;C&#8221;:2,&#8221;D&#8221;:1,&#8221;F&#8221;:0}<br>courseList = [&#8220;CST 161&#8243;,&#8221;Mat 144&#8243;,&#8221;ENG 201&#8243;,&#8221;PSY 101&#8243;,&#8221;HIS 101&#8221;]<br>gradeList = [&#8220;A&#8221;,&#8221;B&#8221;,&#8221;C&#8221;,&#8221;D&#8221;,&#8221;F&#8221;]<br>creditList = [3,4]<\/p>\n\n\n\n<p>data = []<br>total_credit = 0<br>sums = 0<\/p>\n\n\n\n<h1 class=\"wp-block-heading\">input the number of course that you want to select randomly<\/h1>\n\n\n\n<p>n = int(input(&#8220;Enter the number of courses : &#8220;))<br>for i in range(n):<br>\u00c3\u201a\u00c2 \u00c3\u201a\u00c2 temp = []<br>\u00c3\u201a\u00c2 \u00c3\u201a\u00c2 #for courses<br>\u00c3\u201a\u00c2 \u00c3\u201a\u00c2 k = random.randint(0,4)<br>\u00c3\u201a\u00c2 \u00c3\u201a\u00c2 temp.append(courseList[k])<br>\u00c3\u201a\u00c2 \u00c3\u201a\u00c2 #for credits<br>\u00c3\u201a\u00c2 \u00c3\u201a\u00c2 k = random.randint(0,1)<br>\u00c3\u201a\u00c2 \u00c3\u201a\u00c2 total_credit += creditList[k]<br>\u00c3\u201a\u00c2 \u00c3\u201a\u00c2 temp.append(creditList[k])<br>\u00c3\u201a\u00c2 \u00c3\u201a\u00c2 #for grades<br>\u00c3\u201a\u00c2 \u00c3\u201a\u00c2 k = random.randint(0,4)<br>\u00c3\u201a\u00c2 \u00c3\u201a\u00c2 temp.append(gradeList[k])<br>\u00c3\u201a\u00c2 \u00c3\u201a\u00c2 #for gradepoints<br>\u00c3\u201a\u00c2 \u00c3\u201a\u00c2 sums += gradePoints[gradeList[k]] * temp[1]<br>\u00c3\u201a\u00c2 \u00c3\u201a\u00c2 temp.append(gradePoints[gradeList[k]] * temp[1])<\/p>\n\n\n\n<p>\u00c3\u201a\u00c2 \u00c3\u201a\u00c2 data.append(temp)<\/p>\n\n\n\n<p>print(tabulate(data,headers = [&#8220;Course&#8221;,&#8221;Credits&#8221;,&#8221;Grade&#8221;,&#8221;Value Per Course&#8221;]))<br>print(f&#8221;Total credits taken: {total_credit}&#8221;)<br>print(f&#8221;Total quality points earned : {sums}&#8221;)<\/p>\n\n\n\n<p><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-ast-global-color-6-color\"><strong>The Correct Answer and Explanation is :<\/strong><\/mark><\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Explanation:<\/h3>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Imports and Initialization:<\/strong><\/li>\n<\/ol>\n\n\n\n<ul class=\"wp-block-list\">\n<li>The code uses <code>random<\/code> to select courses, grades, and credits randomly, and <code>tabulate<\/code> to format the output into a table.<\/li>\n\n\n\n<li><code>gradePoints<\/code> stores the numeric values for letter grades, where A=4, B=3, etc.<\/li>\n\n\n\n<li><code>courseList<\/code>, <code>gradeList<\/code>, and <code>creditList<\/code> define the courses, possible grades, and credit hours that will be used for the random selection.<\/li>\n<\/ul>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Data Collection:<\/strong><\/li>\n<\/ol>\n\n\n\n<ul class=\"wp-block-list\">\n<li>The user is prompted to input the number of courses they want to select.<\/li>\n\n\n\n<li>A loop runs <code>n<\/code> times, selecting a course, credit hours, and grade randomly for each iteration. For each course selected, its grade points (calculated as <code>gradePoints[grade] * credit<\/code>) are accumulated in the variable <code>sums<\/code>.<\/li>\n\n\n\n<li>The total credit hours (<code>total_credit<\/code>) are also accumulated.<\/li>\n<\/ul>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Tabulate Output:<\/strong><\/li>\n<\/ol>\n\n\n\n<ul class=\"wp-block-list\">\n<li>The <code>tabulate()<\/code> function is used to present the data in a neat tabular format with headers for each column: &#8220;Course&#8221;, &#8220;Credits&#8221;, &#8220;Grade&#8221;, and &#8220;Value Per Course.&#8221;<\/li>\n<\/ul>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Calculation of Total Credits and Quality Points:<\/strong><\/li>\n<\/ol>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Finally, after the loop, the total number of credits and total quality points earned are displayed.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">Key Fixes:<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Indentation Issues:<\/strong> Python is very sensitive to indentation. The original code had strange characters (<code>\u00c3\u201a\u00c2<\/code>) and inconsistent indentation, which have been fixed.<\/li>\n\n\n\n<li><strong>Correct Calculation of Grade Points:<\/strong> The grade points for each course are now correctly calculated by multiplying the credit hours with the grade&#8217;s corresponding value.<\/li>\n\n\n\n<li><strong>Fixed Looping and Data Appending:<\/strong> Ensured that data was being correctly appended into the <code>data<\/code> list.<\/li>\n<\/ul>\n\n\n\n<p>This code simulates selecting random courses, grades, and credits, and then calculates and displays the total credits and grade points.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Python: import randomfrom tabulate import tabulate select the marks gradePoints = {&#8220;A&#8221;:4,&#8221;B&#8221;:3,&#8221;C&#8221;:2,&#8221;D&#8221;:1,&#8221;F&#8221;:0}courseList = [&#8220;CST 161&#8243;,&#8221;Mat 144&#8243;,&#8221;ENG 201&#8243;,&#8221;PSY 101&#8243;,&#8221;HIS 101&#8221;]gradeList = [&#8220;A&#8221;,&#8221;B&#8221;,&#8221;C&#8221;,&#8221;D&#8221;,&#8221;F&#8221;]creditList = [3,4] data = []total_credit = 0sums = 0 input the number of course that you want to select randomly n = int(input(&#8220;Enter the number of courses : &#8220;))for i in range(n):\u00c3\u201a\u00c2 \u00c3\u201a\u00c2 temp [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"default","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","ast-disable-related-posts":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"default","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"footnotes":""},"categories":[25],"tags":[],"class_list":["post-191804","post","type-post","status-publish","format-standard","hentry","category-exams-certification"],"_links":{"self":[{"href":"https:\/\/www.learnexams.com\/blog\/wp-json\/wp\/v2\/posts\/191804","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.learnexams.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.learnexams.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.learnexams.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.learnexams.com\/blog\/wp-json\/wp\/v2\/comments?post=191804"}],"version-history":[{"count":0,"href":"https:\/\/www.learnexams.com\/blog\/wp-json\/wp\/v2\/posts\/191804\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.learnexams.com\/blog\/wp-json\/wp\/v2\/media?parent=191804"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.learnexams.com\/blog\/wp-json\/wp\/v2\/categories?post=191804"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.learnexams.com\/blog\/wp-json\/wp\/v2\/tags?post=191804"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}