Python Forum
Stack problem, repeating results that i dont want
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Stack problem, repeating results that i dont want
#1


class FunnyJukeBox:
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)

class PlaySong:
def __init__(self):
self.container =

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

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

# return first 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)


s = FunnyJukeBox()
s.push('Just the way you are')
s.push('I believe I can Fly')
s.push('Faded')
s.push('Firework')
s.push('One Call away')

p = PlaySong()

import random
songnum = [1,2,3,4,5,6,7,8,9,10]
counter = 0
Song = s.peek()

while counter <=5:
counter = counter + 1
songsnum = random.choice(songnum)

if songsnum <= 8:
print("Random Number", songsnum)
p.push(Song)
s.pop()
if counter == 5:
print("Have a nice day!", "\n")
break
else:
print("Random Number", songsnum)
print("Have a nice day!", "\n")
break

if p.size() == 0:
print("")
elif p.size() != 0:
print("The following song(s) we have played are:")
newCounter = p.size()
if songsnum <= 8:
while newCounter <= 5:
newCounter = newCounter - 1
print(p.peek())
p.pop()
if newCounter == 0:
break
else:
while newCounter <= 5:
newCounter = newCounter - 1
print(p.peek())
p.pop()
if newCounter == 0:
break


Result:

Random Number 5
Random Number 3
Random Number 7
Random Number 7
Random Number 5
Have a nice day!

The following song(s) we have played are:
One Call away <
One Call away <
One Call away < My main problem
One Call away <
One Call away <

I dont want to repeat the results of 'One Call away'. I only want to print it once, pop it, then peek the next item in the stack. I dont understand what is going on.
Reply
#2
I would love to reply to this thread, but without you editing your original post to include the BBcode, so that i can read your code.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Why is 2/3 not just .666 repeating? DocFro 4 704 Dec-12-2023, 09:09 AM
Last Post: buran
  I dont know why my function won't work? MehHz2526 3 1,198 Nov-28-2022, 09:32 PM
Last Post: deanhystad
  repeating a user_input astral_travel 17 2,293 Oct-26-2022, 04:15 PM
Last Post: astral_travel
  Something the code dont work AlexPython 13 2,262 Oct-17-2022, 08:34 PM
Last Post: AlexPython
  if else repeating Frankduc 12 2,510 Jul-14-2022, 12:40 PM
Last Post: Frankduc
  matching a repeating string Skaperen 2 1,256 Jun-23-2022, 10:34 PM
Last Post: Skaperen
  why I dont get any output from this code William369 2 1,130 Jun-23-2022, 09:18 PM
Last Post: William369
  Random Number Repeating Tzenesh 5 4,044 Jan-13-2021, 10:00 PM
Last Post: deanhystad
  factorial, repeating Aldiyar 4 2,811 Sep-01-2020, 05:22 PM
Last Post: DPaul
  Search Results Web results Printing the number of days in a given month and year afefDXCTN 1 2,239 Aug-21-2020, 12:20 PM
Last Post: DeaD_EyE

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020