Python Forum
Pizza Ordering Program
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Pizza Ordering Program
#3
Some other observations:

- your prices (pricem) and printout of prices (functions ptopping1, ptopping2) are not related. This means that any changes in pricing (pricem) is not reflected in printout. This gonna be significant problem if you update prices or add/remove items etc. You should take data for 'pretty-print' from your pricelist. Then all price updates and changes are reflected in pricing printouts as well.

- I advise to print explicitly. Define functions that return something and print result of the function. Functions that print something but does not have return statement actually return None. It may cause problems. Furthermore, it is good to have as much as possible of control of your code flow. You call a function (without parameters and with nondescriptive name) and by reading the code you have no idea what will happen.

I had little spare time when waiting for my child and I refactored beginning of your program with top-down style. There is very little of code and long docstrings. My experience is - if you have any code what is longer than couple of rows then you should document it for yourself. If you are returning to it in couple of weeks and you look at your code it usually seems that somebody else has written it and you have no clue how and why this code is written in a such way.

There is code with my thought process:

def main():
    pass


main()
Then I wrote welcome message part:

def main():
    print(centered_with_padding("Welcome to Freddie's Pizza"))

def centered_with_padding(text):
    pass
I put some thought into that welcome message. I want it to be understandable that it will be printed out (thus print). I want it to be more or less self-explanatory what format is applied (thus name centered_with_padding), I want to show what is the printed message (thus it takes message as argument). Then I maybe overthinking but decide to be defensive. Maybe at some point widht (65) or padding character (=) are deemed 'wrong' by 'art-director' :-). Therefore I use keyword arguments with default settings with ability to change them if need arises.

Considering all this I came up with oneliner function with long docstring:

def main():
    print(centered_with_padding("Welcome to Freddie's Pizza"))     # should be self-explanatory what will happen
     


def centered_with_padding(text, *, width=65, padding='='):
    """
    Return formatted text as string.

    Formatting: split text into words; every word on separate row and centered with padding.
    Width of row and padding character are provided with keyword arguments.

    :param text: text to be formatted
    :type text: str
    :param width: width of the row in return string
    :type width: int
    :param padding: char to be used as row padding in return string
    :type padding: str
    :return: text with formatting applied
    :rtype: str

    """
    return '\n'.join([f'{word.capitalize():{padding}^{width}}' for word in text.split()])

main()
I can run my file and see that so far my program behaves as expected. I can play with arguments to find optimal width and padding character.

Now I think about message about pricing availability. As this message is too long I decided to put this into function and gave hint about the content with descriptive name. So the code looks like this now:

def main():
    print(centered_with_padding("Welcome to Freddie's Pizza"))
    print(pricing_availability_notification()) 


def centered_with_padding(text, *, width=65, padding='='):
    """
    Return formatted text as string.

    Formatting: split text into words; every word on separate row and centered with padding.
    Width of row and padding character are provided with keyword arguments.

    :param text: text to be formatted
    :type text: str
    :param width: width of the row in return string
    :type width: int
    :param padding: char to be used as row padding in return string
    :type padding: str
    :return: text with formatting applied
    :rtype: str

    """
    return '\n'.join([f'{word.capitalize():{padding}^{width}}' for word in text.split()])

    
def pricing_availability_notification():
    """
    Return string with  pricing availability notification.

    Formatting: every statement in statements list on new row.

    :return: notifications listed in statements
    :rtype: str
    """

    statements = [
                  'At anytime when ordering, type "Pricing" (without quotes) to ',
                  'show pricing of ingredients and the total cost of your pizza so far.']
                 ]
    
    return '\n'.join(statements)

main()


At this point my time run out :-)
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply


Messages In This Thread
Pizza Ordering Program - by Trinx - Jan-16-2019, 11:14 PM
RE: Pizza Ordering Program - by perfringo - Jan-17-2019, 08:00 AM
RE: Pizza Ordering Program - by perfringo - Jan-18-2019, 08:10 AM
RE: Pizza Ordering Program - by Trinx - Jan-22-2019, 03:25 PM
RE: Pizza Ordering Program - by perfringo - Jan-22-2019, 03:43 PM
RE: Pizza Ordering Program - by Trinx - Jan-23-2019, 04:25 PM

Forum Jump:

User Panel Messages

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