Monday, September 1, 2014

Computer Science Circles(6:If ~ 6D: Debug)

I learnt 6:If~6D:Debug last lesson.

Example: If

pancakes=int(input())
if pancakes > 3:
   print("Yum!")
if pancakes <= 3:
   print("Still hungry!")

If pancakes are more than 3, it will print Yum!
If pancakes are less than or equal 3, it will print Still hungry!

Example: Debug

NumberPrice
1$0.20
10 (small box)$1.99
20 (medium box)$3.39
40 (large box)$6.19
timbitsLeft = int(input()) # step 1: get the input
totalCost = 0              # step 2: initialize the total cost

# step 3: buy as many large boxes as you can
if timbitsLeft >= 40:
   bigBoxes = int(timbitsLeft / 40)
   totalCost = totalCost + bigBoxes * 6.19    # update the total price
   timbitsLeft = timbitsLeft - 40 * bigBoxes  # calculate timbits still needed

if 40 > timbitsLeft >= 20:                # step 4, can we buy a medium box?   
    totalCost = totalCost + 3.39
    timbitsLeft = timbitsLeft - 20
if 20 > timbitsLeft >= 10:                # step 5, can we buy a small box?
    totalCost = totalCost + 1.99
    timbitsLeft = timbitsLeft - 10
if timbitsLeft < 10:
    totalCost = totalCost

totalCost = totalCost + timbitsLeft * 0.2 # step 6
print(totalCost)                         # step 7

This program will be exact price every time.

No comments:

Post a Comment