Loading...
Reference View

GrPA 1 - While Loop - GRADED

GrPA 1 - While Loop - GRADED

Course: Jan 2026 - Python

Overview

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
View details
16/16 Passed
Submitted on Mar 04, 2026 at 11:18 PM IST
Private Tests
View details
16/16 Passed
Submitted on Mar 06, 2026 at 11:06 PM IST

Question

.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 n times, do once for each item, etc. use for loop 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.
# 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
  1. Accumulation - Accumulating a final result
    1. sum_until_0: Continuously read integers from standard input until you receive a zero. Print the sum of these integers.
    2. 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.
  2. Filtering - Selecting based on a criterion
    1. 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).
    2. 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.
  3. Mapping - Applying the same operation to different items
    1. double_string: Continuously read lines from standard input until an empty line is encountered. Print each line repeated twice.
    2. 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.
  4. Filter and Map - Applying an operation to selected items
    1. only_even_squares: Continuously read numbers from standard input until "NAN" is encountered. Print the square of each number only if it is even.
  5. Filter and Accumulate - Accumulating a result with selected items
    1. 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)
if 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() });

Test Cases

Public Tests ( 16/16 ) Private Tests ( 16/16 )
Case 1
Case 2
Case 3
Case 4
Case 5
Case 6
Case 7
Case 8
Case 9
Case 10
Case 11
Case 12
Case 13
Case 14
Case 15
Case 16
Input
sum_until_0 5 3 2 0
Expected Output
10
Actual Output
10

Full Solution Code

# 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:
        total += n
        n = int(input())
    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":
    word = input()
    while word.lower() != "stop":
        if word.lower().endswith("ed") or word.lower().endswith("ing"):
            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":
    result = []
    while True:
        line = input()
        result.append(line[::2])   # characters at odd positions (1-indexed: positions 1,3,5... → indices 0,2,4...)
        if line.endswith("."):
            break
    print(" ".join(result))

elif task == "only_even_squares":
    while True:
        val = input()
        if val == "NAN":
            break
        n = int(val)
        if n % 2 == 0:
            print(n * n)

elif task == "only_odd_lines":
    result = ""
    line_num = 0
    while True:
        line = input()
        if line == "END":
            break
        line_num += 1
        if line_num % 2 == 1:   # odd line (1-indexed)
            result = line + ("\n" + result if result else "")
    print(result)

Solution (Original Obfuscated)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# 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":
הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX



Document Outline
Table of Contents
System Normal // Awaiting Context

Intelligence Hub

Navigate the knowledge graph to generate context. The Hub adapts dynamically to surface backlinks, related notes, and metadata insights.