Python test 2

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

Examine the following code: def area(width, height) area = width * height return area box1area = area(5, 2) box2area = area(6) What needs to change in order for the height in the area function to be 12 if the height is not specified when calling the function?

Change the def in def area (width, height = 12)

For each statement regarding variables and data types, indicate a Yes if the statement is true and no if false

When variables are declared a date type must be specified: NO The symbol to concatenate text and a numeric variable within a print statement is a plus sign: NO The symbol to concatenate two strings of text is a plus sign: YES

You are trying to build a sign-in sheet for a seven-day class with 10 students each day. Using drag and drop, drag the correct statements, in order, to crate this code.

for classDay in range (1,8): print(f"Day: {classDay}") for student in range (1,11): print(f"Student{student}______________")

You are writing code to have an activity for every day in a 30-day program. There should be no activity on day 15. Using the drop-down arrows, fill in the missing pieces to the code.

for dailyProgram in range (1,31): if dailyProgram == 15: print("No activity on day 15") continue print(f"This is day {dailyProgram}")

You are trying to generate 10 random numbers in between 1 and 10. Using drag drop, drag the proper code elements into the correct places. Not all code elements will be used.

from random import randint for i in range(10): print(randint(1,10))

You are wanting to write text a user inputs to text file called results. The file may exist, so your code needs to account for both the existence of the file and the lack of existence of the file. Using the drag and drop finish the code.

'a' 'w' '\n'

You are writing code to check to see if a file exists. If it does, it should open in append mode. If it does not, it should open in write mode. Using drag and drop.

import os if os.path.isfile('results.txt'): writeFile = open('results.txt','a') else: writeFile = open('results.txt','w')

Which type of error is an error in which programming code causes a divide by zero error?

Runtime

You have a code block that starts with an array. You have two goals with the array: Replace the color green with white Print the colors red blue and black

[1] [::2]

Evaluate the following code and then indicate a Yes for any true statement related to the code and No for any false statement: a = 3 b = a a = 5 sountItems = ["Golf clubs", "Golf balls"] westItems = ["Golf clubs", "Golf Balls"]

b == 5: NO southItems == westItems: YES southItems is westItems: NO

You are writing code to classify customers based on the dollar amounts they order. The rules are as the follows: Customers who buy at least $5000 worth are gold. Customers who buy between $2000 and $4999 are silver. Everyone else is bronze. Using the drop-down lists, fill in the missing code in pieces.

elif else

You need to write code that sends northern sales staff skis and everyone else golf balls if they exceed sales of $100,000 for a month. Using drag and drop, arrange the code pieces needed in the correct order.

if monthlySales > 100,000: if region == "North": print("Send skis") else: print("Send golf balls")

You are building a quiz app using Python. The code is missing an operator to indicate that a variable is not equal to a desired result.

if result != 10:

A junior programmer is struggling to get a numeric variable to print within a print statement. Using the drag-down arrow, add what is needed so the statement prints properly.

str

In the following code, choose the keyword that will ensure nothing runs if the is statement is false?

pass

In the command-line area, which command would run a Python file called calcArea?

python calcArea.py

choose the symbol that needs to be added to the comment out the line containing the code, "Set side variables".

# set side variables

Evaluate the following statements and indicate a Yes if statement is true and no if its false.

If statements have to have an else or elif statement: NO The action to take place under an if statement needs to be indented: YES If statements with elif statements need else statements as well: YES

A fellow Python programmer is trying to build a quiz and has created the following code: a = 3 b = 7 result = input("What is a + b?") if result = 10: print("You are correct!") else: print("You are incorrect.") This code is not working. Why?

The if result statement needs to read if int(result) == 10:

What would be the value of result that prints at the end of this code? a = 3 b = 7 c = 5 result = a + b * c print(result)

38

A junior programmer enters the following code: height = 5 width = 5 if height == width print("You have a square") When trying to run the code, errors are generated. Which two fixes are necessary in order for this code to work?

A colon needs yo be added at the end of the if condition The print statement needs to be indented

Using drag and drop match each expression with its proper default data type

"False" = String False = Boolean 1 = Integer 1.0 = Float

For each of the statements regarding pydoc, indicate a yes if the statement is true and a no if the statement is false

Pydoc is a self-contained executable that can be run from a command-line prompt: NO Pydoc generates documentation on Python methods: YES The proper syntax for pydoc is Python -m pydoc module where module represents the name of a Python modeul: YES

Analyze the following code and then answer yes or no. a = float(input("Enter a number.")) b = float(input("Enter a number to divide by.")) try: print(f"The answer is {a/b}.") except: print("This did not work. Did you try to divide by zero?") else: print("You successfully divided two numbers.") finally: print("Thank you for playing.")

The finally statement will run regardless of which pieces of code above it run: YES Both the try and except parts will run if a = 0: NO The else part will run if a = 0: YES

A junior programmer writes the following code to see how many boxes are leftover after crates have been filled: boxes = 25 crates = 4 leftover = boxes mod crates print(leftovers) The result is an error message. What needs to be done to fix the code?

The mod needs to change to a %

A junior programmer has coded a function to compute the area of a rectangle. The code does not work. Look over the code and choose the two problems with the code: function area(x,y): z = x * y print(z) area (5,10)

The two lines under the function declaration need to be indented AND The function keyword should be replaced with def

You are in the process of writing some code to divide two numbers. You want the result to display so long as there are no errors. If there are errors, you want to display a message indicating that the user wither tried to divide by zero or used an invalid number. Using the drop-down arrows, fill in the missing keywords to make this happen.

a = float(input("Enter a number. "))b = float(input("Enter a number to divide by. ")) try: print(f"The answer is {a/b}.") except: print("This did not work. Did you try to divide by zero?")

You are attempting to build a game to where the game player gets three guesses to answer a question correctly. After three incorrect guesses, the game is over. Use the drop-down arrows to fill in the missing code piece.

break

You have a block of code to start an array and then make adjustments to the array. The output should look like this: Cindy Jack Janet

characters.append("Cindy") characters.remove("Chrissy") characters.sort()

You are writing code to calculate the due date for a project, given a future date someone enters. This involves subtracting today's date from the due date entered. You want the users to enter the date in mm/dd/yy format. Using drag and drop, complete code example.

import datetime currentDate = datetime.date.today() %m, %d, %y

You are demonstrating how to do a calculation with one number raised to another number's power. Using the drag and drop, fill in the missing pieces of the code to accomplish this goal.

import math c = math.pow(a, b)

You are in process of writing code to delete a file if it exists, or just display a message indicating that there was no file to remove if the file does not exist. Using the drop-down arrows, select the proper code pieces to finish this code below.

import os if os.path.isfile('results.txt'): os.remove('results.txt')

You have two pieces of text that need to be concatenated. On adds two strings together and the other requires a variable value to be displayed within a sentence. Using drag and drop, fill in the missing symbols to complete the code.

price = 5.99 sqft = 100 print("The current price per square foot is ",price) print(f"You need enough for {sqft} sqft.")

Evaluate the following code and then the statements on the code. For each statement, indicate a Yes if the statement will return True and No if its false. quote = "The quick brown fox jumped over the lazy dogs"

print("jumps" in quote): NO print("jump" in quote): YES print("dogs" in quote): YES

Evaluate the following code. For each print statement indicate a Yes if the statement is true and No if false. a = 10 b = 20 c = 10

print((a == c) and (b == c)): NO print((a == c) or (b == c)): YES print((a == c) and (b != a)): YES

You are checking to make sure code inputs are not too long. Using the drop-down arrow, indicate the built-in keyword needed to print the total number of characters in the string with the first and last names.

print(len(firstName+lastName)

Which built-in module is used to display execution information?

sys

Drag the missing pieces of code to the message. If the calculation is successful, the message, "You know the number of crates you now need" should display.

try: boxes = int(input("Enter the number of boxes")) crates = boxes / 4 print(f"You need {crate} crates") except: print("You did not enter a valid number") else: print("You now know the number of crates you need")

You are trying to calculate the are of the rectangle given a width and height input from a user. Use the drop-down arrows to complete the code to allow a user to input both height and width, ensuring that only whole numbers are entered for the height and width.

width = int(input height = int(input print(f"The area of your shape is {area}")

You are attempting to write code to a text file called results. The file does not exist prior to your writing to it. Using the drop-down arrow, complete the code to fulfill this task, making sure the file does not take up memory after the write is complete.

writeFile = open('results.txt',w') toResults = input("what do you want to write to the results?") writeFile.write(toResults) writeFile.close()


Ensembles d'études connexes

22. Social Cognitions and Attitudes Quiz

View Set

Ch. 10: Motivation, Personality, and Emotion

View Set

Regional And Interregional interactions

View Set

Psychology Final Exam REAL QUESTIONS

View Set

PSYCH 7A CHAPTER 14: SOCIAL PSYCHOLOGY

View Set