#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2013
    Posts
    2
    Rep Power
    0

    Python Equivalent of an Array?


    EDIT: WE ARE USING PYTHON 2.7.5


    Dear Forum Users, I'm a university Student Studying Computer Games Design and I'm being taught Python in my first year.

    Myself and several other members are in a group together and we have been tasked to create a Project, however, we are stuck at a point in the code and we are unable to progress any further, and the Python web-page forums aren't simple to understand for us beginners.

    the purpose of this project is as following, we have to design a piece of software that will be run on a publicly shared computer, where people are able to request or provide a lift between campuses.

    We have 2 types of people that will use this system, those that are offering a lift, and those that are requesting a lift.

    The Lift givers/Providers, as we have called them, will use the machine to input data, such as the destination campus, the time, and how many seats they have available in their car.

    the lift receivers, will enter the same information, however, this information will be used to check if there is anyone going to the same destination and at the same time, and check if their is space available in their car, and then assign them a space/ remove from the total of free spaces from the Giver.


    THE ISSUE: me and my fellow group members, are unsure of how to take the information we have received from the Lift Providers, (destination, Time and Spare Seats) and store that into an array (or python equivalent) and store it until a later time. We would want to group each set of information separate from the last.

    We are then unsure of how to take the information we receive from the Lift Receivers and compare that to the information stored in any arrays (or python equivalent) to check whether or not the destination and time are the same, and then if there are any free spaces.


    Because we are new to python, we have a text based system, that will list a series of options to the user which will then determine if they are in need of/Offering a lift, The time they are Leaving and the number of seats Needed/Seats Available.





    Conclusion: We are unsure how to store data entered by the Lift Givers with potentially multiple sets of data needing to be stored at once, Then we are unsure of how to take the information entered by the Lift Receivers and compare that to any stored information to determine if they can be allocated a seat in a vehicle.

    Code:
    def PrimaryFn():
    
        print 'Are you Giving a Lift, or do you need to recieve a lift?';
        print '1. Giving a Lift';
        print '2. Need to recieve a lift';
         
        Lift = input('Enter 1 or 2:')
        if (Lift == 1):
            GivingFn();
        if (Lift == 2):
            ReceivingFn();
            
    
    def GivingFn():
        print 'You have selected to give a lift';
    
        print 'Which Campus would you like to go to?';
        print '1. Luton Campus';
        print '2. Bedford Campus';
    
        CampusSelector = input('Enter 1 or 2:')
    
        if (CampusSelector == 1):
            CampusGiving = 'Luton'
            
        if (CampusSelector == 2):
            CampusGiving = 'Bedford'
    
        print 'At What Time are you going to this Campus';
        print '1. AM'
        print '2. PM'
    
        TimeSelector = input('Enter 1 or 2:')
        
        if (TimeSelector == 1):
            AMorPMGiving = 'AM'
            
        if (TimeSelector == 2):
            AMorPMGiving = 'PM'
    
        print 'What time will you be leaving?'
    
        TimeGiving = input('Please enter the time to the nearest Quarter of an Hour:')
        
    
        print 'How many Seats do you have available in your car?'
    
        print '1. 1 Seat Available';
        print '2. 2 Seats Available';
        print '3. 3 Seats Available';
        print '4. 4 Seats Available';
        print '5. 5 Seats Available';
        print '6. 6 Seats Available';
    
        SeatAvailablitySelector = input('Enter 1-6:')
        
        if (SeatAvailablitySelector == 1):
            SeatsAvailableGiving = '1'
    
        if (SeatAvailablitySelector == 2):
            SeatsAvailableGiving = '2'
    
        if (SeatAvailablitySelector == 3):
            SeatsAvailableGiving = '3'
    
        if (SeatAvailablitySelector == 4):
            SeatsAvailableGiving = '4'
    
        if (SeatAvailablitySelector == 5):
            SeatsAvailableGiving = '5'
    
        if (SeatAvailablitySelector == 6):
            SeatsAvailableGiving = '6'
    
    
        
        print 'You have stated that your leaving for:', CampusGiving,'Campus'; 
        print 'At:', TimeGiving, AMorPMGiving;
        print 'You have:', SeatsAvailableGiving, 'Seats Available';
    
        print
        
    
    def ReceivingFn():
    
        print 'You have selected to recieve a lift';
        
        print 'Which Campus do you need to go to?';
        print '1. Luton Campus';
        print '2. Bedford Campus';
    
        CampusSelector = input('Enter 1 or 2:')
    
        if (CampusSelector == 1):
            CampusReceiving = 'Luton'
            
        if (CampusSelector == 2):
            CampusReceiving = 'Bedford'
    
        print 'At What Time do you need to go to this Campus';
        print '1. AM'
        print '2. PM'
    
        TimeSelector = input('Enter 1 or 2:')
        
        if (TimeSelector == 1):
            AMorPMReceiving = 'AM'
            
        if (TimeSelector == 2):
            AMorPMReceiving = 'PM'
    
        print 'What time do you need to leave?'
    
        TimeReceiving = input('Please enter the time to the nearest Quarter of an Hour:')
        
    
        print 'How Many seats do you require?'
        print '1. 1 Seat';
        print '2. 2 Seats';
        print '3. 3 Seats';
        print '4. 4 Seats';
        print '5. 5 Seats';
        print '6. 6 Seats';
    
        SeatRequiredSelector = input('Enter 1-6:')
        
        if (SeatRequiredSelector == 1):
            SeatsRequiredReceiving = '1'
    
        if (SeatRequiredSelector == 2):
            SeatsRequiredReceiving = '2'
    
        if (SeatRequiredSelector == 3):
            SeatsRequiredReceiving = '3'
    
        if (SeatRequiredSelector == 4):
            SeatsRequiredReceiving = '4'
    
        if (SeatRequiredSelector == 5):
            SeatsRequiredReceiving = '5'
    
        if (SeatRequiredSelector == 6):
            SeatsRequiredReceiving = '6'
    
    
        
        print 'You have stated that you want to go to:', CampusReceiving,'Campus'; 
        print 'At:', TimeReceiving, AMorPMReceiving;
        print 'You need:', SeatsRequiredReceiving, 'seats';
    
    PrimaryFn()

    I'm unsure if we should have a maximum limit of arrays (or python equivalent) But what I also want to happen, is that any that are made, are named after the person who is giving the lift, and once a lift receiver has entered their details into the program and had it checked for any free seats, if successful it should tell the receiver to meet ______(Lift Givers name here) at a fixed location (for example the University Car Park) at the arranged time.


    Thank you to any help you can provide, sorry for the long post.
  2. #2
  3. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,904
    Rep Power
    481
    Be clever about handling time.
    TimeGiving = input('Please enter the time to the nearest Quarter of an Hour:')
    Code:
    >>> input('quarter hour: ')
    quarter hour: 13:15
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
      File "<string>", line 1
        13:15
          ^
    SyntaxError: invalid syntax
    I recommend sys.stdin.readline() followed by parsing, or just leave the value as a string. Perhaps show an example in the prompt.


    Use a database. The program needs to retain information between separate runs. Even if you change the program to run continuously power/hardware failures can still stop it. Use pickle or shelve---or sqlite3.


    Python doesn't limit list sizes.


    If your program should email reminders to people you could assign a program to the batch queue that periodically reads the database and does it's work.
    [code]Code tags[/code] are essential for python code and Makefiles!
  4. #3
  5. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,904
    Rep Power
    481
    Oh, the list type is "equivalent of array".
    [code]Code tags[/code] are essential for python code and Makefiles!
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2013
    Posts
    2
    Rep Power
    0
    Thanks for the help, though I think you Mis-understood what I mean by 'Every Quarter of an hour' I meant in 15 minute intervals, 9.00, 9.15, 9.30, 9.45 etc

    Also, How would I go about setting each list as a new one, because I will need availability for more than one list at a time, and then how would I check those lists to match destinations and times? I assume if Statements, though I think I'd have to run some sort of loop to get the program to check multiple lists? I think I get the basic principle behind how I want to do this, I just, know nothing about Python, and the docs.python.org aren't too straight forward. :/
  8. #5
  9. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,904
    Rep Power
    481
    The person using your program needs to know that you expect a non-standard time format. That's why I recommended "show an example in the (time) prompt."



    What is the incentive for lift providers to use the system? Monetary exchange?


    Code like this might be suitable? Study the python documentation about list methods for searching. Better data types, compared to list, might be priority queue ordered by time, or a set.
    Code:
    riders = [] # a list of Rider objects
    liftrs = [] # list of Lifter objects
    
    class RideShareError(Exception):
    
        def __init__(self, reason = 'Not enough seats!'):
            super().__init__(reason)
    
    class VitalData:
    
        def __init__(self, contact_information, destination, time, *args, **kwargs):
            (self.contact_information, self.destination, self.time) = (
                contact_information, destination, time)
    
        def __eq__(self, other):
            return (
                (self.destination == other.destination) and
                (self.time == other.time)
                )
    
    class Lifter(VitalData):
    
        def __init__(self, contact_information, destination, time, seats):
            super().__init__(contact_information, destination, time,)
            self.seats = seats
    
        def __add__(self, seats): # handle cancellation
            self.seats += seats
    
        def __sub__(self, seats): # handle cancellation
            if self.seats < seats:
                raise RideShareError
            self.seats -= seats
    
    class Rider(VitalData):
    
        def getSeats(self, seats):
            'your good code here'
    [code]Code tags[/code] are essential for python code and Makefiles!

IMN logo majestic logo threadwatch logo seochat tools logo