Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › More Help with Python
New Posts  All Forums:Forum Nav:

More Help with Python

post #1 of 2
Thread Starter 
I'm still working with Queues, but I can't for the life of me figure out what I'm doing wrong. I've done a bunch of trial and error stuff, but maybe I can paste some code and see if someone can help me:

Code:
# Room class implementation
class Room:
    def __init__(self, building, roomNo):
        self.building = building
        self.roomNo = roomNo

    def getBuilding(self):
        return self.building

    def getRoomNo(self):
        return self.roomNo

    def setBuilding(self,building):
        self.building = building

    def setRoomNo(self,roomNo):
        self.roomNo = roomNo

    def __str__(self):
        return "Building: " + str(self.building) + "\
"   \\
               + "Room: " + str(self.roomNo) + "\
"
Code:
from Room import *

# Classroom class implementation
class Classroom(Room):
    def __init__(self, building, roomNo, capacity, isSmartRoom):
        Room.__init__(self, building, roomNo)
        self.capacity = capacity
        self.isSmartRoom = isSmartRoom

    def getCapacity(self):
        return self.capacity

    def getIsSmartRoom(self):
        return self.isSmartRoom

    def setCapacity(self,capacity):
        self.capacity = capacity

    def setIsSmartRoom(self,isSmartRoom):
        self.isSmartRoom = isSmartRoom

def makeRoom():
    print()
    print("The user should provide the values for the new object.")
    building = input("Name of building: ")
    roomNo = input("Number of room: ")
    capacity = eval(input("Capacity that it holds: "))
    isSmartRoom = eval(input("Is this a smartroom(True/False): "))
    print()
    return Classroom(building, roomNo, capacity, isSmartRoom)
Code:
class Queue:
    def __init__(self):
        self.items = []

    def isEmpty(self):
        return self.items == []

    def enqueue(self, item):
        self.items.append(item)

    def dequeue(self):
        return self.items.remove(item)

    def size(self):
        return len(self.items)
    
    def peek(self):
        return self.items[-1]
Code:
## Programming Project #3
## Programmer's Name: ########

from Queue import *
from Classroom import *

## QueueTester() tests a queue implementation.
def QueueTester():
    ## create an empty queue
    RoomQueue = Queue()
    question = input('Would you like to perform a stack operation? (yes/no) ')

    while question == ('yes'):

        question2 = input('Which operation would you like to perform? (test, peek, enqueue, or dequeue?) ')

        if question2 == ('test'):
            if RoomQueue.isEmpty():
                print('The queue is empty.')
            else:
                print('The queue has items.')
            
        elif question2 == ('enqueue'):
            m = makeRoom()
            RoomQueue.enqueue(m)

        elif question2 == ('peek'):
            RoomQueue.peek()
            print (m)
        
        elif question2 == ('dequeue'):
            RoomQueue.dequeue()
            print (m)
        

        print ('The size of the queue is:', RoomQueue.size())

        question = input('Would you like to perform a queue operation? (yes/no)
Now I've narrowed it down to being a problem within my class 'Queue'. When running QueueTester() in the Python shell, enqueue, and test work perfectly fine. Peek and dequeue output, but are where I believe the problem lies.

The goal in the program is to reverse the queue so that when you dequeue, it removes the item in front. Also, peek is supposed to look at the first item in the queue. So, I believe my problem is obviously in the peek() and dequeue() definitions. The only problem is I can't for the life of me figure out how to fix it.

If I need to give more info, please ask.
 
OverCoaster
(14 items)
 
 
CPUMotherboardGraphicsRAM
i7 7700K [4.5GHz] ASUS Z270i STRIX Titan X Pascal + Hybrid Corsair Vengeance LPX 3K [16GB] 
Hard DriveHard DriveCoolingOS
Kingston HyperX Savage [480GB] Mushkin Enhanced Reactor [1TB] Corsair H75 + F120MP Windows 10 Professional [x64] 
MonitorMonitorKeyboardPower
Acer Predator X34 Dell P2012Ht Razer Blackwidow X [Tournament Ed.] Corsair SF600 [600w] 
CaseMouseMouse PadAudio
NCASE M1 [v5] G403 Wireless QcK+ [Vault 111 Ed.] Schiit Bifrost Uber + Schiit Asgard 2 + TH-X00... 
CPUMotherboardGraphicsRAM
i5 4670K ASRock Z97E-ITX/ac ASUS GTX 1070 FE Kingston HyperX Fury DDR3 1866 [White Ed.] 
Hard DriveCoolingOSMonitor
Samsung Evo 840 [250GB] Phanteks PH-TC14PE  Win 10 Pro [x64] Dell S2716DGR [144hz] 
KeyboardPowerCaseMouse
KC60 [Miami Ed.] EVGA 550 G2 Phanteks Enthoo Evolv ITX [White] Logitech G403 
Mouse PadAudio
SS QcK Heavy AudioQuest Dragonfly Black + Sennhesier HD650 
  hide details  
Reply
 
OverCoaster
(14 items)
 
 
CPUMotherboardGraphicsRAM
i7 7700K [4.5GHz] ASUS Z270i STRIX Titan X Pascal + Hybrid Corsair Vengeance LPX 3K [16GB] 
Hard DriveHard DriveCoolingOS
Kingston HyperX Savage [480GB] Mushkin Enhanced Reactor [1TB] Corsair H75 + F120MP Windows 10 Professional [x64] 
MonitorMonitorKeyboardPower
Acer Predator X34 Dell P2012Ht Razer Blackwidow X [Tournament Ed.] Corsair SF600 [600w] 
CaseMouseMouse PadAudio
NCASE M1 [v5] G403 Wireless QcK+ [Vault 111 Ed.] Schiit Bifrost Uber + Schiit Asgard 2 + TH-X00... 
CPUMotherboardGraphicsRAM
i5 4670K ASRock Z97E-ITX/ac ASUS GTX 1070 FE Kingston HyperX Fury DDR3 1866 [White Ed.] 
Hard DriveCoolingOSMonitor
Samsung Evo 840 [250GB] Phanteks PH-TC14PE  Win 10 Pro [x64] Dell S2716DGR [144hz] 
KeyboardPowerCaseMouse
KC60 [Miami Ed.] EVGA 550 G2 Phanteks Enthoo Evolv ITX [White] Logitech G403 
Mouse PadAudio
SS QcK Heavy AudioQuest Dragonfly Black + Sennhesier HD650 
  hide details  
Reply
post #2 of 2
Thread Starter 
Well I believe I figured out the answer:

Code:
## Programming Project #3
## Programmer's Name: #####

from Queue import *
from Classroom import *

## QueueTester() tests a queue implementation.
def QueueTester():
    ## create an empty queue
    RoomQueue = Queue()
    question = input('Would you like to perform a queue operation? (yes/no) ')

    while question == ('yes'):

        question2 = input('Which operation would you like to perform? (test, peek, enqueue, or dequeue?) ')

        if question2 == ('test'):                  
            if RoomQueue.isEmpty():                 
                print('The queue is empty.')            
            else:
                print('The queue has items.')
            
        elif question2 == ('enqueue'):             
            m = makeRoom()                      # assigning variable m to the makeRoom function
            RoomQueue.enqueue(m)                # adds user-made room to queue

        elif question2 == ('peek'):
            print (RoomQueue.peek())            # prints the first item in the queue
            
        
        elif question2 == ('dequeue'):
            print (RoomQueue.dequeue())         # prints what is being dequeued
            
        

        print ('The size of the queue is:', RoomQueue.size())

        question = input('Would you like to perform a queue operation? (yes/no) ')
Code:
class Queue:
    def __init__(self):
        self.items = []

    def isEmpty(self):
        return self.items == []     # determines whether or not the queue is empty, and if not, returns the size

    def enqueue(self, item):
        self.items.append(item)     # adds item to end of list/queue so that item is actually at the front

    def dequeue(self):
        return self.items.pop(0)    # removes the first item in the queue so that it maintains its FIFO functionality

    def size(self):
        return len(self.items)      # returns size of queue
    
    def peek(self):
        return (self.items[0])      # returns the first item in the queue, which is the first item added by the user
 
OverCoaster
(14 items)
 
 
CPUMotherboardGraphicsRAM
i7 7700K [4.5GHz] ASUS Z270i STRIX Titan X Pascal + Hybrid Corsair Vengeance LPX 3K [16GB] 
Hard DriveHard DriveCoolingOS
Kingston HyperX Savage [480GB] Mushkin Enhanced Reactor [1TB] Corsair H75 + F120MP Windows 10 Professional [x64] 
MonitorMonitorKeyboardPower
Acer Predator X34 Dell P2012Ht Razer Blackwidow X [Tournament Ed.] Corsair SF600 [600w] 
CaseMouseMouse PadAudio
NCASE M1 [v5] G403 Wireless QcK+ [Vault 111 Ed.] Schiit Bifrost Uber + Schiit Asgard 2 + TH-X00... 
CPUMotherboardGraphicsRAM
i5 4670K ASRock Z97E-ITX/ac ASUS GTX 1070 FE Kingston HyperX Fury DDR3 1866 [White Ed.] 
Hard DriveCoolingOSMonitor
Samsung Evo 840 [250GB] Phanteks PH-TC14PE  Win 10 Pro [x64] Dell S2716DGR [144hz] 
KeyboardPowerCaseMouse
KC60 [Miami Ed.] EVGA 550 G2 Phanteks Enthoo Evolv ITX [White] Logitech G403 
Mouse PadAudio
SS QcK Heavy AudioQuest Dragonfly Black + Sennhesier HD650 
  hide details  
Reply
 
OverCoaster
(14 items)
 
 
CPUMotherboardGraphicsRAM
i7 7700K [4.5GHz] ASUS Z270i STRIX Titan X Pascal + Hybrid Corsair Vengeance LPX 3K [16GB] 
Hard DriveHard DriveCoolingOS
Kingston HyperX Savage [480GB] Mushkin Enhanced Reactor [1TB] Corsair H75 + F120MP Windows 10 Professional [x64] 
MonitorMonitorKeyboardPower
Acer Predator X34 Dell P2012Ht Razer Blackwidow X [Tournament Ed.] Corsair SF600 [600w] 
CaseMouseMouse PadAudio
NCASE M1 [v5] G403 Wireless QcK+ [Vault 111 Ed.] Schiit Bifrost Uber + Schiit Asgard 2 + TH-X00... 
CPUMotherboardGraphicsRAM
i5 4670K ASRock Z97E-ITX/ac ASUS GTX 1070 FE Kingston HyperX Fury DDR3 1866 [White Ed.] 
Hard DriveCoolingOSMonitor
Samsung Evo 840 [250GB] Phanteks PH-TC14PE  Win 10 Pro [x64] Dell S2716DGR [144hz] 
KeyboardPowerCaseMouse
KC60 [Miami Ed.] EVGA 550 G2 Phanteks Enthoo Evolv ITX [White] Logitech G403 
Mouse PadAudio
SS QcK Heavy AudioQuest Dragonfly Black + Sennhesier HD650 
  hide details  
Reply
New Posts  All Forums:Forum Nav:
  Return Home
  Back to Forum: Coding and Programming
Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › More Help with Python