CS101 Lab 04
CS101 Lab 04
>> print_double(2)
4
>> print_sum(3, 4)
7
Week 4
Today’s Tasks
Tasks for Today!
One task with Hubo to practice using variables
▪ Variable s
▪ Whe n you have comple te d all the tasks, le t a TA mark you off
Task 1 | Variables - Addition
▪ Teach Hubo how to add two numbers
○ The re are two numbe rs in base 10 on the row 1 and 2
○ Add two numbe rs and put the re sult on the row 1
NOTE: Your program must work for add1.wld, add2.wld, and add34.wld
HINT: Use variable s in orde r to re me mbe r two numbe rs
Before After
Task 2 | Being Euclid
▪ Given three numbers a, b, and c, it is possible to form a
triangle whose side s have le ngth a, b, and c if and only if
the triangle ine quality holds.
▪ That is, e ve ry side must be shorte r than the sum of the
othe r two side s.
Task 2 | Being Euclid (continued)
▪ Define a function ‘is_triangle()’ which
○ take s thre e float value s (thre e parame te rs)
○ returns True or False (Do NOT print anything in the function)
de pe nding on whe the r thre e value s can form a triangle
▪ Write a program which
○ asks the use r to e nte r thre e float value s
○ e valuate s input numbe rs using function ‘is_triangle ()’
○ outputs YES or NO de pe nding on the re sult of ‘is_triangle ()’ function
HINT: You must call the function ‘input’ and use ‘print’ outside of the function.
▪ Sample input and output
Side a: 2.3
Side b: 4.3
Side c: 5.6
YES
Task 3 | Being Edmund Gunter
▪ Following program prints out the sine value s of:
0 , (1/40 )*2π, (2/40 )*2π, …, 2π
▪ Modify the program to ask user the number of steps to print out value s
be twe e n 0 and 2π (inste ad of 41 fixe d ste ps)
import math
NOTE: Why do you think range(41) is used to
sin = math.sin print out 0, (1/40)*2π, (2/40)*2π, …, 2π ?
pi = math.pi
HINT: try the code below