{"id":180051,"date":"2025-01-03T15:53:18","date_gmt":"2025-01-03T15:53:18","guid":{"rendered":"https:\/\/learnexams.com\/blog\/?p=180051"},"modified":"2025-01-03T15:53:20","modified_gmt":"2025-01-03T15:53:20","slug":"write-a-program-that-awards-olympians-for-winning-gold-medals","status":"publish","type":"post","link":"https:\/\/www.learnexams.com\/blog\/2025\/01\/03\/write-a-program-that-awards-olympians-for-winning-gold-medals\/","title":{"rendered":"Write a program that awards olympians for winning gold medals"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Write a program that awards olympians for winning gold medals. Let\u2019s say there are five events today, and for every gold medal the winner receives $75,000. Prompt the user for how many gold medals the Olympian won. The Get_Winnings(m) function should take exactly one parameter\u2014a string for the number of gold medals. It will return either an integer for the money won or a string Invalid, if the amount is invalid. Olympians can win one more medal per day.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><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<p class=\"wp-block-paragraph\">To create a Python program that calculates the earnings of an Olympian based on the number of gold medals won, we can follow these steps:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Prompt the User<\/strong>: Ask the user to input the number of gold medals won.<\/li>\n\n\n\n<li><strong>Validate the Input<\/strong>: Ensure the input is a valid integer within the acceptable range (0 to 5).<\/li>\n\n\n\n<li><strong>Calculate Earnings<\/strong>: Multiply the number of valid gold medals by $75,000 to determine the total earnings.<\/li>\n\n\n\n<li><strong>Handle Invalid Input<\/strong>: If the input is invalid (non-integer or out of range), return &#8220;Invalid&#8221;.<\/li>\n<\/ol>\n\n\n\n<p class=\"wp-block-paragraph\">Here&#8217;s the Python code implementing this logic:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>def Get_Winnings(m):\n    try:\n        # Convert the input string to an integer\n        medals = int(m)\n        # Check if the number of medals is within the valid range\n        if 0 &lt;= medals &lt;= 5:\n            # Calculate the total earnings\n            earnings = medals * 75000\n            return earnings\n        else:\n            return \"Invalid\"\n    except ValueError:\n        # Return \"Invalid\" if the input is not a valid integer\n        return \"Invalid\"\n\n# Prompt the user for input\nmedals_won = input(\"Enter the number of gold medals won today: \")\n\n# Get the earnings or error message\nresult = Get_Winnings(medals_won)\n\n# Display the result\nprint(result)<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Explanation<\/strong>:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Function Definition<\/strong>: The <code>Get_Winnings<\/code> function takes one parameter <code>m<\/code>, which is expected to be a string representing the number of gold medals won.<\/li>\n\n\n\n<li><strong>Input Conversion and Validation<\/strong>: Inside the function, we attempt to convert <code>m<\/code> to an integer using <code>int(m)<\/code>. If this conversion fails (i.e., if <code>m<\/code> is not a valid integer), a <code>ValueError<\/code> is raised, and the function returns &#8220;Invalid&#8221;.<\/li>\n\n\n\n<li><strong>Range Check<\/strong>: If the conversion is successful, we check if the number of medals is between 0 and 5 (inclusive). If it is, we calculate the earnings by multiplying the number of medals by $75,000. If the number is outside this range, the function returns &#8220;Invalid&#8221;.<\/li>\n\n\n\n<li><strong>User Interaction<\/strong>: The program prompts the user to enter the number of gold medals won, calls the <code>Get_Winnings<\/code> function with the user&#8217;s input, and prints the result.<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Example Runs<\/strong>:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Valid Input<\/strong>:<\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>   Enter the number of gold medals won today: 3\n   225000<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">In this case, the user entered <code>3<\/code>, which is a valid number of medals. The function calculates <code>3 * 75000 = 225000<\/code> and returns this value.<\/p>\n\n\n\n<ol start=\"2\" class=\"wp-block-list\">\n<li><strong>Invalid Input (Non-integer)<\/strong>:<\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>   Enter the number of gold medals won today: three\n   Invalid<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Here, the user entered a non-integer value. The <code>int(m)<\/code> conversion raises a <code>ValueError<\/code>, and the function returns &#8220;Invalid&#8221;.<\/p>\n\n\n\n<ol start=\"3\" class=\"wp-block-list\">\n<li><strong>Invalid Input (Out of Range)<\/strong>:<\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>   Enter the number of gold medals won today: 6\n   Invalid<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The user entered <code>6<\/code>, which is outside the valid range of 0 to 5. The function returns &#8220;Invalid&#8221;.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Additional Considerations<\/strong>:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Input Prompting<\/strong>: The prompt message clearly instructs the user to enter the number of gold medals won today.<\/li>\n\n\n\n<li><strong>Error Handling<\/strong>: The use of a <code>try-except<\/code> block ensures that the program gracefully handles invalid inputs without crashing.<\/li>\n\n\n\n<li><strong>Range Validation<\/strong>: The check <code>0 &lt;= medals &lt;= 5<\/code> ensures that only valid numbers of medals are accepted.<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">This program effectively calculates the earnings of an Olympian based on the number of gold medals won, handling various input scenarios appropriately.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Write a program that awards olympians for winning gold medals. Let\u2019s say there are five events today, and for every gold medal the winner receives $75,000. Prompt the user for how many gold medals the Olympian won. The Get_Winnings(m) function should take exactly one parameter\u2014a string for the number of gold medals. It will return [&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-180051","post","type-post","status-publish","format-standard","hentry","category-exams-certification"],"_links":{"self":[{"href":"https:\/\/www.learnexams.com\/blog\/wp-json\/wp\/v2\/posts\/180051","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=180051"}],"version-history":[{"count":0,"href":"https:\/\/www.learnexams.com\/blog\/wp-json\/wp\/v2\/posts\/180051\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.learnexams.com\/blog\/wp-json\/wp\/v2\/media?parent=180051"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.learnexams.com\/blog\/wp-json\/wp\/v2\/categories?post=180051"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.learnexams.com\/blog\/wp-json\/wp\/v2\/tags?post=180051"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}