Download presentation
Presentation is loading. Please wait.
Published byPierce Small Modified over 9 years ago
1
Week 4 : Function with parameters and return values
2
Today’s Topic Variables Interacting with a user’s input (function ‘raw_input’) Function with parameters and return values
3
Practices Teach Hubo How to Add Addition Being Mathematician being Euclid being Edmund Gunter (#1) being Gunter Reloaded (#2) being Gunter Reloaded (#3) Caution: Save your python files in C:/cs101/ and clear the directory when you complete the tasks.
4
Task | Variables – Addition Teach Hubo how to add two numbers There are two numbers in base 10 on the row 1 and 2 Add two numbers and put the result on the row 1 ※ Your program must work for add1.wld, add2.wld, and add34.wld ※ Hint : Use variables in order to remember two numbers add34.wld Before After add1.wld add2.wld
5
Task | being Euclid Given three numbers a, b, and c, it is possible to form a triangle whose sides have length a, b, and c if and only if the triangle inequality holds. That is, every side must be shorter than the sum of the other two sides.
6
Task | being Euclid (cont’d) Define a function ‘is_triangle()’ which takes three float values (three parameters) returns True or False depending on whether three values can form a triangle Write a program which 1.asks the user to enter three float values 2.evaluates input numbers using function ‘is_triangle()’ 3.outputs YES or NO depending on the result of ‘is_triangle()’ function ※ Hint: You must call the function ‘raw_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
7
Task | being Edmund Gunter Modify the following program to ask the user for the number of steps (instead of using 41 fixed steps). import math sin = math.sin pi = math.pi for i in range(41): x = float(i) / 40.0 * 2 * pi print sin(x) for i in range(3) print i Hint : Try this to find out. Note : By the way, why 41 in range?
8
Task | being Gunter Reloaded #2 Change the program to print a nice curve like this: 40 80
9
Change the program to print a curve like this instead: Hint : Convert the result of ‘sin’ function to a number of blank and a single asterisk (e.g.) print “ “ * 5 + “!!” !! Task | being Gunter Reloaded #3 5
10
Any Questions?
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.