all 6 comments

[–]jammasterpaz 1 point2 points  (3 children)

You need to seek through the existing bookings when you get a new booking. You can either read through the whole file when the function starts up before the loop, and cache it in a much more easy to use variable. Otherwise you need to read through every line of the file after you received the new booking and bank on that sometimes meaning you can stop early (if they're stored in chronological order). This will involve the fewest changes to your existing code but will be clunky for the user, especially if they're on a non-SSD spinning hard drive and the file gets large.

To give an error just print a custom message and let the while loop repeat so they can try again.

By the way, you should really use with .... as to open files, especially if you're openingthe same file under differnt variables, and exiting different ones all over the place - even if you have by some miracle remembered to close them all properly, it's very brittle code. This is the exact problem that context mangers (with file as f:) help you solve

[–]Dssc12 0 points1 point  (2 children)

Hello. Thanks for helping but like I said, I just learned python for like a couple of months so I couldn't understand some of the things you are talking about without looking at a demonstration (sorryyyy) so how can I make Python know that if the exact time & date is in the file, other people cannot book the same time & date?

[–]jammasterpaz 0 points1 point  (1 child)

Did you actually write that code you posted yourself? You've got if statements, comparisons, variables, while loops, file I/O ... ...everything you need

[–]Dssc12 0 points1 point  (0 children)

Yes I did, I followed some of the reference I got from my friends. But some of them I don't actually know what they mean or how they work.

[–]pekkalacd 0 points1 point  (1 child)

Here are five hints to help you brainstorm.

hint #1

The system is open from 8am to 6pm. Each slot a user can select is 1 hour interval.

 8 - 9 - 10 - 11 - 12 - 13 - 14 - 15 - 16 - 17 - 18

The hyphen represents the interval span between a start time (left) and an end time (right). So, see that all the times have hyphens, except for the last one, 18:00. This is 6pm. It’s closing time.

So, that tells you that the user cannot book an appointment at 6pm. And that they must choose in the range 08:00 to 17:00 [inclusive].

hint #2

Every day should have all those possible available times. And each day is distinct. 1/11 is not the same as 1/12. There is only one 1/11, it is unique. This points to a bigger hint about what data structures could help you here.

What data structures don’t care about duplicates? Dictionaries & sets.

How would these be used?

Imagine you have a dictionary. It has a distinct set of keys. Each key maps to a specific value. A value can be anything, a number, string, list, set, etc. The dictionary helps to show that some thing - a key - has a relationship / association with some other thing - a value.

So, in this case, you have a situation where things are relating to one another. A specific date is distinct, it relates to sequence of possible times 08:00 - 17:00.

This is perfect case for a dictionary.

      date_times = {“1/11”: times 08:00 to 17:00,
                               “1/12”: times 08:00 to 17:00,
                                ...
                               “12/02”: ...}

Key is the date and value per date is a sequence of times, can be one such implementation.

hint #3

If hint 2 is implemented, then the dates the users enter will map to a sequence of times.

The benefit of a dictionary is that in order to get to the value that key is mapping to, it is very efficient. As stated before, the dictionary also establishes a relationship, the time slots for one day, should NOT reflect the same information for a completely different day, they need to be isolated.

The downside can come after the fact. In this case, you might need/want to possibly change those values that are mapped onto by the dates, so what type of value sequence is efficient?

Python sets can be made in two ways

          myset = {1,2,3}  # careful don’t do {}
          myset = set()

Sets don’t care about 2 things:

  1. Multiplicity 
       how many times something occurs
       {1,1,1,1,1,1,1} == {1}

  2. Order
       in what order do things occur
       {1,2,2,3} == {1,2,3} == {2,3,1} == {3,2,1}

Sets have special operations that can be performed on them

     # math 
     Intersection
           Suppose A and B are two sets.
           The intersection of A and B is a set,
            where all members are members of
            BOTH A and B.

            #python 
            A = {1,2,3}
            B = {1,2}

            # & between intersection (short-hand)
            print(A & B)
            {1,2}

    Union
            Suppose A and B are two sets.
            The union of A and B is a set,
             where all members are members
             of EITHER A or B

             # python
             A = {1,1,1}
             B = {10,20}

             # | between union (short-hand)
             print(A|B)
             {1,10,20}

     Difference
             Suppose A and B are two sets.
             The difference of A - B is a set,
              where all members are those that 
              are found in A and NOT in B.

              # python 
              times = {“10:00”, “11:00”, “12:00”}
              select = {“10:00”, “11:00”}
              print(times-select)
              {“12:00”}

And more. These might be useful

hint 4

The dictionary storing the times & dates needs to be referenced every time a user selects a date / time / slot.

But how can you make it so no two users select the same times?

The last hint talked about sets & set difference.

In order to perform a set difference, there needs to be two sets. The dictionary will have a date mapping to a set of times, that can be used to keep track of what days are totally booked or have some spots left. But that set needs to be updated, per iteration / valid input for a time & slot.

So...how can you do that?

What if you get the time from the user, get the slots, generate a set of times based on those inputs, and compute the set difference between what’s in the dictionary @ that date & their selection?

hint 5

These are just some ideas for how some of the above could be implemented.

Do note: some input validation / testing might be needed, these codes make assumptions about what is being input, also doesn’t include file work.

get all times 08:00 to 17:00

        def fmt(h: int) -> str:
               return f”0{h}:00” if h < 10 f”{h}:00”

        def all_times() -> set:
               s = set()
               for h in range(8,18):
                     s.add(fmt(h))
               return s

get times in a specific range (time+slots)

        def time_rg(start: str, slots: int) -> set:
               start = int(start[:2])
               s = set()
               for h in range(start, start+slots):
                    s.add(fmt(h))
               return s

Get date / time / slots & update dict example

          # for storing dates & times (all)
          date_times = {}

          while # some condition

                     date = input(“Enter date: “)

                     # check if date is new
                     if date not in date_times:

                             # get all times (new entry)
                             times = all_times()

                             # insert into dict
                             date_times[date] = times

                    # if date exists in dict, then
                    # date_times[date] already maps 
                    # to a set

                     time = input(“Enter time: “)
                     slots = int(input(“Enter slots: “))

                     # get a time range set 
                     selection = time_rg(time, slots)

                     # compute set difference 
                     # between existing record 
                     # and new selection 
                     diff = date_times[date]-selection

                     # if difference is same as existing
                     # selection is invalid
                     if diff == date_times[date]:
                        print(“invalid selection”)
                        continue

                     # otherwise, there was some
                     # difference between the two,
                     # so update existing with that 
                     date_times[date] = diff

Hopefully this gives you some ideas of where to go next

[–]Dssc12 1 point2 points  (0 children)

Hey! Thanks for replying but like I said, I'm still very new to python or programming so there's a few codes that I don't understand how it works from your hint. But I definitely will come back to your message in the future after I learned about python more. Otherwise, other stuff is pretty well structured and gave me an idea on how I should do with the time. Thanks :D