vendredi 24 novembre 2017

How to connect random number generation to an item to print in python

I am using this code below to generate a number between 1 to 10 continuously until it generates either 9 or 10 before it stops

import random while True: rand = random.randint(1, 10) print(rand) if rand > 8: break

I want to display another item if it generates a number from 1 to 8 for example if it generates the number 3 I want it to print out a name in order from a stack data structure. If it generates the numbers 9 or 10 it would break.

An example of the stack data structure

  1. Mary
  2. Peter
  3. Bob
  4. John
  5. Kim

The stack code I'm using is

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)

However, I'm not sure how to proceed from here

Please help




Aucun commentaire:

Enregistrer un commentaire