CBSE Class 12 Computer Science Question 14 of 26

Data Structures in Python — Question 15

Back to all questions
15
Question

Question 15

Write Addscore(Game) and Delscore(Game) methods in Python to add new Score in the list of score in a game and remove a score from a list of score of a game considering these methods to act as PUSH and POP operation of data structure Stack.

Solution
def Addscore(Game):
    score = int(input("Enter the score to add: "))
    Game.append(score)

def Delscore(Game):
    if Game == []:
        print("Game's score list is empty")
    else: 
        print("Deleted score:", Game.pop())

Game = []

Addscore(Game)
Addscore(Game)
Addscore(Game)
print("Initial Game Stack:", Game)

Delscore(Game)
print("Game Stack after deletion:", Game)
Output
Enter the score to add: 10
Enter the score to add: 15
Enter the score to add: 20
Initial Game Stack: [10, 15, 20]
Deleted score: 20
Game Stack after deletion: [10, 15]
Answer

def
Addscore
(
Game
):
score
=
int
(
input
(
"Enter the score to add: "
))
Game
.
append
(
score
)
def
Delscore
(
Game
):
if
Game
==
[]:
print
(
"Game's score list is empty"
)
else
:
print
(
"Deleted score:"
,
Game
.
pop
())
Game
=
[]
Addscore
(
Game
)
Addscore
(
Game
)
Addscore
(
Game
)
print
(
"Initial Game Stack:"
,
Game
)
Delscore
(
Game
)
print
(
"Game Stack after deletion:"
,
Game
)
Output
Enter the score to add: 10
Enter the score to add: 15
Enter the score to add: 20
Initial Game Stack: [10, 15, 20]
Deleted score: 20
Game Stack after deletion: [10, 15]