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 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.
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
  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)
python
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() });

Public Tests ( 16/16 )

Case 1

Input:
text
sum_until_0
5
3
2
0
Expected Output:
text
10
Actual Output:
text
10

Case 2

Input:
text
sum_until_0
10
-5
5
0

Case 3

Input:
text
total_price
2 50
1 100
3 30
END
Expected Output:
text
290
Actual Output:
text
290

Case 4

Input:
text
total_price
5 10
2 20
END
Expected Output:
text
90
Actual Output:
text
90

Case 5

Input:
text
only_ed_or_ing
Reading
completed
running
start
END
STOP
Expected Output:
text
Reading

completed

running
Actual Output:
text
Reading

completed

running

Case 6

Input:
text
only_ed_or_ing
opened
close
stopped
move
ingested
STOP
Expected Output:
text
opened

stopped

ingested
Actual Output:
text
opened

stopped

ingested

Case 7

Input:
text
reverse_sum_palindrome
56
99
32
-1
Expected Output:
text
56

32
Actual Output:
text
56

32

Case 8

Input:
text
reverse_sum_palindrome
12
19
23
34
87
-1
Expected Output:
text
12

23

34
Actual Output:
text
12

23

34

Case 9

Input:
text
double_string
hello
world
Expected Output:
text
hellohello

worldworld
Actual Output:
text
hellohello

worldworld

Case 10

Input:
text
double_string
foo
bar
baz
Expected Output:
text
foofoo

barbar

bazbaz
Actual Output:
text
foofoo

barbar

bazbaz

Case 11

Input:
text
odd_char
Hello
WORLD.
Expected Output:
text
Hlo WRD
Actual Output:
text
Hlo WRD

Case 12

Input:
text
odd_char
This
is
a
sample
sentence.
Expected Output:
text
Ti i a sml snec.
Actual Output:
text
Ti i a sml snec.

Case 13

Input:
text
only_even_squares
3
4
5
NAN
Expected Output:
text
16
Actual Output:
text
16

Case 14

Input:
text
only_even_squares
2
7
8
NAN
Expected Output:
text
4

64
Actual Output:
text
4

64

Case 15

Input:
text
only_odd_lines
one
two
three
four
END
Expected Output:
text
three

one
Actual Output:
text
three

one

Case 16

Input:
text
only_odd_lines
line1
line2
line3
END
Expected Output:
text
line3

line1
Actual Output:
text
line3

line1

Private Tests ( 16/16 )

Case 1

Input:
text
sum_until_0
100
200
300
0
Expected Output:
text
600
Actual Output:
text
600

Case 2

Input:
text
sum_until_0
-1
-1
2
0
Expected Output:
text
0
Actual Output:
text
0

Case 3

Input:
text
total_price
1 1
2 1
3 1
END
Expected Output:
text
6
Actual Output:
text
6

Case 4

Input:
text
total_price
3 4
4 5
END
Expected Output:
text
32
Actual Output:
text
32

Case 5

Input:
text
only_ed_or_ing
tracked
jumped
book
coding
none
STOP
Expected Output:
text
tracked

jumped

coding
Actual Output:
text
tracked

jumped

coding

Case 6

Input:
text
only_ed_or_ing
raced
coded
running
STOP
Expected Output:
text
raced

coded

running
Actual Output:
text
raced

coded

running

Case 7

Input:
text
reverse_sum_palindrome
21
123
456
-1
Expected Output:
text
21

123
Actual Output:
text
21

123

Case 8

Input:
text
reverse_sum_palindrome
44
55
77
-1
Expected Output:
text
44
Actual Output:
text
44

Case 9

Input:
text
double_string
test
example
Expected Output:
text
testtest

exampleexample
Actual Output:
text
testtest

exampleexample

Case 10

Input:
text
double_string
python
programming
Expected Output:
text
pythonpython

programmingprogramming
Actual Output:
text
pythonpython

programmingprogramming

Case 11

Input:
text
odd_char
Programming
in
python
is
fun.
Expected Output:
text
Pormig i pto i fn
Actual Output:
text
Pormig i pto i fn

Case 12

Input:
text
odd_char
Make
it
work.
Expected Output:
text
Mk i wr.
Actual Output:
text
Mk i wr.

Case 13

Input:
text
only_even_squares
9
10
12
NAN
Expected Output:
text
100

144
Actual Output:
text
100

144

Case 14

Input:
text
only_even_squares
15
22
27
NAN
Expected Output:
text
484
Actual Output:
text
484

Case 15

Input:
text
only_odd_lines
A
B
C
D
END
Expected Output:
text
C

A
Actual Output:
text
C

A

Case 16

Input:
text
only_odd_lines
one
two
three
four
five
END
Expected Output:
text
five

three

one
Actual Output:
text
five

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))

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.