Python Code Only

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

Figure out what's the relationship between the strings "cat" and "Cat" by replacing the plus sign with comparison operators. print("cat" + "Cat") 1. "cat" equals "Cat" 2. "cat" is smaller than "Cat" 3. "cat" is larger than "Cat"

"cat" is larger than "Cat" Correct You nailed it! In Python uppercase letters are alphabetically sorted before lowercase letters.

Do you think you can flesh out your own function? I think you can! Let's give it a go. Flesh out the body of the print_seconds function so that it prints the total amount of seconds given the hours, minutes, and seconds function parameters. Remember that there are 3600 seconds in an hour and 60 seconds in a minute. def print_seconds(hours, minutes, seconds): print(___) print_seconds(1,2,3)

.

In math, the factorial of a number is defined as the product of an integer and all the integers below it. For example, the factorial of four (4!) is equal to 1*2*3*4=24. Fill in the blanks to make the factorial function return the right number. def factorial(n): result = 1 for i in ___: __ return result print(factorial(4)) # should return 24 print(factorial(5)) # should return 120

.

In this code, there's an initialization problem that's causing our function to behave incorrectly. Can you find the problem and fix it? def count_down(start_number): while (current > 0): print(current) current -= 1 print("Zero!") count_down(3)

.

Ready to try it yourself? See if you can reduce the code duplication in this script. In this code, identify the repeated pattern and replace it with a function called month_days, that receives the name of the month and the number of days in that month as parameters. Adapt the rest of the code so that the result is the same. Confirm your results by making a function call with the correct parameters for both months listed. # REPLACE THIS STARTER CODE WITH YOUR FUNCTION june_days = 30 print("June has " + str(june_days) + " days.") july_days = 31 print("July has " + str(july_days) + " days.")

.

The following code causes an infinite loop. Can you figure out what's missing and how to fix it? def print_range(start, end): # Loop through the numbers from start to end n = start while n <= end: print(n) print_range(1, 5) # Should print 1 2 3 4 5 (each number on its own line)

.

The function sum_positive_numbers should return the sum of all positive numbers between the number n received and 1. For example, when n is 3 it should return 1+2+3=6, and when n is 5 it should return 1+2+3+4+5=15. Fill in the gaps to make this work: def sum_positive_numbers(n): # The base case is n being smaller than 1 if n < 1: return ___ # The recursive case is adding this number to # the sum of the numbers smaller than this one. return ___ + sum_positive_numbers(___) print(sum_positive_numbers(3)) # Should be 6 print(sum_positive_numbers(5)) # Should be 15

.

The is_positive function should return True if the number received is positive, otherwise it returns None. Can you fill in the gaps to make that happen? def is_positive(number): if ___: return ___

.

The number_group function should return "Positive" if the number received is positive, "Negative" if it's negative, and "Zero" if it's 0. Can you fill in the gaps to make that happen? def number_group(number): if ___: return "Positive" elif ___: return ___ else: ___ print(number_group(10)) #Should be Positive print(number_group(0)) #Should be Zero print(number_group(-5)) #Should be Negative

.

The validate_users function is used by the system to check if a list of users is valid or invalid. A valid user is one that is at least 3 characters long. For example, ['taylor', 'luisa', 'jamaal'] are all valid users. When calling it like in this example, something is not right. Can you figure out what to fix? def validate_users(users): for user in users: if is_valid(user): print(user + " is valid") else: print(user + " is invalid") validate_users("purplecat")

.

This function to calculate the area of a rectangle is not very readable. Can you refactor it, and then call the function to calculate the area with base of 5 and height of 6? Tip: a function that calculates the area of a rectangle should probably be called rectangle_area, and if it's receiving base and height, that's what the parameters should be called. def f1(x, y): z = x*y # the area is base*height print("The area is " + str(z))

.

Use the get_seconds function to work out the amount of seconds in 2 hours and 30 minutes, then add this number to the amount of seconds in 45 minutes and 15 seconds. Then print the result. def get_seconds(hours, minutes, seconds): return 3600*hours + 60*minutes + seconds amount_a = get_seconds(___) amount_b = get_seconds(___) result = ___ print(result)

.

Want to give it a go yourself? Be my guest! Modify the first_and_last function so that it returns True if the first letter of the string is the same as the last letter of the string, False if they're different. Remember that you can access characters using message[0] or message[-1]. Be careful how you handle the empty string, which should return True since nothing is equal to nothing. def first_and_last(message): return False print(first_and_last("else")) print(first_and_last("tree")) print(first_and_last(""))

.

Want to try this out? Let's give it a go! Fill in the gaps of the sum_squares function, so that it returns the sum of all the squares of numbers between 0 and x (not included). Remember that you can use the range(x) function to generate a sequence of numbers from 0 to x (not included). def square(n): return n*n def sum_squares(x): sum = 0 for n in ___: sum += __ return __ print(sum_squares(10)) # Should be 285

.

x = 0 while x < 5: print("Not there yet, x=" + str(x)) x = x + 1 print("x=" + str(x)) How many times will "Not there yet" be printed?

5

Can you work out what this function does? Try passing different parameters to the attempts function to see what it does. def attempts(n): x = 1 while x <= n: print("Attempt " + str(x)) x += 1 print("Done") attempts(5)

Attempt 1 Attempt 2 Attempt 3 Attempt 4 Attempt 5 Done

The is_positive function should return True if the number received is positive and False if it isn't. Can you fill in the gaps to make that happen? def is_positive(number): if number > 0: return True else: ___

def is_positive(number): if number > 0: return True else: return False

Given the following code: teams = [ 'Dragons', 'Wolves', 'Pandas', 'Unicorns'] for home_team in teams: for away_team in teams: What should the next line be to avoid both variables being printed with the same value? while home_team != away_team: for home_team == away_team: away_team = home_team if home_team != away_team:

if home_team != away_team:


Ensembles d'études connexes

Chapter 14 Health Insurance Concepts, Programs, and tax considerations

View Set

As Accounting: Limited Companies

View Set

A & P 2 - Chapter 24 - Fluid, Electrolyte, and Acid-Base Balance

View Set

Chapter 8: Saving, Investment and the Financial System

View Set

AP Human Geography- Models & theories

View Set

Chapter 50: Male and Female Reproductive Systems

View Set