COMP 1005 Lecture 7: Lecture 7

16 views4 pages

Document Summary

Know the use of try-except for catching input errors. Know how to call and define functions in python. Plays as a guard for incorrect user input. Except defines the actions when an input error occurs try: do some calculations except: warn the user in case of input error. Example: inp = input( enter fahrenheit temperature: ) try: fahr = float(inp) cel = (fahr - 32. 0) * 5. 0 / 9. 0 print(cel) except: print( you should input a number ) Short-circuiting the evaluation: a case happens when the evaluation of a logical expression stops because the overall result is already known. Built-in functions are functions a programmer can use without the need for providing the function definition. User-defined functions are defined by the user to perform some functionality. Calling a function: function_name(arguments) def calculate_values(a,b): x = a+b y = a-b print( the value of x is: , x) print( the value of y is: , y) calculate_values(5,7) output:

Get access

Grade+20% off
$8 USD/m$10 USD/m
Billed $96 USD annually
Grade+
Homework Help
Study Guides
Textbook Solutions
Class Notes
Textbook Notes
Booster Class
40 Verified Answers
Class+
$8 USD/m
Billed $96 USD annually
Class+
Homework Help
Study Guides
Textbook Solutions
Class Notes
Textbook Notes
Booster Class
30 Verified Answers