Python Unit 6 Exceptions and Functions

0.0(0)
studied byStudied by 0 people
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
Card Sorting

1/24

encourage image

There's no tags or description

Looks like no tags are added yet.

Study Analytics
Name
Mastery
Learn
Test
Matching
Spaced

No study sessions yet.

25 Terms

1
New cards

Which of the following programs will NOT cause an error?

def say_hi(first_name='John', last_name='Doe'):

print 'hi ' + first_name + ' ' + last_name

say_hi('Polly Pocket')

2
New cards

Which of the programs below will print the following output?

I would like a green balloon.

def balloon_choice(color):

print 'I would like a ' + color + ' balloon.'

balloon_choice('green')

3
New cards

What will be the output of the following program?

a = 4

def print_something():

a = "code"

print a * 3

print_something()

codecodecode

4
New cards

What will be the output of the following program?

name = "Tracy"

def print_hello(name):

name = "Turtle"

print "hello " + name

print_hello(name)

hello Turtle

5
New cards

What would this program print?

def mystery(num, pattern):

result = ""

for i in range(num):

result = result + pattern

return result

print mystery(3, "<>")

<><><>

6
New cards

Consider the following functions.

def fn_a(num):

if num > 3:

print "Fizz"

def fn_b(num):

if num > 2 and num < 4:

print "Fizz"

Which for loop would print

for i in range(6):

fn_a(i)

7
New cards

What would this program print?

x_pos = 16

y_pos = 20

# Computes the midpoint between (0,0)

# and the point

def compute_distance(x_pos, y_pos):

print "Original Point: " + str(x_pos) + ", " + str(y_pos)

print "Midpoint from Origin: " + str(x_pos/2) + ", " + str(y_pos/2)

compute_distance(12, 14)

print "Point: " + str(x_pos) + ", " + str(y_pos)

Original Point: 12, 14

Midpoint from Origin: 6, 7

Point: 16, 20

8
New cards

Which of the following options is NOT a valid Python function definition?

# computes the surface area of a rectangular prism

def surface_area(length, width=10, height):

return 2(length width + width height + height length)

9
New cards

What is the output of this program? Assume the user enters "kipp52", then "baseball45".

# Checks if the user entered the correct username

def validate_user(username):

if username != "coder43" and username != "goodHacker90" and username != "baseball45":

return False

else:

return True

# Gets the user's username

def get_username():

while True:

username = input("Enter your username: ")

if validate_user(username):

print "Welcome!"

return username

else:

print "That's not a valid username. Try again"

print "Welcome to CodeHS!"

get_username()

Welcome to CodeHS!

Enter your username: kipp52

That's not a valid username. Try again

Enter your username: baseball45

Welcome!

10
New cards

What will be printed when the program below is run?

def add_two(x):

return x + 2

def multiply_by_three(x):

return x * 3

def my_function(x):

return add_two(x) + multiply_by_three(x)

print my_function(12)

50

11
New cards

What is the purpose of exceptions?

To indicate that something has gone wrong, and the program does not know how to continue

12
New cards

What will be printed to the screen if the inputs given to the program below are 1.2, -4, hi, 5 ?

def retrieve_positive_number():

while True:

try:

number = int(input("Enter a positive number: "))

if number > 0:

return number

print "The number must be positive!"

except ValueError:

print "That wasn't a number!"

print retrieve_positive_number()

Enter a positive number: 1.2

That wasn't a number!

Enter a positive number: -4

The number must be positive!

Enter a positive number: hi

That wasn't a number!

Enter a positive number: 5

5

13
New cards

Which of the following inputs to the program below would return a print statement containing Your number:

try:

my_number = int(input("Enter an integer: "))

print "Your number: " + str(my_number)

except ValueError:

print "That wasn't an integer!"

5

14
New cards

What line of code should be written after the function definition to add two single digit values and print the sum?

def get_single_digit():

while True:

try:

num = int(input("Enter a single digit positive number: "))

if num > 9:

print "Number must be less than 10"

elif num < 0:

print "Number must be positive."

else:

return num

except ValueError:

print "That isn't a number!"

print get_single_digit() + get_single_digit()

return get_single_digit() + get_single_digit()

print get_single_digit(4) + get_single_digit(5)

print get_single_digit()++

print get_single_digit() + get_single_digit()

15
New cards

Why do programmers use try and except statements when their code might throw an exception?

To handle exceptions gracefully and either handle the error or print out a useful error message.

16
New cards

Which of the following is the correct way to declare a function in Python?

def print_hello():

print("hello")

17
New cards

What is the correct way to call a function named print_sum?

print_sum()

18
New cards

Which of the following code samples correctly passes the number 10 as an argument to the function print_number?

print_number(10)

19
New cards

Which of the following code samples correctly creates a function that takes a single parameter and prints it?

def print_number(x):

print(x)

20
New cards

In which namespace is the variable 'x' defined in the following program?

x = 10

def add_nums():

y = 2

z = x + y

print(z)

Global namespace

21
New cards

Consider this code snippet.

x = 10

def add_nums():

y = 2

z = x + y

print(z)

Which of the following additional functions could be safely added to this code, without causing an error?

def subtract_nums():

y = 5

z = x - y

print(z)

22
New cards

Which of the following functions successfully returns double the variable 'number'?

def my_function(number):

return number*2

23
New cards

Which of the following functions successfully returns the number 10?

def my_function():

return 10

24
New cards

Which of the following keywords are relevant to exceptions in Python?

I. def

II. except

III. try

II and III

25
New cards

What type of error occurs when a program is expecting an integer value and the user enters a string?

ValueError