Thursday, June 7, 2012

Zoo Fare.py

Went to check my Zoo Fare.py file, realized got some flaws in my way of coding

Old Code :

adult = int(raw_input("Please key in the amount of adults : "))
children = int(raw_input("Please key in the amount of children : "))
eldery = int(raw_input("Please key in the amount of senior citizens : "))
location = raw_input("Choose Zoo or Zoo and Night Safari (ZOO/ZNS): ")
upgrade = raw_input("Upgrade to Zoo-per Saver Pass (Y/N): ")


def TotalCost(adult_ticket, child_ticket, eldery_ticket) :
    totalfare = (adult * adult_ticket) + (children * child_ticket) + (eldery * eldery_ticket)

    if (upgrade == "y" or upgrade == "yes") :
        totalprice = (adult * 7) + (children * 4) + (eldery * 7) + totalfare
        print "The total cost would be $" + str(totalprice)
        print "Would you like some fries with that?"


    elif (upgrade == "n" or upgrade == "no") :
        print "The total cost would be $" + str(totalfare)
        print "Would you like some fries with that?"


if (location == "zoo") :
        TotalCost(20,13,10)


elif (location == "zns") :
        TotalCost(42,28,26)


New Code :

adult = raw_input("Please key in the amount of adults : ")
children = raw_input("Please key in the amount of children : ")
eldery = raw_input("Please key in the amount of senior citizens : ")
location = raw_input("Choose Zoo or Zoo and Night Safari (ZOO/ZNS): ")
upgrade = raw_input("Upgrade to Zoo-per Saver Pass (Y/N): ")


try:
    adult = int(adult)
    if adult < 0:
        print "Please enter value equal or more than 0 for adults"
        exit()


    children = int(children)
    if children < 0:
        print "Please enter value equal or more than 0 for children"
        exit()


    eldery = int(eldery)
    if eldery < 0:
        print "Please enter value equal or more than 0 for eldery"
        exit()


except ValueError:
    print "Please key in the amount of people in numbers"

    exit()
upgrade = upgrade.lower()
location = location.lower()


def TotalCost(adult_ticket, child_ticket, eldery_ticket) :
    totalfare = (adult * adult_ticket) + (children * child_ticket) + (eldery * eldery_ticket)

    if (upgrade == "y" or upgrade == "yes") :
        totalprice = (adult * 7) + (children * 4) + (eldery * 7) + totalfare
        print "The total cost would be $" + `totalprice`       


    elif (upgrade == "n" or upgrade == "no") :
        print "The total cost would be $" + `totalfare`       


    else :
        print 'Please key in either Yes(Y) or No(N) for the Upgrade'


if (location == "zoo") :
        TotalCost(20,13,10)


elif (location == "zns") :
        TotalCost(42,28,26)


else :
        print 'Please key in either Zoo or Zns'


What changes has been made :
- Able to determine whether the number of people keyed in are integers, instead of strings or other weird variables
- Able to accept strings for condition upgrade == 'n' or upgrade == 'no' regardless of upper or lower case, example would be like 'nO', 'NO'.
- Display error message if the user keyed in wrong answer in any string statement.
- Found alternative to cast interger into string by typing `x` instead of the standard casting str(x)
- Fixed logical error of negative values input, resulting lesser money count or even negative results

Conclusion :
Although I'm able to detect errors & prevent my program from crashing due to unknown values being entered, my codes will become longer.
No choice but to learn this as teacher will expect our codes have no errors at all in future, just face it lol


Amount of code lines jumped from 21 to 50 lol

Thursday, 07th June 2012

Woke up around 1+ pm today
Slacked around by browsing 9gag, play counter strike & etc until 6 pm


Went to buy plain Double Cheese Burger meal, upsized for lunch, & also Froot Loops cornflakes for dinner.
The cereal had a Madagascar whistle, lion 1.
Think will give Miss Maguro that whistle ~.~




Wednesday, 06th June 2012 (Late post)

Slept at 6 am, woke at 11.30 am x.x
Only few hours of sleep & went for pizza lol

Ordered the cheesy 7 double bliss meal, kinda worth it for $26.90
Since got the cheesy pizza & also a hawaiian regular pan.
Also redeemed a personal pan hawaiian.


Muffin ate half of the cheesy pizza & gave the other half to Maguro, while I ate the regular hawaiian as brunch & personal hawaiian as dinner.

Watched Shanghai Knights with Muffin, Jackie Chan still funny lolz
Showed Muffin my old photos when I still looked nerd.
Also showed her how to play a cat with laser.


Quite a funny day o.o

Guess tomorrow will just slack, nothing to do lol
I'm not the type to do revision