jeudi 22 novembre 2018

Python - How to print multiple random element/item in a stack?

does anyone know how to print multiple element/item in stack? As from what i know, to use choose range numbers from a list is using range but how do i use with a stack? I have already tried doing like a for loop in range (the first element, last element) but had some errors in trying the code out.

Heres my current working code:

import random

class Stack:
 def __init__(self):
     self.container = []  

 def isEmpty(self):
     return self.size() == 0   

 def push(self, item):
     self.container.append(item)  

 def peek(self) :
     if self.size()>0 :
         return self.container[-1]
     else :
         return None

 def pop(self):
     return self.container.pop()  

 def size(self):
     return len(self.container)

Gift = Stack()
GiftTemp = Stack()
GiftWin = Stack()
GiftResult = Stack()
Gift.push ("Smart Watch")
Gift.push ("Gadget Key Chain")
Gift.push ("Happy Birthday Photo Frame")
Gift.push ("Silver Heart Pendant")
Gift.push ("Nike Cap")
Gift.push ("Stuffed Bunny Toy")
Gift.push ("Wireless Headphones")
Gift.push ("iPhone XS")

print("~ Mystery Gift Vending Machine ~")

size = Gift.size()+1
sizeOfGiftTemp = GiftTemp.size()+1

#prints very element in the stack and put the elements in GiftTemp
for i in range(1, size):
GiftTemp.push(Gift.pop())
print(str(i) + " - " + GiftTemp.peek())

#transfer elements from giftTemp to gift
for i in range(1, sizeOfGiftTemp):
Gift.push(GiftTemp.pop())

print("")
print("You will get one of these gifts below.")




Aucun commentaire:

Enregistrer un commentaire