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.

Monday, August 25, 2014

Computer Science Circles (3:Comments and Quotes ~ 5:Input)

Today, I learnt:

3:Comments and Quotes
Python ignore program after # sign.
It usually use explain parts of the program for me or other people.
If you want to print " this sign, you put \ (backslash) before " or ' .

4:Types
The word is 'str', whole number is 'int', decimal number is 'float'.

5:Input
The input() function takes no srguments and always gives back a 'str'.

Wednesday, August 20, 2014

Computer Science Circles (0:Hello!~2:Function)

I record about computer programming. I learn from Computer Science Circles.
http://cscircles.cemc.uwaterloo.ca/0-introduction/

I had done excise are:
0:Hello!
1:Variable
1E: Error
2:Function

[print] is Python code to send messages to output.
   Example: print("Hello")
   ( ) This is print command to use what you want to print
   " " If there aren't " " mark, Hello is meant to be command

Example:Variable
x=5             (x=5)
x=x+x         (x=5+5=10)
x=x-5          (x=10-5=5)
So, x=5 after execute these command

Error
run-time error happens when Python understands what you are saying, but runs into trouble when following your instructions. A syntax error happens when Python cannot understand what are you saying.
Example: Run-time error
print(1/0)
Example: Syntax error
print(Hello) 

Function
Example:
print(max(1,2,3))
This command will print 3
+plus - minus *times /divide

Computer Science Circle (2E:Extra Practice)

I learnt 2E: Extra Practice today. It extra excise in 2:Function.
Sorting Scramble was really difficult question. I took almost class time for this question.
I wanted x=smallest, y=next smallest and z=largest number

tmp=max(x,y)
x=min(x,y)
y=tmp
tmp=max(y,z)
y=min(y,z)
z=tmp
tmp=max(x,y)
x=min(x,y)
y=tmp

After execute these command, x=smallest, y=next smallest and z=largest number every time.