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

Lecture 3 Part 1 - CS50's Introduction To Programming With Python

Uploaded by

fuad
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

Lecture 3 Part 1 - CS50's Introduction To Programming With Python

Uploaded by

fuad
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

11/18/24, 4:18 AM Lecture 3 - CS50's Introduction to Programming with Python

CS50’s Introduction to Programming with Python


OpenCourseWare

Donate  (https://cs50.harvard.edu/donate)

David J. Malan (https://cs.harvard.edu/malan/)


[email protected]
 (https://www.facebook.com/dmalan)  (https://github.com/dmalan)  (https://www.instagram.com/davidjmalan/) 
(https://www.linkedin.com/in/malan/)  (https://www.reddit.com/user/davidjmalan) 
(https://www.threads.net/@davidjmalan)  (https://twitter.com/davidjmalan)

Lecture 3
Exceptions
Runtime Errors
try
else
Creating a Function to Get an Integer
pass
Summing Up

Exceptions
 Exceptions are things that go wrong within our coding.

https://cs50.harvard.edu/python/2022/notes/3/ 1/8
11/18/24, 4:18 AM Lecture 3 - CS50's Introduction to Programming with Python

 In our text editor, type code hello.py to create a new file. Type as follows (with the intentional errors included):

print("hello, world)

Notice that we intentionally left out a quotation mark.


 Running python hello.py in our terminal window, an error is outputted. The compiler states that it is a “syntax error.”” Syntax
errors are those that require you to double-check that you typed in your code correction.
 You can learn more in Python’s documentation of Errors and Exceptions (https://docs.python.org/3/tutorial/errors.html).

Runtime Errors
 Runtime errors refer to those created by unexpected behavior within your code. For example, perhaps you intended for a user to
input a number, but they input a character instead. Your program may throw an error because of this unexpected input from the
user.
 In your terminal window, run code number.py . Code as follows in your text editor:

x = int(input("What's x? "))
print(f"x is {x}")

Notice that by including the f , we tell Python to interpolate what is in the curly braces as the value of x . Further, testing out
your code, you can imagine how one could easily type in a string or a character instead of a number. Even still, a user could type
nothing at all – simply hitting the enter key.
 As programmers, we should be defensive to ensure that our users are entering what we expected. We might consider “corner
cases” such as -1 , 0 , or cat .
 If we run this program and type in “cat”, we’ll suddenly see ValueError: invalid literal for int() with base 10: 'cat'
Essentially, the Python interpreter does not like that we passed “cat” to the int function.
 An effective strategy to fix this potential error would be to create “error handling” to ensure the user behaves as we intend.
 You can learn more in Python’s documentation of Errors and Exceptions (https://docs.python.org/3/tutorial/errors.html).

https://cs50.harvard.edu/python/2022/notes/3/ 2/8

You might also like