Assessment submitted.
(https://swayam.gov.in) (https://swayam.gov.in/nc_details/NPTEL)
X
[email protected]
NPTEL (https://swayam.gov.in/explorer?ncCode=NPTEL) » The Joy of Computing using Python (course)
If already
registered, click
Thank you for taking the Week 7 :
to check your
payment status
Assignment 7.
Course Week 7 : Assignment 7
outline Your last recorded submission was on 2025-09-10, 22:32 IST Due date: 2025-09-10, 23:59 IST.
Refer to the code discussed in the "Joy of Computing using Python" lecture on implementing the
About NPTEL Snake and Ladder game.
()
Ankan and Dilrez are playing a classic game of Snake and Ladder –but this time, it’s implemented in
How does an Python! The game logic is based on the version you’ve seen in the lecture, where snakes pull you
NPTEL online down and ladders push you up. The game board ranges from position 1 to 100.
course work?
() The snake and ladder positions are stored in dictionaries. For instance:
snakes = {16: 6, 98: 78}
Week 1 () ladders = {1: 38, 80: 100}
Week 2 () Each player begins at position 0. In each turn, a player rolls a die (using random.randint(1, 6)) and
moves forward. If they land on the bottom of a ladder, they climb up; if they land on a snake’s mouth,
Week 3 () they slide down. The game alternates between Ankan and Dilrez using a turn variable, and ends when
any player reaches exactly 100.
week 4 ()
Two key functions, check_ladder(position) and check_snake(position), determine whether the player
Week 5 () hits a ladder or a snake after moving.
Week 6 () A function reached_end(position) is used to check if a player has reached position 100. A separate
function is also written to open and display an image of the board using the PIL library.
Week 7 ()
1) In the function play(), what is the significance of the variable turn? 1 point
Snakes and
Ladders - Not It tracks the number of dice rolls
on the Board It ensures alternating turns between the two players
(unit?
It ends the game after a fixed number of turns
unit=143&lesso
n=144) It stores the winning score
Snakes and
2) What happens in the code when a player rolls a number that moves them past position 1 point
Ladders - Not
100?
on the Board -
Part 01 (unit?
The game ends and they win
unit=143&lesso The player’s position resets to 0
Assessment submitted.
n=145)
X The player stays in the same position (doesn’t move)
Snakes and
The player is forced to roll again
Ladders - Not
on the Board - 3) What is the purpose of the following lines in the play() function? 1 point
Part 02 (unit?
unit=143&lesso
n=146)
Snakes and
Ladders - Not
on the Board -
Part 03 (unit?
unit=143&lesso
n=147)
To restart the game
Snakes and
To display final scores and exit if the player chooses not to continue
Ladders - Not
on the Board - To check if the player reached a snake
Part 04 (unit? To decide the winner
unit=143&lesso
n=148)
4) If Ankan is at position 47 and the snakes dictionary contains 47:26, what will happen 1 point
Snakes and immediately after his dice roll moves him to 47?
Ladders - Not
on the Board - He will stay at 47
Part 05 (unit? He will move to 100
unit=143&lesso
He will go to 26
n=149)
The game will end
Snakes and
Ladders - Not
5) What does the function show_board() attempt to do? 1 point
on the Board -
Part 06 (unit?
Display current scores
unit=143&lesso
n=150) Roll the dice for both players
Open and display the game board image
Spiral
Traversing - Initialize the snake and ladder dictionaries
Let's Animate
(unit? 6) Which of these functions is called after a player moves and before their position is 1 point
unit=143&lesso printed?
n=151)
show_board()
Spiral
Traversing - play()
Let's Animate - check_ladder() and check_snake()
Part 01 (unit?
unit=143&lesso
reached_end() only
n=152)
7) How many times is the line random.randint(1, 6) evaluated in one full round of both 1 point
Spiral
players' turns?
Traversing -
Let's Animate - Once
Part 02 (unit?
unit=143&lesso
Twice
n=153) Four times
Spiral Depends on the player positions
Traversing -
Let's Animate - 8) Which part of the code ensures that the game stops immediately when a player reaches 1 point
Part 03 (unit? exactly 100?
unit=143&lesso if pp1 > end:
Assessment submitted.
n=154)
X if pp1 == 100:
Spiral
if reached_end(pp1):
Traversing -
Let's Animate - if check_ladder(pp1):
Part 04 (unit?
9) Consider the following ladders dictionary (in the format bottom: top): ladders = {1: 38, 4: 1 point
unit=143&lesso
14, 9: 31, 21: 42, 28: 84, 36: 44, 51: 67, 71: 91, 80: 100}
n=155)
Spiral If a player is on 27, rolls a 1, and lands on 28, what will happen next?
Traversing -
Let's Animate - The player will stay on 28 because it's not a ladder base
Part 05 (unit?
The player climbs the ladder to 84
unit=143&lesso
n=156) The player must roll again because 28 is a special position
The code throws an error because 28 is not handled explicitly
Spiral
Traversing -
Let's Animate - 10) 1 point
Part 06 (unit?
unit=143&lesso
n=157)
Spiral
Traversing -
Let's Animate -
Part 07 (unit? Why is the condition turn % 2 == 0 used in this part of the code?
unit=143&lesso
n=158) To check if the game has reached the second round
To make sure player 2 only plays after player 1 wins
GPS - Track the
route (unit? To alternate turns between the two players using the value of turn
unit=143&lesso To skip the dice roll for even-numbered turns
n=159)
GPS - Track the
Dilrez, the ever-curious adventurer, had a habit of collecting strange and shiny things during her
route - Part 01
journeys.
(unit?
unit=143&lesso
n=160) One day, the village elder gifted her two magical containers — one called a Backpack and the other
a Pouch.
GPS - Track the
route - Part 02
The elder explained:
(unit?
unit=143&lesso
n=161)
“Dilrez, these are no ordinary containers.
GPS - Track the The Backpack holds items in a fixed order, and once packed, nothing can be changed.
route - Part 03
(unit?
The Pouch, on the other hand, only keeps unique items and doesn’t care about order.”
unit=143&lesso
n=162)
Dilrez was intrigued.
GPS - Track the
route - Part 04 Backpack = Tuple
(unit?
unit=143&lesso
Dilrez packed it like this:
n=163)
Week 7 backpack = ("map", "compass", "torch")
Feedback Form:
The Joy of He checked backpack[0] → 'map'.
Computing
using Python
Assessment
(unit?submitted. But when he tried:
X unit=143&lesso
n=164)
backpack[0] = "magic map"
Quiz: Week 7 :
Assignment 7 He got an error!
(assessment?
name=603) Tuples are ordered, immutable, and allow duplicates.
Week 7 :
Syntax: () (round brackets)
Programming
Assignment 1 Pouch = Set
(/noc25_cs103/
progassignment Dilrez filled his pouch:
?name=584)
Week 7 : pouch = {"coin", "potion", "coin"}
Programming
Assignment 2 He expected 3 items, but got:
(/noc25_cs103/
progassignment {'coin', 'potion'}
?name=585)
Week 7 : He added and removed items freely:
Programming
Assignment 3 pouch.add("gem")
(/noc25_cs103/
progassignment pouch.remove("coin")
?name=602)
Sets are unordered, mutable, and store unique items.
Week 8 ()
Syntax: {} (curly braces)
Text 11) What will be the output of the following code? 1 point
Transcripts ()
Download
Videos ()
Problem "map"
Solving "torch"
Session - July
"compass"
2025 ()
IndexError
12) What will happen when Dilrez runs this code? 1 point
Prints ("magic map", "compass", "torch")
Gives a TypeError
Prints original backpack
Gives a SyntaxError
13) What will be the output? 1 point
Assessment submitted.
X
Select all that apply:
The output will contain only unique elements
The output may appear in any order
It will raise a syntax error due to duplicate elements
The output will be a list with 2 elements
14) What will be the final contents of the pouch? 1 point
{'potion', 'gem'}
{'gem'}
['gem']
Error due to remove()
15) What is the type of x in the following code? 1 point
< class 'tuple' >
< class 'list' >
< class 'str' >
< class 'set' >
16) Which of the following statements is true? 1 point
Tuples can’t store duplicate items
Sets maintain the order of elements
Sets automatically remove duplicate items
Tuples can be changed after creation
17) What will this code output? 1 point
Assessment submitted.
X
33
22
32
23
18) Dilrez tries to update a tuple. Find and fix the error: 1 point
What's the issue?
Tuple index out of range
Syntax error in tuple
Tuples are immutable
Need to use append()
19) What will be the output of the following code? 1 point
4 2 ('map', 'torch')
4 2 ('torch', 'map')
2 2 ('map', 'torch')
4 4 ('map', 'torch', 'torch', 'map')
20) Dilrez does the following: 1 point
Which of the following is true based on this setup?
All three containers contain the same items and in the same order
Only the pouch automatically removes duplicates
Assessment submitted.
X backpack[1] and supply_list[1] give the same result
Sets can be indexed just like lists and tuples
Himmat was given a task: Find the factorial of a number.
He read that:
The factorial of a number n is written as n!
It means:
n! = n × (n-1) × (n-2) × ... × 1
So, for example:
4! = 4 × 3 × 2 × 1 = 24
But instead of doing it manually, Himmat decided to write a recursive
function — a function that calls itself.
21) Why does the code return 1 when n == 0? 1 point
Because 0 × anything is 0
To stop the recursion at some point
By mistake; it should return 0
It helps to calculate square of the number
22) What will be the return value of factorial(1)? 1 point
0
1
It will call itself infinitely
2
23) Identify the error in the following code: 1 point
It will return 6
No base case defined, will result in infinite recursion
n is not decremented
Syntax error
Assessment submitted. 24) What does the line n! = n × (n-1) × (n-2) × ... × 1 represent in the comprehension? 1 point
X
An infinite series
A summation of natural numbers
A recursive definition of factorial
A non-recursive mathematical shortcut
25) 1 point
How many recursive calls are made when he executes factorial(4)?
3
4
5
Infinite
26) Himmat accidentally writes: 1 point
What will happen if he calls factorial(0)?
Returns 1
Returns 0
Throws a recursion error
Returns -1
27) What would be a correct way to make Himmat’s function handle negative numbers 1 point
safely?
Add return -1 for all inputs
Add a base case if n < 0: return "Invalid input"
Remove recursion altogether
Multiply by -1 inside the function
28) If Himmat’s function is modified as: 1 point
Assessment submitted. What happens when factorial(3) is called?
X
Returns 6
Returns 0
Infinite recursion
Error due to syntax
29) Himmat runs this code to reverse a string: 1 point
What will be the output?
"a"
""
Error (index out of range)
"aa"
30) Himmat wants to reverse the word "race" using recursion. He writes a function that checks 1 point
whether the string is non-empty as the base case.
What will be the output of reverse_string("race")?
"race"
"ecar"
"rcae"
Error due to slicing empty string
You may submit any number of times before the due date. The final submission will be considered for
grading.
Submit Answers