Python Unit 6

Réussis tes devoirs et examens dès maintenant avec Quizwiz!

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)

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

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

What would this program print? def mystery(num, pattern): result = "" for i in range(num): result = result + pattern return result print mystery(3, "<>")

<><><>

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

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

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.

What is the purpose of exceptions?

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

# 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!

What will be the output of the following program? a = 4 def print_something(): a = "code" print a * 3 print_something()

codecodecode

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')

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')

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)

name = "Tracy" def print_hello(name): name = "Turtle" print "hello " + name print_hello(name)

hello Turtle

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()


Ensembles d'études connexes

Chapter 14 music FlashcardsListen to Minuet from Water Music. What mode does the piece shift to at 1:10?

View Set

Focus Center Language Arts 4110-D1- Building Sentences

View Set

Praxis Elem. Ed- Math(5003) Chpt 16 - Probability Concepts Quiz Questions

View Set

Chapter 8 & Chapter 9 & Chapter 10 exam

View Set

Part One—Analyzing Accounting Concepts and Procedures

View Set

Chapter 10 The Government in the Economy

View Set

Chapter 6: Eli Whitney and the Cotton Gin

View Set

Review 3-2 Linux+ Shell Scripting

View Set

Managerial Accounting chapter 21

View Set

PHA 326 Pharmaceutics: Final Exam

View Set

Multi-Digit Long Division Practice

View Set