Neural Sync Active
Jan 2026 - Python - Week 3 - GrPA 1 - While Loop - GRADED
Registry Synced
Jan 2026 - Python - Week 3 - GrPA 1 - While Loop - GRADED
2274 words
11 min read
GrPA 1 - While Loop - GRADED
Course: Jan 2026 - Python
GrPA 1 - While Loop - GRADED
Submission deadline has passed for this assignment
Due Mar 04, 2026 at 11:59 PM IST
Instructions
Use "Test Run" to verify your code with public test cases.
Press "Submit" to have your assignment evaluated.
You can submit your assignment multiple times up until the deadline.
Make sure to submit your final code by the deadline to receive your score.
Summary
100 out of100
Score
Public Tests
16/16 Passed
Submitted on Mar 04, 2026 at 11:18 PM IST
Private Tests
16/16 Passed
Submitted on Mar 06, 2026 at 11:06 PM IST
.prob-statement { table { margin: 1em 0; border-collapse: collapse; width: 100%; overflow-x: auto; display: block; font-variant-numeric: lining-nums tabular-nums; } table caption { margin-bottom: 0.75em; } tbody { margin-top: 0.5em; border-top: thin solid #000; border-bottom: thin solid #000; } th, td { border: thin solid #000; padding: 0.5em 0.5em 0.25em 0.5em; } code { white-space: pre-wrap; } span.smallcaps { font-variant: small-caps; } div.columns { display: flex; gap: min(4vw, 1.5em); } div.column { flex: auto; overflow-x: auto; } div.hanging-indent { margin-left: 1.5em; text-indent: -1.5em; } ul.task-list { list-style: none; } ul.task-list li input[type="checkbox"] { width: 0.8em; margin: 0 0.8em 0.2em -1.6em; vertical-align: middle; } pre { line-height: 1.5; } pre code { color: unset; font-family: unset; padding: unset; } code { color: red; font-family: monospace; padding: .2rem; } }
Change in eligibility criteria to write oppe1 exam:
A1>=40/100 AND A2>=40/100 AND A3>=40/100 AND A4>=40/100
**
โ
Important Note on
while loop๐:**- Use while only when the number of iterations is indefinite.
- If you can term the steps as do
ntimes, do once for each item, etc. useforloop instead. - If you can only term the steps as do until something happens. Like when user inputs
10.
A bit of wisdom ๐ There are maily two ways in which while loops are used in the context of taking inputs until a terminal word.
python# method 1 a = input() while a != terminal_word: # opposite of the terminal condition # do something with a a = input() # take the next a # method 2 while True: # loop forever a = input() if a == terminal_word: # the terminal condition break # do something with a
Problem Statement
Problem type - Standard Input - Standard Output
NOTE: None of this problem statements can be written using a
for since the number of repetition is indefinite.Implement different parts of a multi-functional program based on an initial input value. Each part of the program will handle various tasks related to accumulation, filtering, mapping, and combinations of these operations. None of the tasks should use explicit loops for definite repetitions, and the program should handle indefinite inputs gracefully.
Tasks
- Accumulation - Accumulating a final result
sum_until_0: Continuously read integers from standard input until you receive a zero. Print the sum of these integers.total_price: Continuously read pairs of integers from standard input, representing the quantity and price of items, until you receive the string "END". Print the total price of all items.
- Filtering - Selecting based on a criterion
only_ed_or_ing: Continuously read strings from standard input until you encounter the word "STOP" (case insensitive and not included in the output). Print only those strings that end with "ed" or "ing" (case insensitive).reverse_sum_palindrome: Continuously read positive integers from standard input until you encounter a "-1"(not included in the output). Print only those integers for which the sum of the number and its reverse is a palindrome.
- Mapping - Applying the same operation to different items
double_string: Continuously read lines from standard input until an empty line is encountered. Print each line repeated twice.odd_char: Continuously read strings from standard input until you encounter a string ending with a "."(include that string with the "." in the output). Extract characters at odd positions (starting from 1) of each line, and print the results in a single line separated by spaces.
- Filter and Map - Applying an operation to selected items
only_even_squares: Continuously read numbers from standard input until "NAN" is encountered. Print the square of each number only if it is even.
- Filter and Accumulate - Accumulating a result with selected items
only_odd_lines: Continuously read lines from standard input until "END"(not included in the output) is encountered. Create a string by prepending only the odd lines (starting from 1) with a newline character in between, and print the result which will be the odd lines in reverse order.
Template Code(Click to Expand)
pythonif task == "sum_until_0": total = 0 n = int(input()) while ...: # the terminal condition ... # add n to the total ... # take the next n form the input print(total) elif task == "total_price": total_price = 0 while ...: # repeat forever since we are breaking inside line = input() if ...: # The terminal condition break quantity, price = line.split() # split uses space by default quantity, price = ... # convert to ints ... # accumulate the total price print(total_price) elif task == "only_ed_or_ing": ... elif task == "reverse_sum_palindrome": ... elif task == "double_string": ... elif task == "odd_char": ... elif task == "only_even_squares": ... elif task == "only_odd_lines": ...
Python Tutor
Starboard Notebook
Pyodide Terminal
function require(url, callback) { var e = document.createElement("script"); e.src = url; e.type = "text/javascript"; e.addEventListener('load', callback); document.getElementsByTagName("head")[0].appendChild(e); } require("https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.9.0/highlight.min.js", function () { hljs.highlightAll() });
Public Tests ( 16/16 )
Case 1
Input:
textsum_until_0 5 3 2 0
Expected Output:
text10
Actual Output:
text10
Case 2
Input:
textsum_until_0 10 -5 5 0
Case 3
Input:
texttotal_price 2 50 1 100 3 30 END
Expected Output:
text290
Actual Output:
text290
Case 4
Input:
texttotal_price 5 10 2 20 END
Expected Output:
text90
Actual Output:
text90
Case 5
Input:
textonly_ed_or_ing Reading completed running start END STOP
Expected Output:
textReading completed running
Actual Output:
textReading completed running
Case 6
Input:
textonly_ed_or_ing opened close stopped move ingested STOP
Expected Output:
textopened stopped ingested
Actual Output:
textopened stopped ingested
Case 7
Input:
textreverse_sum_palindrome 56 99 32 -1
Expected Output:
text56 32
Actual Output:
text56 32
Case 8
Input:
textreverse_sum_palindrome 12 19 23 34 87 -1
Expected Output:
text12 23 34
Actual Output:
text12 23 34
Case 9
Input:
textdouble_string hello world
Expected Output:
texthellohello worldworld
Actual Output:
texthellohello worldworld
Case 10
Input:
textdouble_string foo bar baz
Expected Output:
textfoofoo barbar bazbaz
Actual Output:
textfoofoo barbar bazbaz
Case 11
Input:
textodd_char Hello WORLD.
Expected Output:
textHlo WRD
Actual Output:
textHlo WRD
Case 12
Input:
textodd_char This is a sample sentence.
Expected Output:
textTi i a sml snec.
Actual Output:
textTi i a sml snec.
Case 13
Input:
textonly_even_squares 3 4 5 NAN
Expected Output:
text16
Actual Output:
text16
Case 14
Input:
textonly_even_squares 2 7 8 NAN
Expected Output:
text4 64
Actual Output:
text4 64
Case 15
Input:
textonly_odd_lines one two three four END
Expected Output:
textthree one
Actual Output:
textthree one
Case 16
Input:
textonly_odd_lines line1 line2 line3 END
Expected Output:
textline3 line1
Actual Output:
textline3 line1
Private Tests ( 16/16 )
Case 1
Input:
textsum_until_0 100 200 300 0
Expected Output:
text600
Actual Output:
text600
Case 2
Input:
textsum_until_0 -1 -1 2 0
Expected Output:
text0
Actual Output:
text0
Case 3
Input:
texttotal_price 1 1 2 1 3 1 END
Expected Output:
text6
Actual Output:
text6
Case 4
Input:
texttotal_price 3 4 4 5 END
Expected Output:
text32
Actual Output:
text32
Case 5
Input:
textonly_ed_or_ing tracked jumped book coding none STOP
Expected Output:
texttracked jumped coding
Actual Output:
texttracked jumped coding
Case 6
Input:
textonly_ed_or_ing raced coded running STOP
Expected Output:
textraced coded running
Actual Output:
textraced coded running
Case 7
Input:
textreverse_sum_palindrome 21 123 456 -1
Expected Output:
text21 123
Actual Output:
text21 123
Case 8
Input:
textreverse_sum_palindrome 44 55 77 -1
Expected Output:
text44
Actual Output:
text44
Case 9
Input:
textdouble_string test example
Expected Output:
texttesttest exampleexample
Actual Output:
texttesttest exampleexample
Case 10
Input:
textdouble_string python programming
Expected Output:
textpythonpython programmingprogramming
Actual Output:
textpythonpython programmingprogramming
Case 11
Input:
textodd_char Programming in python is fun.
Expected Output:
textPormig i pto i fn
Actual Output:
textPormig i pto i fn
Case 12
Input:
textodd_char Make it work.
Expected Output:
textMk i wr.
Actual Output:
textMk i wr.
Case 13
Input:
textonly_even_squares 9 10 12 NAN
Expected Output:
text100 144
Actual Output:
text100 144
Case 14
Input:
textonly_even_squares 15 22 27 NAN
Expected Output:
text484
Actual Output:
text484
Case 15
Input:
textonly_odd_lines A B C D END
Expected Output:
textC A
Actual Output:
textC A
Case 16
Input:
textonly_odd_lines one two three four five END
Expected Output:
textfive three one
Actual Output:
textfive three one
๐ป IITM Official Solution
python# Note this prefix code is to verify that you are not using any for loops in this exercise. This won't affect any other functionality of the program. with open(__file__) as f: content = f.read().split("# <eoi>")[2] if "for " in content: print("You should not use for loop or the word for anywhere in this exercise") # This is the first line of the exercise task = input() # <eoi> if task == "sum_until_0": total = 0 n = int(input()) while n != 0: # the terminal condition total += n # add n to the total n = int(input()) # take the next n form the input print(total) elif task == "total_price": total_price = 0 while True: # repeat forever since we are breaking inside line = input() if line == "END": # The terminal condition break quantity, price = line.split() # split uses space by default quantity, price = int(quantity), int(price) # convert to ints total_price += quantity * price # accumulate the total price print(total_price) elif task == "only_ed_or_ing": word = input() while word.lower() != "stop": if word.lower().endswith("ed") or word.lower()[-3:]=="ing": # both ways of doing it print(word) word = input() elif task == "reverse_sum_palindrome": num = int(input()) while num !=-1: rev_num = int(str(num)[::-1]) num_sum = num+rev_num if str(num_sum) == str(num_sum)[::-1]: print(num) num = int(input()) elif task == "double_string": line = input() while line != "": print(line*2) line = input() elif task == "odd_char": line = input() while line[-1] != ".": print(line[::2],end=" ") line = input() print(line[::2]) elif task == "only_even_squares": while True: line = input() if line == "NAN": break num = int(line) if num % 2 == 0: print(num * num) elif task == "only_odd_lines": result = "" line_no = 1 while True: line = input() if line == "END": break if line_no%2: result = line + "\n" + result line_no+=1 print(result)
๐ป My Submitted Code
python# Note this prefix code is to verify that you are not using any for loops in this exercise. This won't affect any other functionality of the program. with open(__file__) as f: content = f.read().split("# <eoi>")[2] if "for " in content: print("You should not use for loop or the word for anywhere in this exercise") # This is the first line of the exercise task = input() # <eoi> if task == "sum_until_0": total = 0 while True: n = int(input()) if n == 0: break total += n print(total) elif task == "total_price": total_price = 0 while True: line = input() if line == "END": break quantity, price = line.split() quantity, price = int(quantity),int(price) total_price += quantity * price print(total_price) elif task == "only_ed_or_ing": while True: word = input() if word.lower() == "stop": break w = word.lower() if w.endswith("ed") or w.endswith("ing"): print(word) elif task == "reverse_sum_palindrome": while True: n = int(input()) if n == -1: break rev = int(str(n)[::-1]) s = n + rev if str(s) == str(s)[::-1]: print(n) elif task == "double_string": while True: line = input() if line == "": break print(line * 2) elif task == "odd_char": result = [] while True: line = input() if line.endswith("."): result.append(line[::2]) break result.append(line[::2]) print(" ".join(result)) elif task == "only_even_squares": while True: line = input() if line == "NAN": break n = int(line) if n % 2 == 0: print(n*n) elif task == "only_odd_lines": lines = [] index = 1 while True: line = input() if line == "END": break if index % 2 == 1: lines.append(line) index += 1 lines.reverse() print("\n".join(lines))