{"id":20314,"date":"2025-06-14T09:23:08","date_gmt":"2025-06-14T09:23:08","guid":{"rendered":"https:\/\/gaviki.com\/blog\/?p=20314"},"modified":"2025-06-14T09:23:17","modified_gmt":"2025-06-14T09:23:17","slug":"design-a-function-that-collets-seven-days-of-temperatures-from-the-user-using-a-while-loop","status":"publish","type":"post","link":"https:\/\/gaviki.com\/blog\/design-a-function-that-collets-seven-days-of-temperatures-from-the-user-using-a-while-loop\/","title":{"rendered":"Design a function that collets seven days of temperatures from the user using a while loop"},"content":{"rendered":"\n<p>Design a function that collets seven days of temperatures from the user using a while loop. This function accepts no parameters<\/p>\n\n\n\n<p><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-cyan-blue-color\"><strong>The correct answer and explanation is:<\/strong><\/mark><\/p>\n\n\n\n<p>Here&#8217;s a Python function that collects seven days of temperatures from the user using a <code>while<\/code> loop and accepts no parameters:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>def collect_temperatures():\n    temperatures = &#91;]\n    day = 1\n\n    while day &lt;= 7:\n        try:\n            temp = float(input(f\"Enter the temperature for day {day}: \"))\n            temperatures.append(temp)\n            day += 1\n        except ValueError:\n            print(\"Invalid input. Please enter a numeric value.\")\n\n    print(\"You entered the following temperatures:\")\n    for i, t in enumerate(temperatures, 1):\n        print(f\"Day {i}: {t}\u00b0\")\n<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">\ud83d\udd0d <strong>Explanation (300 words)<\/strong><\/h3>\n\n\n\n<p>This function, <code>collect_temperatures<\/code>, is designed to collect temperature data for seven days from a user. It does <strong>not take any parameters<\/strong>, as specified. Here&#8217;s how it works:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Initialization<\/strong>:\n<ul class=\"wp-block-list\">\n<li>An empty list <code>temperatures<\/code> is created to store the temperature values.<\/li>\n\n\n\n<li>A counter variable <code>day<\/code> is initialized to 1 to keep track of the current day.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Using a <code>while<\/code> Loop<\/strong>:\n<ul class=\"wp-block-list\">\n<li>The <code>while<\/code> loop continues <strong>as long as <code>day<\/code> is less than or equal to 7<\/strong>. This ensures that temperatures are collected for exactly seven days.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>User Input with Error Handling<\/strong>:\n<ul class=\"wp-block-list\">\n<li>Inside the loop, the user is prompted to enter a temperature for the current day.<\/li>\n\n\n\n<li>The <code>input()<\/code> function is used to receive user input, which is then converted to a <code>float<\/code>.<\/li>\n\n\n\n<li>If the input is not a valid number (e.g. letters or symbols), a <code>ValueError<\/code> is raised. The <code>try-except<\/code> block catches this and asks the user to try again without incrementing the <code>day<\/code>.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Storing the Data<\/strong>:\n<ul class=\"wp-block-list\">\n<li>Valid temperatures are appended to the <code>temperatures<\/code> list.<\/li>\n\n\n\n<li>The day counter is incremented only when a valid input is received.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Displaying the Results<\/strong>:\n<ul class=\"wp-block-list\">\n<li>After collecting all inputs, the function prints each temperature value along with the corresponding day using a <code>for<\/code> loop with <code>enumerate<\/code>.<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n\n\n\n<p>This approach ensures reliable data collection while protecting against invalid user inputs, all managed efficiently with a simple <code>while<\/code> loop.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Design a function that collets seven days of temperatures from the user using a while loop. This function accepts no parameters The correct answer and explanation is: Here&#8217;s a Python function that collects seven days of temperatures from the user using a while loop and accepts no parameters: \ud83d\udd0d Explanation (300 words) This function, collect_temperatures, [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-20314","post","type-post","status-publish","format-standard","hentry","category-quiz-questions"],"_links":{"self":[{"href":"https:\/\/gaviki.com\/blog\/wp-json\/wp\/v2\/posts\/20314","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/gaviki.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/gaviki.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/gaviki.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/gaviki.com\/blog\/wp-json\/wp\/v2\/comments?post=20314"}],"version-history":[{"count":1,"href":"https:\/\/gaviki.com\/blog\/wp-json\/wp\/v2\/posts\/20314\/revisions"}],"predecessor-version":[{"id":20315,"href":"https:\/\/gaviki.com\/blog\/wp-json\/wp\/v2\/posts\/20314\/revisions\/20315"}],"wp:attachment":[{"href":"https:\/\/gaviki.com\/blog\/wp-json\/wp\/v2\/media?parent=20314"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/gaviki.com\/blog\/wp-json\/wp\/v2\/categories?post=20314"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/gaviki.com\/blog\/wp-json\/wp\/v2\/tags?post=20314"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}