0% found this document useful (0 votes)
22 views

CS101 Lab 04

This document provides an overview of functions with parameters and return values in Python. It introduces how to define functions that take parameters, like printing the double of a value or the sum of two numbers. The document also outlines 4 tasks for students to complete - using variables to add numbers in Hubo, checking if 3 values can form a triangle, modifying a sine value printing program to allow user input of steps, and printing a sine curve with hash symbols or blank spaces representing the sine values.

Uploaded by

chaosofdoomx
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views

CS101 Lab 04

This document provides an overview of functions with parameters and return values in Python. It introduces how to define functions that take parameters, like printing the double of a value or the sum of two numbers. The document also outlines 4 tasks for students to complete - using variables to add numbers in Hubo, checking if 3 values can form a triangle, modifying a sine value printing program to allow user input of steps, and printing a sine curve with hash symbols or blank spaces representing the sine values.

Uploaded by

chaosofdoomx
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

Welcome !

CS101 Introduction to Programming


Week 4
Math with Python
(Functions with parame te rs and re turn value s)
Functions with
parameters!!
Define functions
▪ Take parameters in your ne w functions.
def print_double(v):
print(v * 2)

>> print_double(2)
4

def print_sum(a, b):


print(a + b)

>> print_sum(3, 4)
7
Week 4
Today’s Tasks
Tasks for Today!
One task with Hubo to practice using variables
▪ Variable s

Four tasks without Hubo


▪ Be ing Euclid
▪ Be ing Edmund Gunte r
▪ Be ing Gunte r Re loade d (#2)
▪ Be ing Gunte r Re loade d (#3)

▪ 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

for i in range(41): for i in range(3):


x = float(i) / 40.0 * 2 print(i)
* pi
print( sin(x) )
Task 4 | Being Gunter Reloaded (#2)
▪ Modify the program to print out a sine
curve with ‘#’ characte rs like this:

HINT: Conve rt the re sult of sin function


to a re quire d numbe r of ‘#’ characte rs.

sin(π/2) = 1 ➡ “####...####” (80


“#”s)
sin(π) = 0 ➡ “##...##” (40 “#”s)
sin(3π/2) = -1 ➡ “” (No #)
Task 4 | Being Gunter Reloaded (#3)
▪ Modify the program to print out a sine
curve like this:

HINT: Conve rt the re sult of sin function


to a re quire d numbe r of “ ”s (blank
characte r)

>> print(" " * 5 + "*")


*
questions?

You might also like