0% found this document useful (0 votes)
2 views

STACK AND RANDOM PROGRAM LAB RECORD QUESTION

The document provides two Python programs: one for implementing a stack using a list, allowing users to push, pop, and display elements, and another for simulating a dice roll that generates random numbers between 1 and 6. The stack program includes user prompts for actions and handles underflow cases, while the dice program congratulates the user for rolling a six. Both programs demonstrate basic control flow and user interaction in Python.

Uploaded by

ammaarakheel
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

STACK AND RANDOM PROGRAM LAB RECORD QUESTION

The document provides two Python programs: one for implementing a stack using a list, allowing users to push, pop, and display elements, and another for simulating a dice roll that generates random numbers between 1 and 6. The stack program includes user prompts for actions and handles underflow cases, while the dice program congratulates the user for rolling a six. Both programs demonstrate basic control flow and user interaction in Python.

Uploaded by

ammaarakheel
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Write a program to implement a Stack using List

stack =[]
while True:
print("1.PUSH 2.POP 3.Display elements of stack")
choice=int(input("Enter your choice"))
if choice == 1:
a=int(input("Enter the element which you want to push"))
stack.append(a)
elif choice == 2:
if stack == []:
print("Stack is empty...Underflow case...")
else:
print("Deleted element is :", stack.pop())
elif choice == 3:
print("The elements in the stack =", stack)
else:
print("Wrong input...")
ch = input("Do you want to continue or not(y/n) ?")
if ch in 'nN':
break

Output
1.PUSH 2.POP 3.Display elements of stack
Enter your choice1
Enter the element which you want to push10
Do you want to continue or not(y/n) ?y
1.PUSH 2.POP 3.Display elements of stack
Enter your choice1
Enter the element which you want to push20
Do you want to continue or not(y/n) ?y
1.PUSH 2.POP 3.Display elements of stack
Enter your choice1
Enter the element which you want to push30
Do you want to continue or not(y/n) ?y
1.PUSH 2.POP 3.Display elements of stack
Enter your choice3
The elements in the stack = [10, 20, 30]
Do you want to continue or not(y/n) ?y
1.PUSH 2.POP 3.Display elements of stack
Enter your choice2
Deleted element is : 30
Do you want to continue or not(y/n) ?y
1.PUSH 2.POP 3.Display elements of stack
Enter your choice3
The elements in the stack = [10, 20]
Do you want to continue or not(y/n) ?n
Write a random number generator that generates random number between 1
and 6 (simulates a dice).

import random
print("Rolling the dice")
while True:
num =random.randint(1,6)
if num == 6:
print("You got it" , num, ".....Congratulations!!!")
elif num == 1:
print("Well tried....but you got", num)
else:
print("You got ", num)
ch = input("Roll again (y/n)? ")
if ch in "nN":
break

Output
Rolling the dice
You got 3
Roll again (y/n)? y
You got 5
Roll again (y/n)? y
You got 4
Roll again (y/n)? y
You got it 6 .....Congratulations!!!
Roll again (y/n)? y
You got 5
Roll again (y/n)? n

You might also like