STACK AND RANDOM PROGRAM LAB RECORD QUESTION
STACK AND RANDOM PROGRAM LAB RECORD QUESTION
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