DATABASES

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

1/83

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.

84 Terms

1
New cards

Evaluate each situation regarding data types and answer Yes if it is true or No if it is false.

  1. Python makes the distinction between integers and floating variables.

  2. When declaring variables in Python, a data type must be specified.

  3. When setting a boolean variable, the value must start with a capital letter.

  1. Yes - Python makes the distinction between integers and floating variables.

  2. No - When declaring variables in Python, a data type must be specified.

  3. Yes - When setting a boolean variable, the value must start with a capital letter.


  • Python is a loosely-typed language. Thus, we do not specify a data type when declaring a variable.

  • Python does have distinct data types for integers and floating decimals.

  • When indicating a true or false value for a boolean variable, the value must start with a capital letter, meaning it must be set to True or False.

2
New cards

A developer wants to make sure a serial number cannot be used in a calculation and that a dollar amount entered as a whole number can have decimals.

Then, a message should be displayed to tell a customer the serial number and price for the product.

Using the dropdown arrows, fill in the code to use the proper functions to convert the data to the necessary data types.

𝚜𝚎𝚛𝚒𝚊𝚕𝚗𝚞𝚖𝚋𝚎𝚛 = (1) [ 𝚜𝚝𝚛 / 𝚜𝚝𝚛𝚒𝚗𝚐 / 𝚝𝚎𝚡𝚝 ] (𝟻𝟻𝟻𝟻𝟻)

𝚊𝚖𝚘𝚞𝚗𝚝 = (2) [ 𝚏𝚕𝚘𝚊𝚝 / 𝚏𝚕 / 𝚍𝚎𝚌𝚒𝚖𝚊𝚕 / 𝚍𝚘𝚞𝚋𝚕𝚎 ] (𝟺𝟺)

𝚖𝚎𝚜𝚜𝚊𝚐𝚎 = “𝚃𝚑𝚎 𝚜𝚎𝚛𝚒𝚊𝚕 𝚗𝚞𝚖𝚋𝚎𝚛 𝚘𝚏 𝚝𝚑𝚎 𝚙𝚛𝚘𝚓𝚎𝚌𝚝 𝚒𝚜” (3) [ , / & / + ] 𝚜𝚎𝚛𝚒𝚊𝚕𝚗𝚞𝚖𝚋𝚎𝚛 (4) [ , / & / + ] “𝚊𝚗𝚍 𝚒𝚝 𝚠𝚒𝚕𝚕 𝚌𝚘𝚜𝚝” , 𝚊𝚖𝚘𝚞𝚗𝚝 , “.”

  1. 𝚜𝚝𝚛

  2. 𝚏𝚕𝚘𝚊𝚝

  3. ,

  4. ,

𝚜𝚎𝚛𝚒𝚊𝚕𝚗𝚞𝚖𝚋𝚎𝚛 = 𝚜𝚝𝚛(𝟻𝟻𝟻𝟻𝟻)

𝚊𝚖𝚘𝚞𝚗𝚝 = 𝚏𝚕𝚘𝚊𝚝(𝟺𝟺)

𝚖𝚎𝚜𝚜𝚊𝚐𝚎 = "𝚃𝚑𝚎 𝚜𝚎𝚛𝚒𝚊𝚕 𝚗𝚞𝚖𝚋𝚎𝚛 𝚘𝚏 𝚝𝚑𝚎 𝚙𝚛𝚘𝚓𝚎𝚌𝚝 𝚒𝚜" , 𝚜𝚎𝚛𝚒𝚊𝚕𝚗𝚞𝚖𝚋𝚎𝚛 , "𝚊𝚗𝚍 𝚒𝚝 𝚠𝚒𝚕𝚕 𝚌𝚘𝚜𝚝", 𝚊𝚖𝚘𝚞𝚗𝚝, "."


  • The 𝚜𝚝𝚛 function converts data to a string while the 𝚏𝚕𝚘𝚊𝚝 function converts data to a floating point decimal.

  • Because these are different data types, a + sign will not work. & and | are not concatenation symbols. Commas are needed here.

3
New cards

A developer needs to write code to reverse the character output of a product code. Which variable declaration will reverse one of the product codes?

○ [::𝟷]

○ [𝟷::]

○ [::-𝟷]

○ [-𝟷::]

[::-𝟷]


To reverse the text within a value, one should use a reverse slice, which looks like this [::-𝟷]

[::𝟷] does not do a slice.

[𝟷::] slices off the first character of text.

[-𝟷::] slices off the last character of text.

4
New cards

A warehouse manager has partial safety kits in inventory, but needs to make sure that the count of kits is presented as a whole number with the number of complete sets on display. Drag the appropriate function to the missing code area to convert the inventoryCount variable to a value of 19.

𝚒𝚗𝚟𝚎𝚗𝚝𝚘𝚛𝚢𝙲𝚘𝚞𝚗𝚝 = 𝟷𝟿.𝟿𝟻

𝚙𝚛𝚒𝚗𝚝("𝚆𝚎 𝚑𝚊𝚟𝚎 " + [ ___ ] (𝚒𝚗𝚟𝚎𝚗𝚝𝚘𝚛𝚢𝙲𝚘𝚞𝚗𝚝) + " 𝚌𝚘𝚖𝚙𝚕𝚎𝚝𝚎 𝚞𝚗𝚒𝚝𝚜 𝚒𝚗 𝚜𝚝𝚘𝚌𝚔.")

○ 𝚛𝚘𝚞𝚗𝚍

○ 𝚏𝚕𝚘𝚘𝚛

○ 𝚌𝚎𝚒𝚕

○ 𝚒𝚗𝚝

𝚒𝚗𝚝

𝚒𝚗𝚟𝚎𝚗𝚝𝚘𝚛𝚢𝙲𝚘𝚞𝚗𝚝 = 𝟷𝟿.𝟿𝟻

𝚙𝚛𝚒𝚗𝚝("𝚆𝚎 𝚑𝚊𝚟𝚎 " + 𝚒𝚗𝚝(𝚒𝚗𝚟𝚎𝚗𝚝𝚘𝚛𝚢𝙲𝚘𝚞𝚗𝚝) + " 𝚌𝚘𝚖𝚙𝚕𝚎𝚝𝚎 𝚞𝚗𝚒𝚝𝚜 𝚒𝚗 𝚜𝚝𝚘𝚌𝚔.")


In this scenario, the number of complete units needs to be displayed, meaning that anything after a decimal point needs to not be represented.

To accomplish this, the int function is needed as it extracts the whole number from a value with a floating decimal.

The round function will not work because it will round the number up to 20.

The floor function rounds a number down to the nearest integer.

The ceil function will raise the number to 20.

5
New cards

A developer needs to build a data structure with animals and then sort the structure. Using drag and drop, drag the following lines of code into the correct order to produce this output:

𝙱𝚎𝚊𝚛𝚜

𝙹𝚊𝚐𝚞𝚊𝚛𝚜

𝙻𝚒𝚘𝚗𝚜

𝙿𝚊𝚗𝚝𝚑𝚎𝚛𝚜

Step 1 - ?

Step 2 - ?

Step 3 - ?

Step 4 - ?

Step 5 - ?

a. 𝚊𝚗𝚒𝚖𝚊𝚕𝚜.𝚊𝚍𝚍("𝙹𝚊𝚐𝚞𝚊𝚛𝚜")

b. 𝚙𝚛𝚒𝚗𝚝 (𝚊𝚗𝚒𝚖𝚊𝚕)}

c. 𝚏𝚘𝚛 𝚊𝚗𝚒𝚖𝚊𝚕 𝚒𝚗 𝚊𝚗𝚒𝚖𝚊𝚕𝚜:

d. 𝚊𝚗𝚒𝚖𝚊𝚕𝚜.𝚜𝚘𝚛𝚝()

e. 𝚙𝚛𝚒𝚗𝚝(𝚊𝚗𝚒𝚖𝚊𝚕)

f. 𝚊𝚗𝚒𝚖𝚊𝚕𝚜.𝚊𝚙𝚙𝚎𝚗𝚍("𝙹𝚊𝚐𝚞𝚊𝚛𝚜")

g. 𝚊𝚗𝚒𝚖𝚊𝚕𝚜 = ["𝙱𝚎𝚊𝚛𝚜", "𝙿𝚊𝚗𝚝𝚑𝚎𝚛𝚜", "𝙻𝚒𝚘𝚗𝚜"]

h. 𝚏𝚘𝚛 𝚊𝚗𝚒𝚖𝚊𝚕 𝚒𝚗 𝚊𝚗𝚒𝚖𝚊𝚕𝚜 {

Step 1: g. 𝚊𝚗𝚒𝚖𝚊𝚕𝚜 = ["𝙱𝚎𝚊𝚛𝚜", "𝙿𝚊𝚗𝚝𝚑𝚎𝚛𝚜", "𝙻𝚒𝚘𝚗𝚜"]

Step 2: f. 𝚊𝚗𝚒𝚖𝚊𝚕𝚜.𝚊𝚙𝚙𝚎𝚗𝚍("𝙹𝚊𝚐𝚞𝚊𝚛𝚜")

Step 3: d. 𝚊𝚗𝚒𝚖𝚊𝚕𝚜.𝚜𝚘𝚛𝚝()

Step 4: c. 𝚏𝚘𝚛 𝚊𝚗𝚒𝚖𝚊𝚕 𝚒𝚗 𝚊𝚗𝚒𝚖𝚊𝚕𝚜:

Step 5: e. 𝚙𝚛𝚒𝚗𝚝 (𝚊𝚗𝚒𝚖𝚊𝚕)

𝚊𝚗𝚒𝚖𝚊𝚕𝚜 = ["𝙱𝚎𝚊𝚛𝚜", "𝙻𝚒𝚘𝚗𝚜", "𝙹𝚊𝚐𝚞𝚊𝚛𝚜"]

𝚊𝚗𝚒𝚖𝚊𝚕𝚜.𝚊𝚙𝚙𝚎𝚗𝚍("𝙿𝚊𝚗𝚝𝚑𝚎𝚛𝚜")

𝚊𝚗𝚒𝚖𝚊𝚕𝚜.𝚜𝚘𝚛𝚝()

𝚏𝚘𝚛 𝚊𝚗𝚒𝚖𝚊𝚕 𝚒𝚗 𝚊𝚗𝚒𝚖𝚊𝚕𝚜:

⇥ 𝚙𝚛𝚒𝚗𝚝(𝚊𝚗𝚒𝚖𝚊𝚕)


First, the list of animals needs to be built.

Then, the 𝙹𝚊𝚐𝚞𝚊𝚛𝚜 need to be added (appended).

At this point, the list is not in alphabetical order, so it needs to be sorted.

Then, a 𝚏𝚘𝚛 loop will go through and print each animal.

6
New cards

Analyze this code example:

𝚙𝚒𝚎𝚌𝚎𝚜 = ["𝚔𝚒𝚗𝚐", "𝚚𝚞𝚎𝚎𝚗", "𝚛𝚘𝚘𝚔", "𝚋𝚒𝚜𝚑𝚘𝚙", "𝚔𝚗𝚒𝚐𝚑𝚝", "𝚙𝚊𝚠𝚗"]

𝚙𝚒𝚎𝚌𝚎𝚜.𝚜𝚘𝚛𝚝()

Which two commands will display the rook?

𝚙𝚛𝚒𝚗𝚝(𝚙𝚒𝚎𝚌𝚎𝚜[𝟻])

𝚙𝚛𝚒𝚗𝚝(𝚙𝚒𝚎𝚌𝚎𝚜[𝟹])

𝚙𝚛𝚒𝚗𝚝(𝚙𝚒𝚎𝚌𝚎𝚜[𝟼])

𝚙𝚛𝚒𝚗𝚝(𝚙𝚒𝚎𝚌𝚎𝚜[-𝟷])

𝚙𝚛𝚒𝚗𝚝(𝚙𝚒𝚎𝚌𝚎𝚜[𝟻])

𝚙𝚛𝚒𝚗𝚝(𝚙𝚒𝚎𝚌𝚎𝚜[-𝟷])


Once this list is sorted, the rook becomes the final item in the list.

There are six items in the list. Lists are zero-based, meaning the first item is item 0, and consequently the rook is item 5, not 6.

Another way to print the last item in the list is to refer to the item as -1, as the last item in a list will have an index of -1, as well as its original number.

3 is incorrect because rook is not third (or fourth) item in the list once the list is sorted.

7
New cards

Analyze the following code:

𝚡 = 𝟻

𝚢 = 𝟹

𝚣 = 𝚡 + 𝚢 > 𝟽 𝚊𝚗𝚍 𝚡 * 𝚢 < 𝟷𝟻 𝚘𝚛 𝚡 - 𝚢 > 𝟸

Which expression is performed last for calculating the value of z?

○ 𝚡 + 𝚢 > 𝟽 𝚊𝚗𝚍 𝚡 * 𝚢 < 𝟷𝟻

○ 𝚣 =

○ 𝚡 - 𝚢 > 𝟸

○ 𝚡 * 𝚢 < 𝟷𝟻 𝚘𝚛 𝚡 - 𝚢 > 𝟸

𝚣 =


With order of operations, logical operators, such as 𝚊𝚗𝚍, 𝚘𝚛, 𝚘𝚛 𝚗𝚘𝚝, are evaluated before assignment operators, such as =.

Comparison operators take precedence over logical operators.

Within logical operators, 𝚗𝚘𝚝 takes precedence over 𝚊𝚗𝚍. 𝚊𝚗𝚍 takes precedence over 𝚘𝚛. Thus, the expression for z is evaluated in this order.

𝚡 + 𝚢 > 𝟽 (True)

𝚡 * 𝚢 < 𝟷𝟻 (False)

𝚡 - 𝚢 > 𝟸 (False)

At this point, 𝚣 = 𝚃𝚛𝚞𝚎 𝚊𝚗𝚍 𝙵𝚊𝚕𝚜𝚎 𝚘𝚛 𝙵𝚊𝚕𝚜𝚎

𝚊𝚗𝚍 is evaluated before 𝚘𝚛, thus 𝚣 = 𝙵𝚊𝚕𝚜𝚎 𝚘𝚛 𝙵𝚊𝚕𝚜𝚎 and ultimately, 𝚣 = 𝙵𝚊𝚕𝚜𝚎

8
New cards

Evaluate each of these variables and assignments and then indicate, for each expression, Yes if the expression is true or No if it is false.

𝚊 = 𝟷𝟶𝟶 - 𝟽𝟶 / 𝟽

𝚋 = (𝟹𝟻 % 𝟷𝟻) // 𝟸

𝚌 = -𝟹 ** 𝟸

  1. 𝚊 == 𝟿𝟶

  2. 𝚋 == 𝟸.𝟻

  3. 𝚌 == -𝟿

  1. Yes

  2. No

  3. Yes


  • 𝚊 = 𝟿𝟶 as the division of 𝟽𝟶 / 𝟽 is done first and then subtracted from 𝟷𝟶𝟶.

  • 𝚋 != 𝟸.𝟻 as the 𝟹𝟻 % 𝟷𝟻 in parentheses is done first, leaving a modulus of 𝟻 (𝟹𝟻 % 𝟷𝟻 = 𝟸 with a remainder of 𝟻). The // is floor division, so 𝟻 // 𝟸 = 𝟸 and not 𝟸.𝟻. 𝟻 / 𝟸 = 𝟸.𝟻

  • 𝚌 = -𝟿 as 𝟹 ** 𝟸 is calculated first, giving us 𝟿, and then the unary minus sign makes 𝟿 a -𝟿.

9
New cards

As part of a student app, the following messages need to be delivered for the following grades:

Grade

Message

100

Outstanding

90-99

Great

71-89

You are doing well. Strive to improve.

70 and under

Study hard

Use this information and the dropdown arrows to complete the code as needed.

𝚒𝚏 𝚐𝚛𝚊𝚍𝚎 (1) ___ 𝟷𝟶𝟶:

○ ==

○ =

○ >=

⇥ 𝚙𝚛𝚒𝚗𝚝("𝙾𝚞𝚝𝚜𝚝𝚊𝚗𝚍𝚒𝚗𝚐")

𝚎𝚕𝚒𝚏 𝚐𝚛𝚊𝚍𝚎 (2) ___ 𝟿𝟶:

○ >=

○ >

○ <=

⇥ 𝚙𝚛𝚒𝚗𝚝("𝙶𝚛𝚎𝚊𝚝")

𝚎𝚕𝚒𝚏 𝚐𝚛𝚊𝚍𝚎 (3) ___ 𝟽𝟶:

○ <=

○ <

○ >=

𝚙𝚛𝚒𝚗𝚝("𝚂𝚝𝚞𝚍𝚢 𝚑𝚊𝚛𝚍")

𝚎𝚕𝚜𝚎:

⇥ 𝚙𝚛𝚒𝚗𝚝("𝚈𝚘𝚞 𝚊𝚛𝚎 𝚍𝚘𝚒𝚗𝚐 𝚠𝚎𝚕𝚕. 𝚂𝚝𝚛𝚒𝚟𝚎 𝚝𝚘 𝚒𝚖𝚙𝚛𝚘𝚟𝚎")

  1. ==

  2. >=

  3. <=

𝚒𝚏 𝚐𝚛𝚊𝚍𝚎 == 𝟷𝟶𝟶:

⇥ 𝚙𝚛𝚒𝚗𝚝("𝙾𝚞𝚝𝚜𝚝𝚊𝚗𝚍𝚒𝚗𝚐")

𝚎𝚕𝚒𝚏 𝚐𝚛𝚊𝚍𝚎 >= 𝟿𝟶:

⇥ 𝚙𝚛𝚒𝚗𝚝("𝙶𝚛𝚎𝚊𝚝")

𝚎𝚕𝚒𝚏 𝚐𝚛𝚊𝚍𝚎 <= 𝟽𝟶:

⇥ 𝚙𝚛𝚒𝚗𝚝("𝚂𝚝𝚞𝚍𝚢 𝚑𝚊𝚛𝚍")

𝚎𝚕𝚜𝚎:

⇥ 𝚙𝚛𝚒𝚗𝚝("𝚈𝚘𝚞 𝚊𝚛𝚎 𝚍𝚘𝚒𝚗𝚐 𝚠𝚎𝚕𝚕. 𝚂𝚝𝚛𝚒𝚟𝚎 𝚝𝚘 𝚒𝚖𝚙𝚛𝚘𝚟𝚎")

The == symbol represents is equal to and should be used for a grade of 100.

>= represents greater than or equal to and should be used for all other grades 90 and up. > is not the correct operator here because 90 needs to be included.

<= represents less than or equal to and should be used for all grades 70 and below. < is not the correct operator here because 70 needs to be included.

10
New cards

The following variables have been assigned the following values:

𝚊 = 𝟷𝟶

𝚋 = 𝟹

Using the dropdown arrows, match each operator to the desired result for each calculation.

𝚊 (1) [ + / * / // ] 𝚋 = 𝟷𝟹

𝚊 (2) [ - / / / // ] 𝚋 = 𝟽

𝚊 (3) [ % / // / / ] 𝚋 = 𝟹.𝟹𝟹

𝚊 (4) [ // / / / % ] 𝚋 = 𝟹

𝚊 (5) [ // / / / % ] 𝟹 = 𝟷

  1. +

  2. -

  3. /

  4. //

  5. %


𝚊 + 𝚋 = 𝟷𝟹

𝚊 - 𝚋 = 𝟽

𝚊 / 𝚋 = 𝟹.𝟹𝟹

𝚊 // 𝚋 = 𝟹 as // represents floor division

𝚊 % 𝟹 = 𝟷 as % is a modulus, meaning remainder, and 𝟷𝟶 divided by 𝟹 = 𝟹 with a remainder of 𝟷.

11
New cards

Look at the following code.

For each statement, select Yes if the statement is True from a Boolean standpoint and No if the statement is False from a Boolean standpoint.

𝚊 = 𝟻

𝚋 = 𝟹

𝚌 = 𝚋

𝚋 = 𝚊

  1. 𝚊 == 𝚋

  2. 𝚊 𝚒𝚜 𝚋

  3. 𝚋 == 𝟹

  4. 𝚌 𝚒𝚜 𝚋

  1. Yes

  2. Yes

  3. No

  4. No


  • The last statement set b = a, so a == b.

  • And, because b = a, a is b.

  • When b was set to a, however, b took a value of 5, not 3, so b == 3 is a false statement.

  • Setting c = b puts does not put both variables in the same memory space, so c is not b.

12
New cards

Look at the following code and then select the correct keyword to use to check to see if the word "nine" is part of the quote.

If this code runs, it should return a value of True.

𝚚𝚞𝚘𝚝𝚎 = "𝙰 𝚜𝚝𝚒𝚝𝚌𝚑 𝚒𝚗 𝚝𝚒𝚖𝚎 𝚜𝚊𝚟𝚎𝚜 𝚗𝚒𝚗𝚎"

𝚙𝚛𝚒𝚗𝚝 ("𝚗𝚒𝚗𝚎" [ 𝚒𝚜 / 𝚒𝚗 / 𝚜𝚞𝚋 / 𝚜𝚞𝚋𝚜𝚝𝚛𝚒𝚗𝚐 ] 𝚚𝚞𝚘𝚝𝚎)

𝚒𝚗

𝚚𝚞𝚘𝚝𝚎 = "𝙰 𝚜𝚝𝚒𝚝𝚌𝚑 𝚒𝚗 𝚝𝚒𝚖𝚎 𝚜𝚊𝚟𝚎𝚜 𝚗𝚒𝚗𝚎"

𝚙𝚛𝚒𝚗𝚝 ("𝚗𝚒𝚗𝚎" 𝚒𝚗 𝚚𝚞𝚘𝚝𝚎)


The 𝚒𝚗 keyword is used as a containment keyword.

In other words, the 𝚒𝚗 keyword checks to see if a number or text is inside of a variable.

13
New cards

Drag the lines of code into the correct order to accomplish the following:

The messages "Great month" and "Keep it going" are printed for month sales of over 10000.

Not all lines of code will be used.

Line 1 - ?

Line 2 - ?

Line 3 - ?

a. 𝚎𝚕𝚜𝚎

b. 𝚒𝚏 𝚖𝚘𝚗𝚝𝚑_𝚜𝚊𝚕𝚎𝚜 > 𝟷𝟶𝟶𝟶𝟶:

c. 𝚙𝚛𝚒𝚗𝚝("𝙶𝚛𝚎𝚊𝚝 𝚖𝚘𝚗𝚝𝚑")

d. 𝚙𝚛𝚒𝚗𝚝("𝙺𝚎𝚎𝚙 𝚒𝚝 𝚐𝚘𝚒𝚗𝚐")

e. 𝚎𝚕𝚒𝚏 𝚖𝚘𝚗𝚝𝚑_𝚜𝚊𝚕𝚎𝚜 <= 𝟷𝟶𝟶𝟶𝟶

f. 𝚒𝚏 𝚖𝚘𝚗𝚝𝚑_𝚜𝚊𝚕𝚎𝚜 >= 𝟷𝟶𝟶𝟶𝟶

Line 1 - b. 𝚒𝚏 𝚖𝚘𝚗𝚝𝚑_𝚜𝚊𝚕𝚎𝚜 > 𝟷𝟶𝟶𝟶𝟶:

Line 2 - c. 𝚙𝚛𝚒𝚗𝚝("𝙶𝚛𝚎𝚊𝚝 𝚖𝚘𝚗𝚝𝚑")

Line 3 - d. 𝚙𝚛𝚒𝚗𝚝("𝙺𝚎𝚎𝚙 𝚒𝚝 𝚐𝚘𝚒𝚗𝚐")

𝚒𝚏 𝚖𝚘𝚗𝚝𝚑_𝚜𝚊𝚕𝚎𝚜 > 𝟷𝟶𝟶𝟶𝟶 :

⇥ 𝚙𝚛𝚒𝚗𝚝("𝙶𝚛𝚎𝚊𝚝 𝚖𝚘𝚗𝚝𝚑")

⇥ 𝚙𝚛𝚒𝚗𝚝("𝙺𝚎𝚎𝚙 𝚒𝚝 𝚐𝚘𝚒𝚗𝚐")

First, the condition specifically wants the messages for sales over 10000.

Thus, the correct if statement is month_sales > 10000 and not month_sales >= 10000.

Secondly, there is no other condition besides the first one, so there is no need for an else statement or the "Try again next month" message.


14
New cards

Choose the correct lines of code to satisfy the needs of the following function:

Students with scores of 90 or higher get an A.

Students with scores from 80 to 89 get a B.

Students with scores from 70 to 79 get a C.

Everyone else gets an F.

𝚍𝚎𝚏 𝚐𝚛𝚊𝚍𝚎(𝚜𝚌𝚘𝚛𝚎):

𝚒𝚏 (1) [ ___ ]

○ 𝚜𝚌𝚘𝚛𝚎 > 𝟿𝟶:

○ 𝚜𝚌𝚘𝚛𝚎 >= 𝟿𝟶:

○ 𝚜𝚌𝚘𝚛𝚎 = 𝟿𝟶:

○ 𝚜𝚌𝚘𝚛𝚎 < 𝟷𝟶𝟶:

⇥ 𝚐𝚛𝚊𝚍𝚎 = "𝙰"


𝚎𝚕𝚒𝚏 (2) [ ___ ]

○ 𝚜𝚌𝚘𝚛𝚎 <= 𝟾𝟿:

○ 𝚜𝚌𝚘𝚛𝚎 < 𝟿𝟶:

○ 𝚜𝚌𝚘𝚛𝚎 > 𝟾𝟶:

○ 𝚜𝚌𝚘𝚛𝚎 >= 𝟾𝟶:

⇥ 𝚐𝚛𝚊𝚍𝚎 = "𝙱"


𝚎𝚕𝚒𝚏 (3) [ ___ ]

○ 𝚜𝚌𝚘𝚛𝚎 >= 𝟽𝟶:

○ 𝚜𝚌𝚘𝚛𝚎 < 𝟾𝟶:

○ 𝚜𝚌𝚘𝚛𝚎 > 𝟽𝟶:

○ 𝚜𝚌𝚘𝚛𝚎 <= 𝟽𝟿:

⇥ 𝚐𝚛𝚊𝚍𝚎 = "𝙲"


𝚎𝚕𝚜𝚎:

⇥ (4) [ ___ ]

○ 𝚜𝚌𝚘𝚛𝚎 <= 𝟽𝟶: 𝚐𝚛𝚊𝚍𝚎 = "𝙵"

○ 𝚐𝚛𝚊𝚍𝚎 = "𝙵"

○ 𝚜𝚌𝚘𝚛𝚎 < 𝟽𝟶: 𝚐𝚛𝚊𝚍𝚎 = "𝙵"

○ 𝚜𝚌𝚘𝚛𝚎 < 𝟼𝟶: 𝚐𝚛𝚊𝚍𝚎 = "𝙵"

𝚛𝚎𝚝𝚞𝚛𝚗 𝚐𝚛𝚊𝚍𝚎

  1. 𝚜𝚌𝚘𝚛𝚎 >= 𝟿𝟶:

  2. 𝚜𝚌𝚘𝚛𝚎 >= 𝟾𝟶:

  3. 𝚜𝚌𝚘𝚛𝚎 >= 𝟽𝟶:

  4. 𝚐𝚛𝚊𝚍𝚎 = "𝙵"

𝚍𝚎𝚏 𝚐𝚛𝚊𝚍𝚎(𝚜𝚌𝚘𝚛𝚎):

𝚒𝚏 𝚜𝚌𝚘𝚛𝚎 >= 𝟿𝟶:

⇥ 𝚐𝚛𝚊𝚍𝚎 = "𝙰"


𝚎𝚕𝚒𝚏 𝚜𝚌𝚘𝚛𝚎 >= 𝟾𝟶:

⇥ 𝚐𝚛𝚊𝚍𝚎 = "𝙱"


𝚎𝚕𝚒𝚏 𝚜𝚌𝚘𝚛𝚎 >= 𝟽𝟶:

⇥ 𝚐𝚛𝚊𝚍𝚎 = "𝙲"


𝚎𝚕𝚜𝚎:

⇥ 𝚐𝚛𝚊𝚍𝚎 = "𝙵"

𝚛𝚎𝚝𝚞𝚛𝚗 𝚐𝚛𝚊𝚍𝚎


This is a straightforward if/elif/else statement. As this deals with analyzing a numeric amount, the largest amount should be accounted for first.

Thus, the if part should have a score 90 or higher.

The first elif should have a score 80 or higher.

The second elif should have a score of 70 or higher.

The else part indicates the F score.

The <= logic does not work for either elif because a score <= 89 could be <= 70 and return a "B" when a "B" was not earned.

For the else part, there is no condition to check.

15
New cards

Analyze the following code:

𝚡 = 𝟽

𝚢 = 𝟺

𝚙𝚛𝚒𝚗𝚝(𝚡 > 𝚢 𝚊𝚗𝚍 𝚡 - 𝚢 >= 𝟸 𝚘𝚛 𝚡 + 𝚢 == 𝟷𝟷 𝚊𝚗𝚍 𝚗𝚘𝚝 𝚡 * 𝚢 > 𝟸𝟻)

Using drag and drop, arrange the sequence of execution order for the print statement.

1 - ?

2 - ?

3 - ?

4 - ?

a. 𝚗𝚘𝚝 𝚡 * 𝚢 > 𝟸𝟻

b. 𝚡 > 𝚢 𝚊𝚗𝚍 𝚡 − 𝚢 >= 𝟸

c. 𝚡 + 𝚢 == 𝟷𝟷 𝚊𝚗𝚍 𝚗𝚘𝚝 𝚡 * 𝚢 > 𝟸𝟻

d. 𝚡 > 𝚢 𝚊𝚗𝚍 𝚡 - 𝚢 >= 𝟸 𝚘𝚛 𝚡 + 𝚢 == 𝟷𝟷 𝚊𝚗𝚍 𝚗𝚘𝚝 𝚡 * 𝚢 > 𝟸𝟻

  1. 𝚗𝚘𝚝 𝚡 * 𝚢 > 𝟸𝟻

  2. 𝚡 > 𝚢 𝚊𝚗𝚍 𝚡 − 𝚢 >= 𝟸

  3. 𝚡 + 𝚢 == 𝟷𝟷 𝚊𝚗𝚍 𝚗𝚘𝚝 𝚡 * 𝚢 > 𝟸𝟻

  4. 𝚡 > 𝚢 𝚊𝚗𝚍 𝚡 - 𝚢 >= 𝟸 𝚘𝚛 𝚡 + 𝚢 == 𝟷𝟷 𝚊𝚗𝚍 𝚗𝚘𝚝 𝚡 * 𝚢 > 𝟸𝟻


Within logical operators, the order of operations is as follows:

  1. 𝚗𝚘𝚝

  2. 𝚊𝚗𝚍

  3. 𝚘𝚛

Thus, for the print statement, the sequence of execution is as follows:

  1. 𝚗𝚘𝚝 𝚡 * 𝚢 > 𝟸𝟻 (False)

  2. 𝚡 > 𝚢 𝚊𝚗𝚍 𝚡 − 𝚢 >= 𝟸 (True)

  3. 𝚡 + 𝚢 == 𝟷𝟷 𝚊𝚗𝚍 𝚗𝚘𝚝 𝚡 * 𝚢 > 𝟸𝟻 (False)

Then the 𝚘𝚛 expression is evaluated 𝚃𝚛𝚞𝚎 or 𝙵𝚊𝚕𝚜𝚎, so the print statement would return 𝚃𝚛𝚞𝚎.

16
New cards

You are trying to loop through some values retrieved from a list. You want the list to keep printing these values, but if the list sees a value of "end of day", then the printing should stop.

Select the missing code examples to finish writing this code block.

𝚜𝚌𝚑𝚎𝚍𝚞𝚕𝚎 = ["𝙾𝚙𝚎𝚗𝚒𝚗𝚐 𝙲𝚘𝚖𝚖𝚎𝚗𝚝𝚜", "𝙱𝚛𝚎𝚊𝚔𝚏𝚊𝚜𝚝", "𝙱𝚛𝚎𝚊𝚔𝚘𝚞𝚝 𝚂𝚎𝚜𝚜𝚒𝚘𝚗 𝟷", "𝙻𝚞𝚗𝚌𝚑", "𝙱𝚛𝚎𝚊𝚔𝚘𝚞𝚝 𝚂𝚎𝚜𝚜𝚒𝚘𝚗 𝟸", "𝙴𝚗𝚍 𝚘𝚏 𝙳𝚊𝚢", "𝙾𝚙𝚎𝚗𝚒𝚗𝚐 𝙲𝚘𝚖𝚖𝚎𝚗𝚝𝚜", "𝙱𝚛𝚎𝚊𝚔𝚏𝚊𝚜𝚝"]

𝚜𝚌𝚑𝚎𝚍𝚞𝚕𝚎𝚍𝙴𝚟𝚎𝚗𝚝 = 𝟶

(1) [ 𝚍𝚘 / 𝚠𝚑𝚒𝚕𝚎 / 𝚏𝚘𝚛 / 𝚒𝚏 ] (𝚜𝚌𝚑𝚎𝚍𝚞𝚕𝚎𝚍𝙴𝚟𝚎𝚗𝚝 < 𝚕𝚎𝚗(𝚜𝚌𝚑𝚎𝚍𝚞𝚕𝚎)):

⇥ 𝚙𝚛𝚒𝚗𝚝(𝚜𝚌𝚑𝚎𝚍𝚞𝚕𝚎[𝚜𝚌𝚑𝚎𝚍𝚞𝚕𝚎𝚍𝙴𝚟𝚎𝚗𝚝])

⇥ 𝚒𝚏 𝚜𝚌𝚑𝚎𝚍𝚞𝚕𝚎[𝚜𝚌𝚑𝚎𝚍𝚞𝚕𝚎𝚍𝙴𝚟𝚎𝚗𝚝] == "𝙴𝚗𝚍 𝚘𝚏 𝙳𝚊𝚢":

⇥ ⇥ (2) [ 𝚜𝚝𝚘𝚙 / 𝚎𝚗𝚍 / 𝚌𝚘𝚗𝚝𝚒𝚗𝚞𝚎 / 𝚋𝚛𝚎𝚊𝚔 ]

⇥ 𝚎𝚕𝚜𝚎:

⇥ ⇥ (3) [ 𝚜𝚌𝚑𝚎𝚍𝚞𝚕𝚎𝚍𝙴𝚟𝚎𝚗𝚝 / 𝚋𝚛𝚎𝚊𝚔 / 𝚌𝚘𝚗𝚝𝚒𝚗𝚞𝚎 / 𝚜𝚌𝚑𝚎𝚍𝚞𝚕𝚎𝚍𝙴𝚟𝚎𝚗𝚝 +=𝟷 ]

  1. 𝚠𝚑𝚒𝚕𝚎

  2. 𝚋𝚛𝚎𝚊𝚔

  3. 𝚜𝚌𝚑𝚎𝚍𝚞𝚕𝚎𝚍𝙴𝚟𝚎𝚗𝚝 +=𝟷

𝚜𝚌𝚑𝚎𝚍𝚞𝚕𝚎 = ["𝙾𝚙𝚎𝚗𝚒𝚗𝚐 𝙲𝚘𝚖𝚖𝚎𝚗𝚝𝚜", "𝙱𝚛𝚎𝚊𝚔𝚏𝚊𝚜𝚝", "𝙱𝚛𝚎𝚊𝚔𝚘𝚞𝚝 𝚂𝚎𝚜𝚜𝚒𝚘𝚗 𝟷", "𝙻𝚞𝚗𝚌𝚑", "𝙱𝚛𝚎𝚊𝚔𝚘𝚞𝚝 𝚂𝚎𝚜𝚜𝚒𝚘𝚗 𝟸", "𝙴𝚗𝚍 𝚘𝚏 𝙳𝚊𝚢", "𝙾𝚙𝚎𝚗𝚒𝚗𝚐 𝙲𝚘𝚖𝚖𝚎𝚗𝚝𝚜", "𝙱𝚛𝚎𝚊𝚔𝚏𝚊𝚜𝚝"]

𝚜𝚌𝚑𝚎𝚍𝚞𝚕𝚎𝚍𝙴𝚟𝚎𝚗𝚝 = 𝟶

𝚠𝚑𝚒𝚕𝚎 (𝚜𝚌𝚑𝚎𝚍𝚞𝚕𝚎𝚍𝙴𝚟𝚎𝚗𝚝 < 𝚕𝚎𝚗(𝚜𝚌𝚑𝚎𝚍𝚞𝚕𝚎)):

𝚙𝚛𝚒𝚗𝚝(𝚜𝚌𝚑𝚎𝚍𝚞𝚕𝚎[𝚜𝚌𝚑𝚎𝚍𝚞𝚕𝚎𝚍𝙴𝚟𝚎𝚗𝚝])

𝚒𝚏 𝚜𝚌𝚑𝚎𝚍𝚞𝚕𝚎[𝚜𝚌𝚑𝚎𝚍𝚞𝚕𝚎𝚍𝙴𝚟𝚎𝚗𝚝] == "𝙴𝚗𝚍 𝚘𝚏 𝙳𝚊𝚢":

⇥ ⇥ 𝚋𝚛𝚎𝚊𝚔

𝚎𝚕𝚜𝚎:

⇥ ⇥ 𝚜𝚌𝚑𝚎𝚍𝚞𝚕𝚎𝚍𝙴𝚟𝚎𝚗𝚝 +=𝟷


Upon further examination of this loop, it should run until a certain value is reached. This indicates the need for a 𝚠𝚑𝚒𝚕𝚎 loop, not a 𝚏𝚘𝚛 loop, as a 𝚏𝚘𝚛 loop runs a set number of times. When the value is reached, the code should break.

And since the code is set to run until the list in iterated through, the counter, 𝚜𝚌𝚑𝚎𝚍𝚞𝚕𝚎𝚍𝙴𝚟𝚎𝚗𝚝, needs to have 𝟷 added to it at the end of each loop. Otherwise, the code could be stuck in an endless loop.

17
New cards

The following code example needs to print the number of minutes per day to walk, starting with 10 minutes a day in week 1 and ending with 50 minutes a day in week 5.

Choose the correct line of code to start this iteration and finish this code example.

(1)

𝚏𝚘𝚛 𝚛𝚊𝚗𝚐𝚎(𝟷, 𝟻)

𝚠𝚑𝚒𝚕𝚎 𝚠𝚎𝚎𝚔 𝚒𝚗 𝚛𝚊𝚗𝚐𝚎(𝟷,𝟼)

𝚏𝚘𝚛 𝚠𝚎𝚎𝚔 𝚒𝚗 𝚛𝚊𝚗𝚐𝚎(𝟷,𝟻)

𝚏𝚘𝚛 𝚠𝚎𝚎𝚔 𝚒𝚗 𝚛𝚊𝚗𝚐𝚎(𝟷,𝟼) :

⇥ 𝚙𝚛𝚒𝚗𝚝(𝚏"𝚈𝚘𝚞 𝚜𝚑𝚘𝚞𝚕𝚍 𝚠𝚊𝚕𝚔 𝚏𝚘𝚛 {𝚠𝚎𝚎𝚔 * 𝟷𝟶} 𝚖𝚒𝚗𝚞𝚝𝚎𝚜 𝚒𝚗 {𝚠𝚎𝚎𝚔}")

𝚏𝚘𝚛 𝚠𝚎𝚎𝚔 𝚒𝚗 𝚛𝚊𝚗𝚐𝚎(𝟷,𝟼) :

𝚏𝚘𝚛 𝚠𝚎𝚎𝚔 𝚒𝚗 𝚛𝚊𝚗𝚐𝚎(𝟷,𝟼) :

⇥ 𝚙𝚛𝚒𝚗𝚝(𝚏"𝚈𝚘𝚞 𝚜𝚑𝚘𝚞𝚕𝚍 𝚠𝚊𝚕𝚔 𝚏𝚘𝚛 {𝚠𝚎𝚎𝚔 * 𝟷𝟶} 𝚖𝚒𝚗𝚞𝚝𝚎𝚜 𝚒𝚗 {𝚠𝚎𝚎𝚔}")


As this loop runs a fixed number of times, a for loop is needed.

Given that for loops are zero-based unless otherwise specified, a range of 1 through 6 needs to be specified as the loop will end before the number 6.

Technically, a while condition could work here, but the while condition specified is incorrect for this code need.

18
New cards

A developer needs to add, to an existing app, code that will print a message a set number of times. However, the developer does not yet know the message but the variables used in the loop for a message are used elsewhere in the app. Which keyword, when added to the end of the code, will serve as a placeholder for future text? Here is the code example:

𝚏𝚘𝚛 𝚡 𝚒𝚗 𝚛𝚊𝚗𝚐𝚎(𝟷,𝟻):

○ 𝚠𝚑𝚒𝚕𝚎

○ 𝚙𝚊𝚜𝚜

○ 𝚌𝚘𝚗𝚝𝚒𝚗𝚞𝚎

○ 𝚋𝚛𝚎𝚊𝚔

𝚙𝚊𝚜𝚜

𝚏𝚘𝚛 𝚡 𝚒𝚗 𝚛𝚊𝚗𝚐𝚎(𝟷,𝟻):

⇥ 𝚙𝚊𝚜𝚜


The 𝚙𝚊𝚜𝚜 keyword is used as a placeholder.

It is commonly used as part of an 𝚒𝚏 /𝚎𝚕𝚒𝚏/𝚎𝚕𝚜𝚎 or loop statement to where one condition temporarily does not need any action taken.

The 𝚋𝚛𝚎𝚊𝚔 keyword exits a loop.

The 𝚠𝚑𝚒𝚕𝚎 keyword starts a loop.

The 𝚌𝚘𝚗𝚝𝚒𝚗𝚞𝚎 keyword skips a turn in a loop.

19
New cards

Complete the code example to build a function that does the following:

  • Its name is calcSubtotal

  • The function takes an amount and a sales tax rate and calculates a subtotal

  • The new subtotal is returned

(1)

○ 𝚏𝚞𝚗𝚌𝚝𝚒𝚘𝚗 𝚌𝚊𝚕𝚌𝚂𝚞𝚋𝚝𝚘𝚝𝚊𝚕

○ 𝚍𝚎𝚏 𝚌𝚊𝚕𝚌𝚂𝚞𝚋𝚝𝚘𝚝𝚊𝚕

○ 𝚌𝚊𝚕𝚌𝚂𝚞𝚋𝚝𝚘𝚝𝚊𝚕

(2)

○ ()

○ ():

○ (𝚊𝚖𝚘𝚞𝚗𝚝, 𝚜𝚊𝚕𝚎𝚜𝚃𝚊𝚡𝚁𝚊𝚝𝚎)

○ (𝚊𝚖𝚘𝚞𝚗𝚝, 𝚜𝚊𝚕𝚎𝚜𝚃𝚊𝚡𝚁𝚊𝚝𝚎):

⇥ 𝚜𝚞𝚋𝚝𝚘𝚝𝚊𝚕 = 𝚊𝚖𝚘𝚞𝚗𝚝 * (𝟷 + 𝚜𝚊𝚕𝚎𝚜𝚃𝚊𝚡𝚁𝚊𝚝𝚎)

⇥ (3)

○ 𝚛𝚎𝚝𝚞𝚛𝚗

○ 𝚛𝚎𝚝𝚞𝚛𝚗 𝚊𝚖𝚘𝚞𝚗𝚝

○ 𝚛𝚎𝚝𝚞𝚛𝚗 𝚜𝚞𝚋𝚝𝚘𝚝𝚊𝚕

○ 𝚛𝚎𝚝𝚞𝚛𝚗 𝚌𝚊𝚕𝚌𝚂𝚞𝚋𝚝𝚘𝚝𝚊𝚕

  1. 𝚍𝚎𝚏 𝚌𝚊𝚕𝚌𝚂𝚞𝚋𝚝𝚘𝚝𝚊𝚕

  2. (𝚊𝚖𝚘𝚞𝚗𝚝, 𝚜𝚊𝚕𝚎𝚜𝚃𝚊𝚡𝚁𝚊𝚝𝚎):

  3. 𝚛𝚎𝚝𝚞𝚛𝚗 𝚜𝚞𝚋𝚝𝚘𝚝𝚊𝚕

𝚍𝚎𝚏 𝚌𝚊𝚕𝚌𝚂𝚞𝚋𝚝𝚘𝚝𝚊𝚕 (𝚊𝚖𝚘𝚞𝚗𝚝, 𝚜𝚊𝚕𝚎𝚜𝚃𝚊𝚡𝚁𝚊𝚝𝚎):

⇥ 𝚜𝚞𝚋𝚝𝚘𝚝𝚊𝚕 = 𝚊𝚖𝚘𝚞𝚗𝚝 * (𝟷 + 𝚜𝚊𝚕𝚎𝚜𝚃𝚊𝚡𝚁𝚊𝚝𝚎)

⇥ 𝚛𝚎𝚝𝚞𝚛𝚗 𝚜𝚞𝚋𝚝𝚘𝚝𝚊𝚕


For Python, the keyword 𝚍𝚎𝚏 indicates a function is going to be built.

Thus, the 𝚍𝚎𝚏 keyword is needed in front of the function name.

Because the function is going to use two parameters in a calculation (𝚊𝚖𝚘𝚞𝚗𝚝 and 𝚜𝚊𝚕𝚎𝚜𝚃𝚊𝚡𝚁𝚊𝚝𝚎) these need to be defined as function parameters, with a : at the end of the 𝚍𝚎𝚏 statement.

The 𝚜𝚞𝚋𝚝𝚘𝚝𝚊𝚕 is the amount calculated, so that value, not the amount or the name of the function, needs to be returned.

20
New cards

Take a look at the following code example, used to calculate a subtotal for an order:

𝚍𝚎𝚏 𝚌𝚊𝚕𝚌𝚃𝚘𝚝𝚊𝚕(𝚝𝚊𝚡𝚊𝚋𝚕𝚎, 𝚊𝚖𝚘𝚞𝚗𝚝, 𝚜𝚊𝚕𝚎𝚜𝚃𝚊𝚡, 𝚜𝚑𝚒𝚙𝚙𝚒𝚗𝚐):|

⇥ 𝚒𝚏 𝚝𝚊𝚡𝚊𝚋𝚕𝚎 == "𝚈𝚎𝚜":

⇥ ⇥ 𝚜𝚞𝚋𝚝𝚘𝚝𝚊𝚕 = 𝚊𝚖𝚘𝚞𝚗𝚝 + (𝟷 * 𝚜𝚊𝚕𝚎𝚜𝚃𝚊𝚡) + 𝚜𝚑𝚒𝚙𝚙𝚒𝚗𝚐

⇥ 𝚎𝚕𝚒𝚏 𝚜𝚑𝚒𝚙𝚙𝚒𝚗𝚐 == 𝟶:

⇥ ⇥ 𝚙𝚊𝚜𝚜

⇥ 𝚎𝚕𝚜𝚎:

⇥ ⇥ 𝚜𝚞𝚋𝚝𝚘𝚝𝚊𝚕 = 𝚊𝚖𝚘𝚞𝚗𝚝 + 𝚜𝚑𝚒𝚙𝚙𝚒𝚗𝚐

⇥ 𝚛𝚎𝚝𝚞𝚛𝚗 𝚜𝚞𝚋𝚝𝚘𝚝𝚊𝚕

𝚘𝚛𝚍𝚎𝚛𝟷 = 𝚌𝚊𝚕𝚌𝚃𝚘𝚝𝚊𝚕("𝙽𝚘", 𝟻𝟶𝟶,.𝟶𝟽, 𝟶)

𝚙𝚛𝚒𝚗𝚝 ("𝚈𝚘𝚞𝚛 𝚘𝚛𝚍𝚎𝚛 𝚝𝚘𝚝𝚊𝚕 𝚒𝚜 ", 𝚘𝚛𝚍𝚎𝚛𝟷)

What is the amount of order1?

An error is raised

500.07

○ 500

○ 535

An error is raised


An error is raised because it's a non-taxable order with 𝚜𝚑𝚒𝚙𝚙𝚒𝚗𝚐 set to 𝟶. With 𝚙𝚊𝚜𝚜, nothing is assigned to 𝚘𝚛𝚍𝚎𝚛𝟷, and the app crashes.

The amount is not 500 as the 𝚜𝚑𝚒𝚙𝚙𝚒𝚗𝚐 being set to 𝟶 invokes the 𝚙𝚊𝚜𝚜 statement. And, the 𝚜𝚊𝚕𝚎𝚜𝚃𝚊𝚡 variable is not used even though it is specified in the function call. The order is not 535 (amount + sales tax) or 500.07 (the sales tax is a percentage, not an amount.)

21
New cards

Take a look at the following function:

𝚍𝚎𝚏 𝚜𝚞𝚋𝚝𝚘𝚝𝚊𝚕(𝚘𝚛𝚍𝚎𝚛_𝚊𝚖𝚝, 𝚜𝚊𝚕𝚎𝚜_𝚝𝚊𝚡):

⇥ 𝚜𝚞𝚋𝚝𝚘𝚝𝚊𝚕 = 𝚏𝚕𝚘𝚊𝚝(𝚘𝚛𝚍𝚎𝚛_𝚊𝚖𝚝) * (𝟷 + 𝚏𝚕𝚘𝚊𝚝(𝚜𝚊𝚕𝚎𝚜_𝚝𝚊𝚡))

⇥ 𝚛𝚎𝚝𝚞𝚛𝚗 𝚜𝚞𝚋𝚝𝚘𝚝𝚊𝚕

Which code example properly calls the function and returns a calculation, stored in a variable called 𝚘𝚛𝚍𝚎𝚛_𝚝𝚘𝚝𝚊𝚕?

○ 𝚘𝚛𝚍𝚎𝚛_𝚝𝚘𝚝𝚊𝚕 = 𝚌𝚊𝚕𝚕 𝚜𝚞𝚋𝚝𝚘𝚝𝚊𝚕(𝟻𝟶𝟶,.𝟶𝟽)

○ 𝚘𝚛𝚍𝚎𝚛_𝚝𝚘𝚝𝚊𝚕(𝚜𝚞𝚋𝚝𝚘𝚝𝚊𝚕(𝟻𝟶𝟶,.𝟶𝟽))

○ 𝚘𝚛𝚍𝚎𝚛_𝚝𝚘𝚝𝚊𝚕 = 𝚜𝚞𝚋𝚝𝚘𝚝𝚊𝚕(𝟻𝟶𝟶,.𝟶𝟽)

○ 𝚘𝚛𝚍𝚎𝚛_𝚝𝚘𝚝𝚊𝚕 = 𝚍𝚎𝚏 𝚜𝚞𝚋𝚝𝚘𝚝𝚊𝚕(𝟻𝟶𝟶,.𝟶𝟽)

𝚘𝚛𝚍𝚎𝚛_𝚝𝚘𝚝𝚊𝚕 = 𝚜𝚞𝚋𝚝𝚘𝚝𝚊𝚕(𝟻𝟶𝟶,.𝟶𝟽)


A variable can be declared at any time with the value being the result of a function.

Thus, 𝚘𝚛𝚍𝚎𝚛_𝚝𝚘𝚝𝚊𝚕 = 𝚜𝚞𝚋𝚝𝚘𝚝𝚊𝚕(𝟻𝟶𝟶,.𝟶𝟽) is correct code. Without the = , the code is incorrect.

The 𝚌𝚊𝚕𝚕 keyword is not needed.

The 𝚍𝚎𝚏 keyword is used to define a function, not call it.

22
New cards

In Python, which character(s) should be placed before text in a line of code to make that line of code a comment?

○ '

○ /*

○ #

○ //

#


The # initiates a comment in Python.

The // characters initiate a comment in many other programming languages, but not Python.

The /* initiates a comment in HTML code.

The ' initiates a comment in Visual Basic.

23
New cards

Look at the following code example and then indicate which symbol is needed in the missing area to have the print statement for the sales representative output on a single line.

𝚒𝚖𝚙𝚘𝚛𝚝 𝚜𝚝𝚛𝚒𝚗𝚐

𝚊𝚗𝚗𝚞𝚊𝚕𝚂𝚊𝚕𝚎𝚜 = 𝟻𝟶𝟶𝟶𝟶𝟶

𝚒𝚏 𝚊𝚗𝚗𝚞𝚊𝚕𝚂𝚊𝚕𝚎𝚜 >= 𝟽𝟶𝟶𝟶𝟶𝟶:

⇥ 𝚙𝚛𝚒𝚗𝚝("𝙶𝚛𝚎𝚊𝚝 𝚢𝚎𝚊𝚛")

𝚎𝚕𝚒𝚏 𝚊𝚗𝚗𝚞𝚊𝚕𝚂𝚊𝚕𝚎𝚜 >= 𝟹𝟶𝟶𝟶𝟶𝟶:

⇥ 𝚙𝚛𝚒𝚗𝚝("𝙳𝚎𝚌𝚎𝚗𝚝 𝚢𝚎𝚊𝚛")

𝚎𝚕𝚜𝚎:

⇥ 𝚙𝚛𝚒𝚗𝚝("𝙱𝚎𝚝𝚝𝚎𝚛 𝚕𝚞𝚌𝚔 𝚗𝚎𝚡𝚝 𝚢𝚎𝚊𝚛")

𝚙𝚛𝚒𝚗𝚝("𝚃𝚑𝚊𝚗𝚔 𝚢𝚘𝚞 𝚏𝚘𝚛 𝚢𝚘𝚞𝚛 𝚎𝚏𝚏𝚘𝚛𝚝𝚜")

𝚙𝚛𝚒𝚗𝚝(𝚏"𝚈𝚘𝚞𝚛 𝚜𝚊𝚕𝚎𝚜 𝚛𝚎𝚙𝚛𝚎𝚜𝚎𝚗𝚝𝚊𝚝𝚒𝚟𝚎 𝚒𝚜 𝙽𝚒𝚌𝚘𝚕𝚎, 𝚢𝚘𝚞 𝚊𝚛𝚎 𝚒𝚗 𝚝𝚑𝚎 𝙴𝚊𝚜𝚝 𝚛𝚎𝚐𝚒𝚘𝚗, " [ \ / \\ / \𝚗 / \𝚝 ]

"𝚊𝚗𝚍 𝚢𝚘𝚞 𝚊𝚛𝚎 𝚋𝚊𝚜𝚎𝚍 𝚒𝚗 𝚝𝚑𝚎 𝙿𝚘𝚝𝚘𝚖𝚊𝚌 𝚘𝚏𝚏𝚒𝚌𝚎.")

\

𝚒𝚖𝚙𝚘𝚛𝚝 𝚜𝚝𝚛𝚒𝚗𝚐

𝚊𝚗𝚗𝚞𝚊𝚕𝚂𝚊𝚕𝚎𝚜 = 𝟻𝟶𝟶𝟶𝟶𝟶

𝚒𝚏 𝚊𝚗𝚗𝚞𝚊𝚕𝚂𝚊𝚕𝚎𝚜 >= 𝟽𝟶𝟶𝟶𝟶𝟶:

⇥ 𝚙𝚛𝚒𝚗𝚝("𝙶𝚛𝚎𝚊𝚝 𝚢𝚎𝚊𝚛")

𝚎𝚕𝚒𝚏 𝚊𝚗𝚗𝚞𝚊𝚕𝚂𝚊𝚕𝚎𝚜 >= 𝟹𝟶𝟶𝟶𝟶𝟶:

⇥ 𝚙𝚛𝚒𝚗𝚝("𝙳𝚎𝚌𝚎𝚗𝚝 𝚢𝚎𝚊𝚛")

𝚎𝚕𝚜𝚎:

⇥ 𝚙𝚛𝚒𝚗𝚝("𝙱𝚎𝚝𝚝𝚎𝚛 𝚕𝚞𝚌𝚔 𝚗𝚎𝚡𝚝 𝚢𝚎𝚊𝚛")

𝚙𝚛𝚒𝚗𝚝("𝚃𝚑𝚊𝚗𝚔 𝚢𝚘𝚞 𝚏𝚘𝚛 𝚢𝚘𝚞𝚛 𝚎𝚏𝚏𝚘𝚛𝚝𝚜")

𝚙𝚛𝚒𝚗𝚝(𝚏"𝚈𝚘𝚞𝚛 𝚜𝚊𝚕𝚎𝚜 𝚛𝚎𝚙𝚛𝚎𝚜𝚎𝚗𝚝𝚊𝚝𝚒𝚟𝚎 𝚒𝚜 𝙽𝚒𝚌𝚘𝚕𝚎, 𝚢𝚘𝚞 𝚊𝚛𝚎 𝚒𝚗 𝚝𝚑𝚎 𝙴𝚊𝚜𝚝 𝚛𝚎𝚐𝚒𝚘𝚗, " \

"𝚊𝚗𝚍 𝚢𝚘𝚞 𝚊𝚛𝚎 𝚋𝚊𝚜𝚎𝚍 𝚒𝚗 𝚝𝚑𝚎 𝙿𝚘𝚝𝚘𝚖𝚊𝚌 𝚘𝚏𝚏𝚒𝚌𝚎."


In Python, the \ symbol tells Python "continue the code on the line below on the current line."

\𝚗 tells Python to print a new line.

\𝚝 tells Python to insert a tab.

\\ tells Python to use the \ character.

24
New cards

Using drag and drop, drag over the lines of code that will accomplish the following:

  • Open the shirts file in a mode to where it cannot be written to

  • Read the contents of the entire file

  • Print the contents of the entire file

Not every line of code will be used.

Line 1 - ?

Line 2 - ?

Line 3 - ?

a. print(shirtFileContents)

b. shirtFileContents = shirtFile.read()

c. for shirtLines = range(shirtFile):

d. shirtFile = open("shirts.txt","a")

e. shirtFile = open("shirts.txt","r")

Line 1 - 𝚜𝚑𝚒𝚛𝚝𝙵𝚒𝚕𝚎 = 𝚘𝚙𝚎𝚗("𝚜𝚑𝚒𝚛𝚝𝚜.𝚝𝚡𝚝", "𝚛")

Line 2 - 𝚜𝚑𝚒𝚛𝚝𝙵𝚒𝚕𝚎𝙲𝚘𝚗𝚝𝚎𝚗𝚝𝚜 = 𝚜𝚑𝚒𝚛𝚝𝙵𝚒𝚕𝚎.𝚛𝚎𝚊𝚍()

Line 3 - 𝚙𝚛𝚒𝚗𝚝(𝚜𝚑𝚒𝚛𝚝𝙵𝚒𝚕𝚎𝙲𝚘𝚗𝚝𝚎𝚗𝚝𝚜)


First, the file needs to be opened in read mode. The open command with the "r" at the end satisfies this need.

Then, the contents of the file need to be read. The read method accomplishes this.

Finally, the print command prints the contents of the file, after the contents have been read.

Opening a file with the "a"; attribute opens it in append mode, not read mode.

The read() method reads the entire file, so there is no need for a for loop.

25
New cards

A developer is starting to build a unit test and needs help figuring out the built-in module that is needed for the test, the way to check to see if the unit test is run as the main module, and the actual unit test to run to see if two values are equal to each other.

Using the dropdown arrows, fill in the code needed to finish building the unit test.

𝚒𝚖𝚙𝚘𝚛𝚝 𝚞𝚗𝚒𝚝𝚝𝚎𝚜𝚝

𝚊 = 𝟹

𝚋 = 𝟼

𝚌𝚕𝚊𝚜𝚜 𝚃𝚎𝚜𝚝_𝙴𝚡𝚊𝚖𝚙𝚕𝚎_𝚄𝚗𝚒𝚝𝚃𝚎𝚜𝚝(𝚞𝚗𝚒𝚝𝚝𝚎𝚜𝚝. (1) [ 𝚃𝚎𝚜𝚝𝙲𝚊𝚜𝚎 / 𝚃𝚎𝚜𝚝 / 𝙰𝚜𝚜𝚎𝚛𝚝 ]):

𝚍𝚎𝚏 𝚝𝚎𝚜𝚝_𝚎𝚚𝚞𝚊𝚕(𝚜𝚎𝚕𝚏):

𝚜𝚎𝚕𝚏. (2) [ 𝚊𝚜𝚜𝚎𝚛𝚝𝙴𝚚𝚞𝚊𝚕 / 𝚊𝚜𝚜𝚎𝚛𝚝𝚃𝚛𝚞𝚎 / 𝚊𝚜𝚜𝚎𝚛𝚝𝙸𝚗 ] (𝟸 + 𝟾, 𝚊 + 𝚋)

𝚒𝚏 (3) [ __𝚗𝚊𝚖𝚎__ / _𝚗𝚊𝚖𝚎_ / 𝚗𝚊𝚖𝚎 ] == '__𝚖𝚊𝚒𝚗__':

𝚞𝚗𝚒𝚝𝚝𝚎𝚜𝚝.𝚖𝚊𝚒𝚗()

  1. 𝚃𝚎𝚜𝚝𝙲𝚊𝚜𝚎

  2. 𝚊𝚜𝚜𝚎𝚛𝚝𝙴𝚚𝚞𝚊𝚕

  3. __𝚗𝚊𝚖𝚎__


A class that builds a unittest passes through the 𝚞𝚗𝚒𝚝𝚝𝚎𝚜𝚝.𝚃𝚎𝚜𝚝𝙲𝚊𝚜𝚎 method.

The __𝚗𝚊𝚖𝚎__ status is checked to see if a test is the main test, and if so, the main method is run.

The 𝚊𝚜𝚜𝚎𝚛𝚝𝙴𝚚𝚞𝚊𝚕 test tests to see if two values are equal.

26
New cards

In the following code example, add the necessary code tell Python to read and print the text file being opened, if the file already exists.

If the file does not exist, print a message indicating as such.

𝚒𝚖𝚙𝚘𝚛𝚝 𝚘𝚜.𝚙𝚊𝚝𝚑

𝚒𝚏 𝚘𝚜.𝚙𝚊𝚝𝚑. (1) [ 𝚎𝚡𝚒𝚜𝚝𝚜 / 𝚏𝚒𝚕𝚎.𝚎𝚡𝚒𝚜𝚝𝚜 / 𝚏𝚒𝚕𝚎 ] ('𝚠𝚘𝚛𝚔.𝚝𝚡𝚝') == 𝚃𝚛𝚞𝚎:

⇥ 𝚠𝚘𝚛𝚔𝙵𝚒𝚕𝚎 = 𝚘𝚙𝚎𝚗('𝚠𝚘𝚛𝚔.𝚝𝚡𝚝', '𝚛')

⇥ 𝚠𝚘𝚛𝚔𝙵𝚒𝚕𝚎𝙵𝚒𝚛𝚜𝚝𝙻𝚒𝚗𝚎 = (2) [ 𝚠𝚘𝚛𝚔𝙵𝚒𝚕𝚎.𝚛𝚎𝚊𝚍𝚏𝚒𝚛𝚜𝚝𝚕𝚒𝚗𝚎() / 𝚠𝚘𝚛𝚔𝙵𝚒𝚕𝚎.𝚛𝚎𝚊𝚍() / 𝚠𝚘𝚛𝚔𝙵𝚒𝚕𝚎.𝚜𝚎𝚎𝚔() / 𝚠𝚘𝚛𝚔𝙵𝚒𝚕𝚎.𝚛𝚎𝚊𝚍𝚏𝚒𝚛𝚜𝚝() ]

𝚎𝚕𝚜𝚎:

⇥ 𝚙𝚛𝚒𝚗𝚝("𝚃𝚑𝚎 𝚠𝚘𝚛𝚔 𝚏𝚒𝚕𝚎 𝚒𝚜 𝚗𝚘𝚝 𝚒𝚗 𝚝𝚑𝚒𝚜 𝚏𝚘𝚕𝚍𝚎𝚛.")

  1. 𝚎𝚡𝚒𝚜𝚝𝚜

  2. 𝚠𝚘𝚛𝚔𝙵𝚒𝚕𝚎.𝚛𝚎𝚊𝚍()

𝚒𝚖𝚙𝚘𝚛𝚝 𝚘𝚜.𝚙𝚊𝚝𝚑

𝚒𝚏 𝚘𝚜.𝚙𝚊𝚝𝚑.𝚎𝚡𝚒𝚜𝚝𝚜('𝚠𝚘𝚛𝚔.𝚝𝚡𝚝') == 𝚃𝚛𝚞𝚎:

⇥ 𝚠𝚘𝚛𝚔𝙵𝚒𝚕𝚎 = 𝚘𝚙𝚎𝚗('𝚠𝚘𝚛𝚔.𝚝𝚡𝚝', '𝚛')

⇥ 𝚠𝚘𝚛𝚔𝙵𝚒𝚕𝚎𝙵𝚒𝚛𝚜𝚝𝙻𝚒𝚗𝚎 = 𝚠𝚘𝚛𝚔𝙵𝚒𝚕𝚎.𝚛𝚎𝚊𝚍()

𝚎𝚕𝚜𝚎:

⇥ 𝚙𝚛𝚒𝚗𝚝("𝚃𝚑𝚎 𝚠𝚘𝚛𝚔 𝚏𝚒𝚕𝚎 𝚒𝚜 𝚗𝚘𝚝 𝚒𝚗 𝚝𝚑𝚒𝚜 𝚏𝚘𝚕𝚍𝚎𝚛.")


In Python, once the os.path module is imported, the exists function can check to see if a file exists.

The read() command reads the contents of a file.

The seek() command seeks a specified character index within text.

The readline() command reads a line of text within a file.

27
New cards

A junior Python programmer wants to have a user input a location and then have the location print on the screen. In addition, the developer wants to tell the user to enter North, South, East, or West for a location and have the user try again if a proper location is not entered or print the location if the location is North, South, East, or West.

Using drag and drop, drag over the lines of code necessary to accomplish this. Location should be the first variable declared. Not every line of code will be used.

Line 1 - ?

Line 2 - ?

Line 3 - ?

Line 4 - ?

Line 5 - ?

Line 6 - ?

a. 𝚠𝚑𝚒𝚕𝚎 𝚛𝚎𝚜𝚙𝚘𝚗𝚜𝚎 𝚗𝚘𝚝 𝚒𝚗 𝚕𝚘𝚌𝚊𝚝𝚒𝚘𝚗:

b. 𝚛𝚎𝚜𝚙𝚘𝚗𝚜𝚎 = 𝚒𝚗𝚙𝚞𝚝("𝙴𝚗𝚝𝚎𝚛 𝙽𝚘𝚛𝚝𝚑, 𝚂𝚘𝚞𝚝𝚑, 𝚆𝚎𝚜𝚝, 𝚘𝚛 𝙴𝚊𝚜𝚝 𝚏𝚘𝚛 𝚊 𝚕𝚘𝚌𝚊𝚝𝚒𝚘𝚗.")

c. 𝚙𝚛𝚒𝚗𝚝(𝚛𝚎𝚜𝚙𝚘𝚗𝚜𝚎)

d. 𝚕𝚘𝚌𝚊𝚝𝚒𝚘𝚗 = ["𝙽𝚘𝚛𝚝𝚑", "𝚂𝚘𝚞𝚝𝚑", "𝚆𝚎𝚜𝚝", "𝙴𝚊𝚜𝚝"]

e. 𝚙𝚛𝚒𝚗𝚝("𝚃𝚛𝚢 𝚊𝚐𝚊𝚒𝚗.")

Line 1 - d

Line 2 - b

Line 3 - a

Line 4 - e

Line 5 - b

Line 6 - c

𝚕𝚘𝚌𝚊𝚝𝚒𝚘𝚗 = ["𝙽𝚘𝚛𝚝𝚑", "𝚂𝚘𝚞𝚝𝚑", "𝚆𝚎𝚜𝚝", "𝙴𝚊𝚜𝚝"]

𝚛𝚎𝚜𝚙𝚘𝚗𝚜𝚎 = 𝚒𝚗𝚙𝚞𝚝("𝙴𝚗𝚝𝚎𝚛 𝙽𝚘𝚛𝚝𝚑, 𝚂𝚘𝚞𝚝𝚑, 𝚆𝚎𝚜𝚝, 𝚘𝚛 𝙴𝚊𝚜𝚝 𝚏𝚘𝚛 𝚊 𝚕𝚘𝚌𝚊𝚝𝚒𝚘𝚗.")

𝚠𝚑𝚒𝚕𝚎 𝚛𝚎𝚜𝚙𝚘𝚗𝚜𝚎 𝚗𝚘𝚝 𝚒𝚗 𝚕𝚘𝚌𝚊𝚝𝚒𝚘𝚗:

⇥ 𝚙𝚛𝚒𝚗𝚝("𝚃𝚛𝚢 𝚊𝚐𝚊𝚒𝚗.")

⇥ 𝚛𝚎𝚜𝚙𝚘𝚗𝚜𝚎 = 𝚒𝚗𝚙𝚞𝚝("𝙴𝚗𝚝𝚎𝚛 𝙽𝚘𝚛𝚝𝚑, 𝚂𝚘𝚞𝚝𝚑, 𝚆𝚎𝚜𝚝, 𝚘𝚛 𝙴𝚊𝚜𝚝 𝚏𝚘𝚛 𝚊 𝚕𝚘𝚌𝚊𝚝𝚒𝚘𝚗.")

𝚙𝚛𝚒𝚗𝚝(𝚛𝚎𝚜𝚙𝚘𝚗𝚜𝚎)


The wanted locations can be stored in a list and the location list is used to store values for locations.

The response statement is an input statement asking for a location.

The while loop checks to see if a proper location was entered. If not, the user is told to try again.

28
New cards

Using the dropdown arrow, add the symbol needed to get the number of items to print instead of {items}.

𝚃𝚑𝚎 𝚏𝚘𝚕𝚕𝚘𝚠𝚒𝚗𝚐 𝚌𝚘𝚍𝚎 𝚎𝚡𝚊𝚖𝚙𝚕𝚎, 𝚊𝚜 𝚒𝚜, 𝚙𝚛𝚒𝚗𝚝𝚜 "𝚈𝚘𝚞 𝚑𝚊𝚟𝚎 {𝚒𝚝𝚎𝚖𝚜} 𝚒𝚝𝚎𝚖𝚜 𝚒𝚗 𝚜𝚝𝚘𝚌𝚔" 𝚒𝚗𝚜𝚝𝚎𝚊𝚍 𝚘𝚏 𝚝𝚑𝚎 𝚗𝚞𝚖𝚋𝚎𝚛 𝚘𝚏 𝚒𝚝𝚎𝚖𝚜 𝚒𝚗 𝚜𝚝𝚘𝚌𝚔.

𝚙𝚛𝚒𝚌𝚎 = 𝟿.𝟿𝟻

𝚒𝚝𝚎𝚖𝚜= 𝟷𝟻

𝚙𝚛𝚒𝚗𝚝( [ 𝚏 / \ / 𝚏𝚘𝚛𝚖𝚊𝚝 / % ] "𝚆𝚎 𝚑𝚊𝚟𝚎 {𝚒𝚝𝚎𝚖𝚜} 𝚒𝚝𝚎𝚖𝚜 𝚒𝚗 𝚜𝚝𝚘𝚌𝚔.")

𝚏

𝚙𝚛𝚒𝚌𝚎 = 𝟿.𝟿𝟻

𝚒𝚝𝚎𝚖𝚜= 𝟷𝟻

𝚙𝚛𝚒𝚗𝚝(𝚏"𝚆𝚎 𝚑𝚊𝚟𝚎 {𝚒𝚝𝚎𝚖𝚜} 𝚒𝚝𝚎𝚖𝚜 𝚒𝚗 𝚜𝚝𝚘𝚌𝚔.")


The letter 𝚏 tells Python that the following text will be formatted text and, specifically, that the text in {} should refer to a variable.

\ is an escape character.

% is used for other formatting purposes.

The keyword "format" is not needed here.

29
New cards

Evaluate the following code that writes a message to an existing log file at the start of each day:

𝚠𝚒𝚝𝚑 𝚘𝚙𝚎𝚗('𝚕𝚘𝚐.𝚝𝚡𝚝', '𝚠') 𝚊𝚜 𝚏𝚒𝚕𝚎:

⇥ 𝚏𝚒𝚕𝚎.𝚠𝚛𝚒𝚝𝚎('𝙳𝚊𝚒𝚕𝚢 𝙻𝚘𝚐')

⇥ 𝚏𝚒𝚕𝚎.𝚌𝚕𝚘𝚜𝚎()

Which statements about the code are true? (Choose two)

☐ The close function is not needed.

☐ A file.open() statement needs to be added at the beginning of the with statement.

☐ The log file is overwritten each time it is opened.

☐ The text in the write function adds itself to the end of the log file.

The close function is not needed.

The log file is overwritten each time it is opened.


The 'w' within the open function writes to a file, and, if the file exists, overwrites any content within the file. An 'a' attribute would be needed to append the text in the write function to the existing file. And, a with statement, when done, removes a file from memory automatically, so file.close() is not needed.

30
New cards

When using the sys.argv[0] command as part of running a Python app in a command prompt, what does the 0 represent in sys.argv?

○ The first function in the file

○ The file name

○ The main class in the file

○ The first argument in a function

The file name


sys.argv[0] represents the file name within a Python command. Other sys.argv values can represent a function, class, or argument passed through a function, but 0 always represents the file name.

31
New cards

Take a look at the following code example:

𝚌𝚊𝚛𝙻𝚘𝚊𝚗 = 𝟸𝟶𝟶𝟶𝟶

𝚒𝚗𝚝𝚁𝚊𝚝𝚎 = 𝟷.𝟶𝟻

𝚕𝚒𝚌𝚎𝚗𝚜𝚎𝙵𝚎𝚎 = 𝟻𝟶𝟶

𝚝𝚘𝚝𝚊𝚕𝙻𝚘𝚊𝚗 = 𝚌𝚊𝚛𝙻𝚘𝚊𝚗 + 𝚕𝚒𝚌𝚎𝚗𝚜𝚎𝙵𝚎𝚎 * 𝚒𝚗𝚝𝚁𝚊𝚝𝚎

A user of the app with this code is complaining that the total loan amount is far lower than what is anticipated. Choose the line of code that needs to be adjusted and the adjusted code to make the code calculate the loan total based off the interest rate being applied to the car loan and the license fee.

○ 𝚝𝚘𝚝𝚊𝚕𝙻𝚘𝚊𝚗 = 𝚌𝚊𝚛𝙻𝚘𝚊𝚗 + (𝚕𝚒𝚌𝚎𝚗𝚜𝚎𝙵𝚎𝚎 * 𝚒𝚗𝚝𝚁𝚊𝚝𝚎)

○ 𝚝𝚘𝚝𝚊𝚕𝙻𝚘𝚊𝚗 = 𝚌𝚊𝚛𝚕𝚘𝚊𝚗 - 𝚕𝚒𝚌𝚎𝚗𝚜𝚎𝙵𝚎𝚎 * 𝚒𝚗𝚝𝚁𝚊𝚝𝚎

○ 𝚝𝚘𝚝𝚊𝚕𝙻𝚘𝚊𝚗 = 𝚌𝚊𝚛𝙻𝚘𝚊𝚗 + 𝚕𝚒𝚌𝚎𝚗𝚜𝚎𝙵𝚎𝚎 / 𝚒𝚗𝚝𝚁𝚊𝚝𝚎

○ 𝚝𝚘𝚝𝚊𝚕𝙻𝚘𝚊𝚗 = (𝚌𝚊𝚛𝙻𝚘𝚊𝚗 + 𝚕𝚒𝚌𝚎𝚗𝚜𝚎𝙵𝚎𝚎) * 𝚒𝚗𝚝𝚁𝚊𝚝𝚎

𝚝𝚘𝚝𝚊𝚕𝙻𝚘𝚊𝚗 = (𝚌𝚊𝚛𝙻𝚘𝚊𝚗 + 𝚕𝚒𝚌𝚎𝚗𝚜𝚎𝙵𝚎𝚎) * 𝚒𝚗𝚝𝚁𝚊𝚝𝚎

𝚌𝚊𝚛𝙻𝚘𝚊𝚗 = 𝟸𝟶𝟶𝟶𝟶

𝚒𝚗𝚝𝚁𝚊𝚝𝚎 =.𝟶𝟻

𝚕𝚒𝚌𝚎𝚗𝚜𝚎𝙵𝚎𝚎 = 𝟻𝟶𝟶

𝚝𝚘𝚝𝚊𝚕𝙻𝚘𝚊𝚗 = (𝚌𝚊𝚛𝙻𝚘𝚊𝚗 + 𝚕𝚒𝚌𝚎𝚗𝚜𝚎𝙵𝚎𝚎) * 𝚒𝚗𝚝𝚁𝚊𝚝𝚎


This is clearly a logic error as the totalLoan variable is a victim of order of operations.

The intRate is only being calculated on the license fee, not the car loan plus the license fee.

To fix this logic error, parentheses should be added around the carLoan + licenseFee portion of the code.

Remember that multiplication will happen before addition in order of operations, hence the current problem with the intRate only being calculated on the license fee.

32
New cards

Evaluate the following anatomy of a unit test, part of a larger block of code.

𝚍𝚎𝚏 𝚝𝚎𝚜𝚝_𝚖𝚎𝚖𝚘𝚛𝚢(𝚜𝚎𝚕𝚏):

⇥ 𝚊 = 𝟹

⇥ 𝚋 = 𝚊

⇥ #𝚒𝚗𝚜𝚎𝚛𝚝 𝚊𝚜𝚜𝚎𝚛𝚝 𝚝𝚎𝚜𝚝 𝚑𝚎𝚛𝚎

Which statement will test to see if a and b share the same memory space?

○ 𝚜𝚎𝚕𝚏.𝚊𝚜𝚜𝚎𝚛𝚝𝚃𝚛𝚞𝚎(𝚊,𝚋)

○ 𝚜𝚎𝚕𝚏.𝚊𝚜𝚜𝚎𝚛𝚝𝙴𝚚𝚞𝚊𝚕(𝚊,𝚋)

○ 𝚜𝚎𝚕𝚏.𝚊𝚜𝚜𝚎𝚛𝚝𝙸𝚗(𝚊,𝚋)

○ 𝚜𝚎𝚕𝚏.𝚊𝚜𝚜𝚎𝚛𝚝𝙸𝚜(𝚊,𝚋)

𝚜𝚎𝚕𝚏.𝚊𝚜𝚜𝚎𝚛𝚝𝙸𝚜(𝚊,𝚋)


self.assertIs(a,b) tests to see if a and b are in the same memory space.

self.assertEqual(a,b) would test to see if a equals b, not if they share the same memory space.

self.assertTrue(a == b) tests a statement, not two values compared to each other.

self.assertIn(a,b) would only apply if b were a group of values and then a can be tested to see if it's in the b group.

33
New cards

The following code example does a simple calculation to divide two numbers. You want to handle any errors gracefully and ensure that the last print statement always prints, even if there is an error.

Using the dropdown arrows, fill in the missing keywords in the code to accomplish this.

(1) [ 𝚝𝚛𝚢: / 𝚎𝚕𝚜𝚎: / 𝚎𝚡𝚌𝚎𝚙𝚝: / 𝚏𝚒𝚗𝚊𝚕𝚕𝚢: ]

⇥ 𝚡 = 𝚏𝚕𝚘𝚊𝚝(𝚒𝚗𝚙𝚞𝚝("𝙴𝚗𝚝𝚎𝚛 𝚊 𝚗𝚞𝚖𝚋𝚎𝚛. "))

⇥ 𝚢 = 𝚏𝚕𝚘𝚊𝚝(𝚒𝚗𝚙𝚞𝚝("𝙴𝚗𝚝𝚎𝚛 𝚊 𝚗𝚞𝚖𝚋𝚎𝚛 𝚝𝚘 𝚍𝚒𝚟𝚒𝚍𝚎 𝚋𝚢. "))

⇥ 𝚙𝚛𝚒𝚗𝚝(𝚏"𝚃𝚑𝚎 𝚊𝚗𝚜𝚠𝚎𝚛 𝚒𝚜 {𝚡/𝚢}.")

(2) [ 𝚝𝚛𝚢: / 𝚎𝚕𝚜𝚎: / 𝚎𝚡𝚌𝚎𝚙𝚝: / 𝚏𝚒𝚗𝚊𝚕𝚕𝚢: ]

⇥ 𝚙𝚛𝚒𝚗𝚝("𝚄𝚑 𝚘𝚑. 𝙳𝚒𝚍 𝚢𝚘𝚞 𝚎𝚗𝚝𝚎𝚛 𝚜𝚘𝚖𝚎𝚝𝚑𝚒𝚗𝚐 𝚋𝚎𝚜𝚒𝚍𝚎𝚜 𝚊 𝚗𝚞𝚖𝚋𝚎𝚛? 𝙳𝚒𝚍 𝚢𝚘𝚞 𝚝𝚛𝚢 𝚝𝚘 𝚍𝚒𝚟𝚒𝚍𝚎 𝚋𝚢 𝚣𝚎𝚛𝚘?")

(3) [ 𝚝𝚛𝚢: / 𝚎𝚕𝚜𝚎: / 𝚎𝚡𝚌𝚎𝚙𝚝: / 𝚏𝚒𝚗𝚊𝚕𝚕𝚢: ]

⇥ 𝚙𝚛𝚒𝚗𝚝("𝚈𝚘𝚞 𝚜𝚞𝚌𝚌𝚎𝚜𝚜𝚏𝚞𝚕𝚕𝚢 𝚙𝚕𝚊𝚢𝚎𝚍 𝚝𝚑𝚎 𝚍𝚒𝚟𝚒𝚜𝚒𝚘𝚗 𝚐𝚊𝚖𝚎.")

(4) [ 𝚝𝚛𝚢: / 𝚎𝚕𝚜𝚎: / 𝚎𝚡𝚌𝚎𝚙𝚝: / 𝚏𝚒𝚗𝚊𝚕𝚕𝚢: ]

⇥𝚙𝚛𝚒𝚗𝚝("𝚃𝚑𝚊𝚗𝚔 𝚢𝚘𝚞 𝚏𝚘𝚛 𝚙𝚕𝚊𝚢𝚒𝚗𝚐.")

  1. 𝚝𝚛𝚢:

  2. 𝚎𝚡𝚌𝚎𝚙𝚝:

  3. 𝚎𝚕𝚜𝚎:

  4. 𝚏𝚒𝚗𝚊𝚕𝚕𝚢:


𝚝𝚛𝚢:

𝚡 = 𝚏𝚕𝚘𝚊𝚝(𝚒𝚗𝚙𝚞𝚝("𝙴𝚗𝚝𝚎𝚛 𝚊 𝚗𝚞𝚖𝚋𝚎𝚛. "))

𝚢 = 𝚏𝚕𝚘𝚊𝚝(𝚒𝚗𝚙𝚞𝚝("𝙴𝚗𝚝𝚎𝚛 𝚊 𝚗𝚞𝚖𝚋𝚎𝚛 𝚝𝚘 𝚍𝚒𝚟𝚒𝚍𝚎 𝚋𝚢. "))

𝚙𝚛𝚒𝚗𝚝(𝚏"𝚃𝚑𝚎 𝚊𝚗𝚜𝚠𝚎𝚛 𝚒𝚜 {𝚡/𝚢}.")

𝚎𝚡𝚌𝚎𝚙𝚝:

𝚙𝚛𝚒𝚗𝚝("𝚄𝚑 𝚘𝚑. 𝙳𝚒𝚍 𝚢𝚘𝚞 𝚎𝚗𝚝𝚎𝚛 𝚜𝚘𝚖𝚎𝚝𝚑𝚒𝚗𝚐 𝚋𝚎𝚜𝚒𝚍𝚎𝚜 𝚊 𝚗𝚞𝚖𝚋𝚎𝚛? 𝙳𝚒𝚍 𝚢𝚘𝚞 𝚝𝚛𝚢 𝚝𝚘 𝚍𝚒𝚟𝚒𝚍𝚎 𝚋𝚢 𝚣𝚎𝚛𝚘?")

𝚎𝚕𝚜𝚎:

𝚙𝚛𝚒𝚗𝚝("𝚈𝚘𝚞 𝚜𝚞𝚌𝚌𝚎𝚜𝚜𝚏𝚞𝚕𝚕𝚢 𝚙𝚕𝚊𝚢𝚎𝚍 𝚝𝚑𝚎 𝚍𝚒𝚟𝚒𝚜𝚒𝚘𝚗 𝚐𝚊𝚖𝚎.")

𝚏𝚒𝚗𝚊𝚕𝚕𝚢:

𝚙𝚛𝚒𝚗𝚝("𝚃𝚑𝚊𝚗𝚔 𝚢𝚘𝚞 𝚏𝚘𝚛 𝚙𝚕𝚊𝚢𝚒𝚗𝚐.")


The code that is being attempted, the division, should be wrapped in a try statement.

The error message needs to be wrapped in an except statement.

The else statement shows if the try was successful.

The code that needs to run at the end needs to be wrapped in a finally statement.

34
New cards

Take a look at the code example below. In the missing code area, indicate the keyword or method necessary to throw an exception that the calling code can catch. Also, indicate the missing keyword to show a block of code no matter the outcome of the try portion of the error handling process.

𝚡 = 𝚏𝚕𝚘𝚊𝚝(𝚒𝚗𝚙𝚞𝚝("𝙴𝚗𝚝𝚎𝚛 𝚊 𝚗𝚞𝚖𝚋𝚎𝚛. "))

𝚢 = 𝚏𝚕𝚘𝚊𝚝(𝚒𝚗𝚙𝚞𝚝("𝙴𝚗𝚝𝚎𝚛 𝚊 𝚗𝚞𝚖𝚋𝚎𝚛 𝚝𝚘 𝚍𝚒𝚟𝚒𝚍𝚎 𝚋𝚢. "))

𝚝𝚛𝚢:

⇥ 𝚙𝚛𝚒𝚗𝚝(𝚏"𝚃𝚑𝚎 𝚊𝚗𝚜𝚠𝚎𝚛 𝚒𝚜 {𝚡/𝚢}.")

𝚎𝚡𝚌𝚎𝚙𝚝:

⇥ 𝚒𝚏 𝚢==𝟶:

⇥ ⇥ (1) [ 𝚝𝚑𝚛𝚘𝚠 / 𝚛𝚊𝚒𝚜𝚎 / 𝚛𝚊𝚒𝚜𝚎() / 𝚝𝚑𝚛𝚘𝚠() ] 𝙴𝚡𝚌𝚎𝚙𝚝𝚒𝚘𝚗("𝚈𝚘𝚞 𝚌𝚊𝚗𝚗𝚘𝚝 𝚍𝚒𝚟𝚒𝚍𝚎 𝚋𝚢 𝚣𝚎𝚛𝚘")

(2) [ 𝚏𝚒𝚗𝚊𝚕𝚕𝚢 / 𝚛𝚎𝚜𝚞𝚕𝚝 / 𝚎𝚕𝚜𝚎 ] :

⇥ 𝚙𝚛𝚒𝚗𝚝("𝚃𝚑𝚊𝚗𝚔 𝚢𝚘𝚞 𝚏𝚘𝚛 𝚙𝚕𝚊𝚢𝚒𝚗𝚐.")

  1. 𝚛𝚊𝚒𝚜𝚎

  2. 𝚏𝚒𝚗𝚊𝚕𝚕𝚢

𝚡 = 𝚏𝚕𝚘𝚊𝚝(𝚒𝚗𝚙𝚞𝚝("𝙴𝚗𝚝𝚎𝚛 𝚊 𝚗𝚞𝚖𝚋𝚎𝚛. "))

𝚢 = 𝚏𝚕𝚘𝚊𝚝(𝚒𝚗𝚙𝚞𝚝("𝙴𝚗𝚝𝚎𝚛 𝚊 𝚗𝚞𝚖𝚋𝚎𝚛 𝚝𝚘 𝚍𝚒𝚟𝚒𝚍𝚎 𝚋𝚢. "))

𝚝𝚛𝚢:

⇥ 𝚙𝚛𝚒𝚗𝚝(𝚏"𝚃𝚑𝚎 𝚊𝚗𝚜𝚠𝚎𝚛 𝚒𝚜 {𝚡/𝚢}.")


𝚎𝚡𝚌𝚎𝚙𝚝:

⇥ 𝚒𝚏 𝚢==𝟶:

⇥ ⇥ 𝚛𝚊𝚒𝚜𝚎 𝙴𝚡𝚌𝚎𝚙𝚝𝚒𝚘𝚗("𝚈𝚘𝚞 𝚌𝚊𝚗𝚗𝚘𝚝 𝚍𝚒𝚟𝚒𝚍𝚎 𝚋𝚢 𝚣𝚎𝚛𝚘")

𝚏𝚒𝚗𝚊𝚕𝚕𝚢:

⇥ 𝚙𝚛𝚒𝚗𝚝("𝚃𝚑𝚊𝚗𝚔 𝚢𝚘𝚞 𝚏𝚘𝚛 𝚙𝚕𝚊𝚢𝚒𝚗𝚐.")


The raise keyword will throw an exception that the calling code can catch..

Raise is a keyword, not a method.

throw is the Java equivalent of raise in Python.

The finally keyword runs code no matter what the result is of code in the try portion of try/except/else/finally. Else runs code when a try is successful.

35
New cards

An inventory manager needs to take data from calculations, an average inventory number in this case and have three possible outcomes: the nearest whole number up, represented by the upper_bound variable, the nearest whole number down, represented by the lower_bound variable, and the integer of the whole number, represented by the bound variable. Using the dropdown lists, complete the code snippet needed to fulfill the inventory manager's need.

(1) [ 𝚒𝚖𝚙𝚘𝚛𝚝 𝚖𝚊𝚝𝚑 / 𝚞𝚜𝚎 𝚖𝚊𝚝𝚑 / 𝚜𝚢𝚜.𝚖𝚊𝚝𝚑 ]

𝚊𝚟𝚎𝚛𝚊𝚐𝚎_𝚒𝚗𝚟𝚎𝚗𝚝𝚘𝚛𝚢 = 𝟽𝟽.𝟺

𝚞𝚙𝚙𝚎𝚛_𝚋𝚘𝚞𝚗𝚍 = (2) [ 𝚖𝚊𝚝𝚑.𝚌𝚎𝚒𝚕 / 𝚖𝚊𝚝𝚑.𝚝𝚛𝚞𝚗𝚌 / 𝚖𝚊𝚝𝚑.𝚛𝚘𝚞𝚗𝚍 / 𝚖𝚊𝚝𝚑.𝚏𝚕𝚘𝚘𝚛 ] (𝚊𝚟𝚎𝚛𝚊𝚐𝚎_𝚒𝚗𝚟𝚎𝚗𝚝𝚘𝚛𝚢)


𝚕𝚘𝚠𝚎𝚛_𝚋𝚘𝚞𝚗𝚍 = (3) [ 𝚖𝚊𝚝𝚑.𝚌𝚎𝚒𝚕 / 𝚖𝚊𝚝𝚑.𝚝𝚛𝚞𝚗𝚌 / 𝚖𝚊𝚝𝚑.𝚛𝚘𝚞𝚗𝚍 / 𝚖𝚊𝚝𝚑.𝚏𝚕𝚘𝚘𝚛 ] (𝚊𝚟𝚎𝚛𝚊𝚐𝚎_𝚒𝚗𝚟𝚎𝚗𝚝𝚘𝚛𝚢)


𝚋𝚘𝚞𝚗𝚍 = (4) [ 𝚖𝚊𝚝𝚑.𝚌𝚎𝚒𝚕 / 𝚖𝚊𝚝𝚑.𝚝𝚛𝚞𝚗𝚌 / 𝚖𝚊𝚝𝚑.𝚛𝚘𝚞𝚗𝚍 / 𝚖𝚊𝚝𝚑.𝚏𝚕𝚘𝚘𝚛 ](𝚊𝚟𝚎𝚛𝚊𝚐𝚎_𝚒𝚗𝚟𝚎𝚗𝚝𝚘𝚛𝚢)


𝚙𝚛𝚒𝚗𝚝(𝚞𝚙𝚙𝚎𝚛_𝚋𝚘𝚞𝚗𝚍)

𝚙𝚛𝚒𝚗𝚝(𝚕𝚘𝚠𝚎𝚛_𝚋𝚘𝚞𝚗𝚍)

𝚙𝚛𝚒𝚗𝚝(𝚋𝚘𝚞𝚗𝚍)

  1. 𝚒𝚖𝚙𝚘𝚛𝚝 𝚖𝚊𝚝𝚑

  2. 𝚖𝚊𝚝𝚑.𝚌𝚎𝚒𝚕

  3. 𝚖𝚊𝚝𝚑.𝚏𝚕𝚘𝚘𝚛

  4. 𝚖𝚊𝚝𝚑.𝚝𝚛𝚞𝚗𝚌

𝚒𝚖𝚙𝚘𝚛𝚝 𝚖𝚊𝚝𝚑

𝚊𝚟𝚎𝚛𝚊𝚐𝚎_𝚒𝚗𝚟𝚎𝚗𝚝𝚘𝚛𝚢 = 𝟽𝟽.𝟺

𝚞𝚙𝚙𝚎𝚛𝙱𝚘𝚞𝚗𝚍 = 𝚖𝚊𝚝𝚑.𝚌𝚎𝚒𝚕(𝚊𝚟𝚎𝚛𝚊𝚐𝚎_𝚒𝚗𝚟𝚎𝚗𝚝𝚘𝚛𝚢)

𝚕𝚘𝚠𝚎𝚛𝙱𝚘𝚞𝚗𝚍 = 𝚖𝚊𝚝𝚑.𝚏𝚕𝚘𝚘𝚛 (𝚊𝚟𝚎𝚛𝚊𝚐𝚎_𝚒𝚗𝚟𝚎𝚗𝚝𝚘𝚛𝚢)

𝚋𝚘𝚞𝚗𝚍 = 𝚖𝚊𝚝𝚑.𝚝𝚛𝚞𝚗𝚌(𝚊𝚟𝚎𝚛𝚊𝚐𝚎_𝚒𝚗𝚟𝚎𝚗𝚝𝚘𝚛𝚢)

𝚙𝚛𝚒𝚗𝚝(𝚞𝚙𝚙𝚎𝚛𝙱𝚘𝚞𝚗𝚍)

𝚙𝚛𝚒𝚗𝚝(𝚕𝚘𝚠𝚎𝚛𝙱𝚘𝚞𝚗𝚍)

𝚙𝚛𝚒𝚗𝚝(𝚋𝚘𝚞𝚗𝚍)


As the Math built-in module is being used, the module needs to be imported. That is done on the first line of code.

The math.ceil function takes a floating number up to the next whole number.

The math.floor function lowers a floating number to its base whole number.

The math.trunc function returns the integer portion of a number.

A round function would not be efficient here as it would not guarantee the upper and lower integers from the floating number are returned.

36
New cards

A game developer is testing some random number generators. The results should be as follows:

result1 should have a random country from a list of countries

result2 should display the list in a random order

result3 should display two random countries from a list of countries

Using the dropdown arrows, fill in the code needed to generate the proper random numbers for each variable.

(1) [ 𝚒𝚖𝚙𝚘𝚛𝚝 𝚜𝚑𝚞𝚏𝚏𝚕𝚎 / 𝚒𝚖𝚙𝚘𝚛𝚝 𝚛𝚊𝚗𝚍𝚘𝚖 / 𝚒𝚖𝚙𝚘𝚛𝚝 𝚌𝚑𝚘𝚒𝚌𝚎 / 𝚒𝚖𝚙𝚘𝚛𝚝 𝚜𝚊𝚖𝚙𝚕𝚎 ]

𝚌𝚘𝚞𝚗𝚝𝚛𝚒𝚎𝚜 =['𝚄𝚂𝙰','𝙲𝚊𝚗𝚊𝚍𝚊','𝙼𝚎𝚡𝚒𝚌𝚘','𝙹𝚊𝚙𝚊𝚗','𝚂𝚙𝚊𝚒𝚗','𝙺𝚎𝚗𝚢𝚊']

𝚛𝚎𝚜𝚞𝚕𝚝𝟷 = 𝚛𝚊𝚗𝚍𝚘𝚖. (2) [ 𝚌𝚑𝚘𝚒𝚌𝚎(𝚌𝚘𝚞𝚗𝚝𝚛𝚒𝚎𝚜) / 𝚜𝚑𝚞𝚏𝚏𝚕𝚎(𝚌𝚘𝚞𝚗𝚝𝚛𝚒𝚎𝚜) / 𝚜𝚊𝚖𝚙𝚕𝚎(𝚌𝚘𝚞𝚗𝚝𝚛𝚒𝚎𝚜,𝟸) ]

𝚛𝚎𝚜𝚞𝚕𝚝𝟸 = 𝚛𝚊𝚗𝚍𝚘𝚖.(3) [ 𝚌𝚑𝚘𝚒𝚌𝚎(𝚌𝚘𝚞𝚗𝚝𝚛𝚒𝚎𝚜) / 𝚜𝚑𝚞𝚏𝚏𝚕𝚎(𝚌𝚘𝚞𝚗𝚝𝚛𝚒𝚎𝚜) / 𝚜𝚊𝚖𝚙𝚕𝚎(𝚌𝚘𝚞𝚗𝚝𝚛𝚒𝚎𝚜,𝟸) ]

𝚛𝚎𝚜𝚞𝚕𝚝𝟹 = 𝚛𝚊𝚗𝚍𝚘𝚖. (4) [ 𝚌𝚑𝚘𝚒𝚌𝚎(𝚌𝚘𝚞𝚗𝚝𝚛𝚒𝚎𝚜) / 𝚜𝚑𝚞𝚏𝚏𝚕𝚎(𝚌𝚘𝚞𝚗𝚝𝚛𝚒𝚎𝚜) / 𝚜𝚊𝚖𝚙𝚕𝚎(𝚌𝚘𝚞𝚗𝚝𝚛𝚒𝚎𝚜,𝟸) ]

  1. 𝚒𝚖𝚙𝚘𝚛𝚝 𝚛𝚊𝚗𝚍𝚘𝚖

  2. 𝚌𝚑𝚘𝚒𝚌𝚎(𝚌𝚘𝚞𝚗𝚝𝚛𝚒𝚎𝚜)

  3. 𝚜𝚑𝚞𝚏𝚏𝚕𝚎(𝚌𝚘𝚞𝚗𝚝𝚛𝚒𝚎𝚜

  4. 𝚜𝚊𝚖𝚙𝚕𝚎(𝚌𝚘𝚞𝚗𝚝𝚛𝚒𝚎𝚜,𝟸)

𝚒𝚖𝚙𝚘𝚛𝚝 𝚛𝚊𝚗𝚍𝚘𝚖

𝚌𝚘𝚞𝚗𝚝𝚛𝚒𝚎𝚜 = ['𝚄𝚂𝙰','𝙲𝚊𝚗𝚊𝚍𝚊','𝙼𝚎𝚡𝚒𝚌𝚘','𝙹𝚊𝚙𝚊𝚗','𝚂𝚙𝚊𝚒𝚗','𝙺𝚎𝚗𝚢𝚊']

𝚛𝚎𝚜𝚞𝚕𝚝𝟷 = 𝚛𝚊𝚗𝚍𝚘𝚖.𝚌𝚑𝚘𝚒𝚌𝚎(𝚌𝚘𝚞𝚗𝚝𝚛𝚒𝚎𝚜)

𝚛𝚎𝚜𝚞𝚕𝚝𝟸 = 𝚛𝚊𝚗𝚍𝚘𝚖.𝚜𝚑𝚞𝚏𝚏𝚕𝚎(𝚌𝚘𝚞𝚗𝚝𝚛𝚒𝚎𝚜)

𝚛𝚎𝚜𝚞𝚕𝚝𝟹 = 𝚛𝚊𝚗𝚍𝚘𝚖.𝚜𝚊𝚖𝚙𝚕𝚎(𝚌𝚘𝚞𝚗𝚝𝚛𝚒𝚎𝚜,𝟸)


The random module must be imported to access the random functions needed to pull values from a list.

The choice function selects a random value from a list.

The shuffle function reorders a list randomly.

The sample function returns a set number of random values from a list.

37
New cards

A new Python developer is learning built-in modules and the methods used in those modules. Specifically, the developer needs to know the modules used to open text files, find the mean from a series of test scores, make directories within Python, and exit a gaming app when the game is over. Using drag and drop, match each method with the built-in module used.

1. open()

2. ceil()

3. mkdir()

4. exit()

a. math

b. sys

c. os

d. io

1 + d = open() - io

2 + a = ceil() - math

3 + c = mkdir() - os

4 + b = exit() - sys


The io modules the input/output module used to allow Python to read from and write to files. Files are opened using the open() method.

The math module allows one to use mathematical methods, including ceil(), which rounds a floating number of to the nearest whole number. 

The os module allows Python to manipulate operating system areas, including folders. Folders are created using the mkdir() method.

The sys module allows Python code to run system operations, such as to exit an app using the exit() method.

38
New cards

Using the snippets of code below, use the drag-and-drop method to build code that will do the following:

  • Store the actual value of pi in a variable called pi

  • A user will enter the radius of the circle and needs an option to have the radius be carried out to decimal places.

  • Use pi to calculate the area of a circle and store it in a variable called area.

  • Print the result (the area), formatted to two decimal places.

Not every line of code will be used.

Line 1 - ?

Line 2 - ?

Line 3 - ?

Line 4 - ?

Line 5 - ?

a. 𝚞𝚜𝚎 𝚖𝚊𝚝𝚑

b. 𝚛𝚊𝚍𝚒𝚞𝚜 = 𝚒𝚗𝚝(𝚒𝚗𝚙𝚞𝚝("𝙴𝚗𝚝𝚎𝚛 𝚊 𝚛𝚊𝚍𝚒𝚞𝚜 𝚏𝚘𝚛 𝚊 𝚌𝚒𝚛𝚌𝚕𝚎."))

c. 𝚛𝚊𝚍𝚒𝚞𝚜 = 𝚏𝚕𝚘𝚊𝚝(𝚒𝚗𝚙𝚞𝚝("𝙴𝚗𝚝𝚎𝚛 𝚊 𝚛𝚊𝚍𝚒𝚞𝚜 𝚏𝚘𝚛 𝚊 𝚌𝚒𝚛𝚌𝚕𝚎."))

d. 𝚙𝚛𝚒𝚗𝚝(𝚏"𝙰 𝚌𝚒𝚛𝚌𝚕𝚎 𝚠𝚒𝚝𝚑 𝚊 𝚛𝚊𝚍𝚒𝚞𝚜 𝚘𝚏 {𝚛𝚊𝚍𝚒𝚞𝚜} 𝚠𝚒𝚕𝚕 𝚑𝚊𝚟𝚎 𝚊𝚗 𝚊𝚛𝚎𝚊 𝚘𝚏 %.𝟸𝚍." %𝚊𝚛𝚎𝚊)

e. 𝚊𝚛𝚎𝚊 = 𝚙𝚒* 𝚛𝚊𝚍𝚒𝚞𝚜 ^ 𝟸

f. 𝚙𝚒 = 𝚖𝚊𝚝𝚑.𝚙𝚒

g. 𝚙𝚛𝚒𝚗𝚝(𝚏"𝙰 𝚌𝚒𝚛𝚌𝚕𝚎 𝚠𝚒𝚝𝚑 𝚊 𝚛𝚊𝚍𝚒𝚞𝚜 𝚘𝚏 {𝚛𝚊𝚍𝚒𝚞𝚜} 𝚠𝚒𝚕𝚕 𝚑𝚊𝚟𝚎 𝚊𝚗 𝚊𝚛𝚎𝚊 𝚘𝚏 %.𝟸𝚏." %𝚊𝚛𝚎𝚊)

h. 𝚒𝚖𝚙𝚘𝚛𝚝 𝚖𝚊𝚝𝚑

i. 𝚊𝚛𝚎𝚊 = 𝚙𝚒* 𝚛𝚊𝚍𝚒𝚞𝚜 ** 𝟸

Line 1 - h: 𝚒𝚖𝚙𝚘𝚛𝚝 𝚖𝚊𝚝𝚑

Line 2 - f: 𝚙𝚒 = 𝚖𝚊𝚝𝚑.𝚙𝚒

Line 3 - c: 𝚛𝚊𝚍𝚒𝚞𝚜 = 𝚏𝚕𝚘𝚊𝚝(𝚒𝚗𝚙𝚞𝚝("𝙴𝚗𝚝𝚎𝚛 𝚊 𝚛𝚊𝚍𝚒𝚞𝚜 𝚏𝚘𝚛 𝚊 𝚌𝚒𝚛𝚌𝚕𝚎."))

Line 4 - i: 𝚊𝚛𝚎𝚊 = 𝚙𝚒 𝚛𝚊𝚍𝚒𝚞𝚜 * 𝟸

Line 5 - g: 𝚙𝚛𝚒𝚗𝚝(𝚏"𝙰 𝚌𝚒𝚛𝚌𝚕𝚎 𝚠𝚒𝚝𝚑 𝚊 𝚛𝚊𝚍𝚒𝚞𝚜 𝚘𝚏 {𝚛𝚊𝚍𝚒𝚞𝚜} 𝚠𝚒𝚕𝚕 𝚑𝚊𝚟𝚎 𝚊𝚗 𝚊𝚛𝚎𝚊 𝚘𝚏 %.𝟸𝚏." %𝚊𝚛𝚎𝚊)

𝚒𝚖𝚙𝚘𝚛𝚝 𝚖𝚊𝚝𝚑

𝚙𝚒 = 𝚖𝚊𝚝𝚑.𝚙𝚒

𝚛𝚊𝚍𝚒𝚞𝚜 = 𝚏𝚕𝚘𝚊𝚝(𝚒𝚗𝚙𝚞𝚝("𝙴𝚗𝚝𝚎𝚛 𝚊 𝚛𝚊𝚍𝚒𝚞𝚜 𝚏𝚘𝚛 𝚊 𝚌𝚒𝚛𝚌𝚕𝚎."))

𝚊𝚛𝚎𝚊 = 𝚙𝚒 𝚛𝚊𝚍𝚒𝚞𝚜 * 𝟸

𝚙𝚛𝚒𝚗𝚝(𝚏"𝙰 𝚌𝚒𝚛𝚌𝚕𝚎 𝚠𝚒𝚝𝚑 𝚊 𝚛𝚊𝚍𝚒𝚞𝚜 𝚘𝚏 {𝚛𝚊𝚍𝚒𝚞𝚜} 𝚠𝚒𝚕𝚕 𝚑𝚊𝚟𝚎 𝚊𝚗 𝚊𝚛𝚎𝚊 𝚘𝚏 %.𝟸𝚏." %𝚊𝚛𝚎𝚊)


As pi is part of the built-in math module, the math module needs to be imported.

Then, math.pi is stored in a variable called pi.

For an accurate calculation, whatever a user inputs for a circle radius should be stored as a floating number, not an integer.

Next, the area is calculated and stored, using the formula pi radius * 2, with the ** indicating "raise to the power of."

Then, the result is printed, with the %2f indicating a floating number, formatted to two numbers after the decimal point.

39
New cards

A developer is building a code block to log a current date and time for app activity. Which two code snippets will replace the #current date and time comment with the correct date and time?

𝚒𝚖𝚙𝚘𝚛𝚝 𝚍𝚊𝚝𝚎𝚝𝚒𝚖𝚎

𝚕𝚘𝚐_𝚝𝚒𝚖𝚎 = #𝚌𝚞𝚛𝚛𝚎𝚗𝚝 𝚍𝚊𝚝𝚎 𝚊𝚗𝚍 𝚝𝚒𝚖𝚎

𝚙𝚛𝚒𝚗𝚝("𝚎𝚗𝚝𝚛𝚢 𝚝𝚒𝚖𝚎: ",𝚕𝚘𝚐_𝚝𝚒𝚖𝚎)

☐ 𝚍𝚊𝚝𝚎𝚝𝚒𝚖𝚎.𝚍𝚊𝚝𝚎𝚝𝚒𝚖𝚎.𝚝𝚘𝚍𝚊𝚢()

☐ 𝚍𝚊𝚝𝚎𝚝𝚒𝚖𝚎.𝚍𝚊𝚝𝚎𝚝𝚒𝚖𝚎.𝚜𝚝𝚛𝚙𝚝𝚒𝚖𝚎()

☐ 𝚍𝚊𝚝𝚎𝚝𝚒𝚖𝚎.𝚍𝚊𝚝𝚎𝚝𝚒𝚖𝚎.𝚗𝚘𝚠()

☐ 𝚍𝚊𝚝𝚎𝚝𝚒𝚖𝚎.𝚍𝚊𝚝𝚎𝚝𝚒𝚖𝚎.𝚜𝚝𝚛𝚏𝚝𝚒𝚖𝚎()

𝚍𝚊𝚝𝚎𝚝𝚒𝚖𝚎.𝚍𝚊𝚝𝚎𝚝𝚒𝚖𝚎.𝚝𝚘𝚍𝚊𝚢()

𝚍𝚊𝚝𝚎𝚝𝚒𝚖𝚎.𝚍𝚊𝚝𝚎𝚝𝚒𝚖𝚎.𝚗𝚘𝚠()


The datetime.datetime.now() and datetime.datetime.today() methods return the current date and time.

strftime() converts a date object to a string.

strptime() converts a string to a date object.

40
New cards

Evaluate the following partial code snippet:

𝚌𝚒𝚝𝚒𝚎𝚜 = ['𝙰𝚗𝚌𝚑𝚘𝚛𝚊𝚐𝚎','𝙹𝚞𝚗𝚎𝚊𝚞','𝙵𝚊𝚒𝚛𝚋𝚊𝚗𝚔𝚜','𝙺𝚎𝚝𝚌𝚑𝚒𝚔𝚊𝚗','𝚂𝚒𝚝𝚔𝚊','𝚆𝚊𝚜𝚒𝚕𝚕𝚊']

𝚏𝚘𝚛 𝚌𝚒𝚝𝚢 𝚒𝚗 𝚌𝚒𝚝𝚒𝚎𝚜:

⇥ 𝚙𝚛𝚒𝚗𝚝(𝚏'{𝚌𝚒𝚝𝚢} 𝚒𝚜 𝚊 𝚏𝚊𝚖𝚘𝚞𝚜 𝙰𝚕𝚊𝚜𝚔𝚊𝚗 𝚌𝚒𝚝𝚢.')

⇥ 𝚒𝚏 𝚌𝚒𝚝𝚢 == '𝙺𝚎𝚝𝚌𝚑𝚒𝚔𝚊𝚗':

⇥ ⇥ 𝚋𝚛𝚎𝚊𝚔

How many cities from the list will print?: ___

4


The list will print four cities, as the break command will end the loop after the city Ketchikan prints. Had the break come before the print statement, only three cities would print.

41
New cards

Analyze the following code:

𝚊 = 𝟻

𝚋 = 𝟸

𝚌 = 𝟹

𝚊**= 𝚋

𝚋*= 𝚌

𝚊//=𝚋

For each statement regarding the values of these variables, indicate Yes if the statement is true and No if the statement is false.

  1. 𝚌 == 𝟹

  2. 𝚋 == 𝟼

  3. 𝚊 == 𝟷

  1. Yes

  2. Yes

  3. No


In running through the code, a**= b is a raised to the power of b. 5**2 = 25, so a = 25 after that line of code is executed.

b*= c is b multiplied by c. 2*3 = 6, so b = 6.

a//=b is a floor division result of a and b, not a modulus. Thus, 25//=6 = 4 and not 1, which is the modulus of dividing 25 and 6. Floor division removes any remainder after the integer.

c is never assigned a new value, so c = 3.

42
New cards

Evaluate the following code:

𝚊 = 𝟷𝟶

𝚋 = 𝟽

𝚌 = 𝟻

Using these variables, determine if each calculation is true. Indicate a Yes if it is true and a No if it is not.

  1. 𝚊 > 𝚋 𝚊𝚗𝚍 𝚋 > 𝚌

  2. 𝚊 >= 𝚌 𝚊𝚗𝚍 𝚗𝚘𝚝 (𝚋+ 𝚌 > 𝚊)

  3. 𝚊 + 𝚋 * 𝚌 𝟾𝟻 𝚘𝚛 𝚊 - 𝚋 * 𝚌 == 𝟷𝟻

  1. Yes

  2. No

  3. No


  • 10 > 7 and 7 > 5, so a > b and b > c

  • 10 >= 5 and 12 > 10 but with the not keyword in front of the statement, both statements are not true, so the statement is false.

  • 10 + 35 = 45, which is not 85, and 10 - 35 = -25, which is not 15, so that statement is false.

43
New cards

Using the dropdown arrows for the missing code pieces, complete the following code example so that it accomplishes the functionality of this game:

A user gets five chances to correctly guess a whole number from 1 to 10. If the user guesses correctly, they get a congratulatory message and the game ends. If not, they get a message thanking them for playing.

(1)

[ 𝚏𝚛𝚘𝚖 𝚛𝚊𝚗𝚍𝚘𝚖 𝚒𝚖𝚙𝚘𝚛𝚝 𝚛𝚊𝚗𝚍𝚘𝚖 /

𝚏𝚛𝚘𝚖 𝚛𝚊𝚗𝚍𝚘𝚖 𝚒𝚖𝚙𝚘𝚛𝚝 𝚛𝚊𝚗𝚍𝚒𝚗𝚝 /

𝚏𝚛𝚘𝚖 𝚛𝚊𝚗𝚍𝚘𝚖 𝚒𝚖𝚙𝚘𝚛𝚝 𝚛𝚊𝚗𝚍𝚛𝚊𝚗𝚐𝚎 ]

𝚏𝚘𝚛 𝚒 𝚒𝚗 𝚛𝚊𝚗𝚐𝚎(𝟻):

𝚐𝚞𝚎𝚜𝚜 = 𝚒𝚗𝚝(𝚒𝚗𝚙𝚞𝚝("𝙴𝚗𝚝𝚎𝚛 𝚊 𝚗𝚞𝚖𝚋𝚎𝚛 𝚏𝚛𝚘𝚖 𝟷 𝚝𝚘 𝟷𝟶. "))

𝚛𝚊𝚗𝚍𝙽𝚞𝚖 = (𝟸) [ 𝚛𝚊𝚗𝚍𝚛𝚊𝚗𝚐𝚎(𝟷,𝟷𝟷) / 𝚛𝚊𝚗𝚍𝚛𝚊𝚗𝚐𝚎(𝟷,𝟷𝟶) / 𝚛𝚊𝚗𝚍𝚒𝚗𝚝(𝟷,𝟷𝟶) / 𝚛𝚊𝚗𝚍𝚒𝚗𝚝(𝟷,𝟷𝟷) ]

𝚒𝚏 𝚐𝚞𝚎𝚜𝚜 == 𝚛𝚊𝚗𝚍𝙽𝚞𝚖:

𝚙𝚛𝚒𝚗𝚝("𝚆𝚎 𝚖𝚊𝚝𝚌𝚑𝚎𝚍!")

𝚋𝚛𝚎𝚊𝚔

𝚎𝚕𝚜𝚎:

𝚙𝚛𝚒𝚗𝚝("𝚆𝚎 𝚍𝚒𝚍 𝚗𝚘𝚝 𝚖𝚊𝚝𝚌𝚑. 𝚃𝚛𝚢 𝚊𝚐𝚊𝚒𝚗")

1) 𝚏𝚛𝚘𝚖 𝚛𝚊𝚗𝚍𝚘𝚖 𝚒𝚖𝚙𝚘𝚛𝚝 𝚛𝚊𝚗𝚍𝚒𝚗𝚝

2) 𝚛𝚊𝚗𝚍𝚒𝚗𝚝(𝟷,𝟷𝟶)


𝚏𝚛𝚘𝚖 𝚛𝚊𝚗𝚍𝚘𝚖 𝚒𝚖𝚙𝚘𝚛𝚝 𝚛𝚊𝚗𝚍𝚒𝚗𝚝

𝚏𝚘𝚛 𝚒 𝚒𝚗 𝚛𝚊𝚗𝚐𝚎(𝟻):

𝚐𝚞𝚎𝚜𝚜 = 𝚒𝚗𝚝(𝚒𝚗𝚙𝚞𝚝("𝚎𝚗𝚝𝚎𝚛 𝚊 𝚗𝚞𝚖𝚋𝚎𝚛 𝚏𝚛𝚘𝚖 𝟷 𝚝𝚘 𝟷𝟶. "))

𝚛𝚊𝚗𝚍𝚗𝚞𝚖 = 𝚛𝚊𝚗𝚍𝚒𝚗𝚝(𝟷,𝟷𝟶)

𝚒𝚏 𝚐𝚞𝚎𝚜𝚜 == 𝚛𝚊𝚗𝚍𝚗𝚞𝚖:

𝚙𝚛𝚒𝚗𝚝("𝚠𝚎 𝚖𝚊𝚝𝚌𝚑𝚎𝚍!")

𝚋𝚛𝚎𝚊𝚔

𝚎𝚕𝚜𝚎:

𝚙𝚛𝚒𝚗𝚝("𝚠𝚎 𝚍𝚒𝚍 𝚗𝚘𝚝 𝚖𝚊𝚝𝚌𝚑. 𝚃𝚛𝚢 𝚊𝚐𝚊𝚒𝚗")


This game calls for whole numbers to be used, so the randint module needs to be imported from the random module.

The actual random function to use in this code example is randint, which generates random integers.

1 and 10 are the lowest and highest values, respectively, so those are the arguments used.

44
New cards

A developer wants documentation for a function to display when called upon in a print statement. Use the dropdown menus to fill in the remainder of the code necessary to generate the documentation.

𝚍𝚎𝚏 𝚊𝚛𝚎𝚊(𝚠𝚒𝚍𝚝𝚑,𝚑𝚎𝚒𝚐𝚑𝚝):

⇥ (1) [ ''' / # / // ] 𝙶𝚎𝚗𝚎𝚛𝚊𝚝𝚎𝚜 𝚝𝚑𝚎 𝚊𝚛𝚎𝚊 𝚘𝚏 𝚊 𝚛𝚎𝚌𝚝𝚊𝚗𝚐𝚕𝚎 (2) [ ''' / ### / /// ]

⇥ 𝚝𝚘𝚝𝚊𝚕𝙰𝚛𝚎𝚊 = 𝚠𝚒𝚍𝚝𝚑 * 𝚑𝚎𝚒𝚐𝚑𝚝

⇥ 𝚛𝚎𝚝𝚞𝚛𝚗 𝚝𝚘𝚝𝚊𝚕𝙰𝚛𝚎𝚊

𝚙𝚛𝚒𝚗𝚝( (3) [ 𝚊𝚛𝚎𝚊.__𝚍𝚘𝚌__ / 𝚊𝚛𝚎𝚊(__𝚍𝚘𝚌__) / 𝚊𝚛𝚎𝚊.𝚍𝚘𝚌 ] )

Text surrounded by ''' right after a function is declared documents the function and will output the documentation when the print method is called with the function name followed by the doc attribute. # and // are comment generators, the former being one in Python. Thus, the completed code looks like this:

𝚍𝚎𝚏 𝚊𝚛𝚎𝚊(𝚠𝚒𝚍𝚝𝚑,𝚑𝚎𝚒𝚐𝚑𝚝):

⇥ '''𝙶𝚎𝚗𝚎𝚛𝚊𝚝𝚎𝚜 𝚝𝚑𝚎 𝚊𝚛𝚎𝚊 𝚘𝚏 𝚊 𝚛𝚎𝚌𝚝𝚊𝚗𝚐𝚕𝚎'''

⇥ 𝚝𝚘𝚝𝚊𝚕𝙰𝚛𝚎𝚊 = 𝚠𝚒𝚍𝚝𝚑 * 𝚑𝚎𝚒𝚐𝚑𝚝

⇥ 𝚛𝚎𝚝𝚞𝚛𝚗 𝚝𝚘𝚝𝚊𝚕𝙰𝚛𝚎𝚊

𝚙𝚛𝚒𝚗𝚝(𝚊𝚛𝚎𝚊.__𝚍𝚘𝚌__)

45
New cards

Examine the following code:

𝚍𝚎𝚏 𝚊𝚛𝚎𝚊 (𝚠𝚒𝚍𝚝𝚑, 𝚑𝚎𝚒𝚐𝚑𝚝):

⇥ 𝚊𝚛𝚎𝚊 = 𝚠𝚒𝚍𝚝𝚑 * 𝚑𝚎𝚒𝚐𝚑𝚝

⇥ 𝚛𝚎𝚝𝚞𝚛𝚗 𝚊𝚛𝚎𝚊

𝚋𝚘𝚡𝟷𝚊𝚛𝚎𝚊 = 𝚊𝚛𝚎𝚊 (𝟻, 𝟸)

𝚋𝚘𝚡𝟸𝚊𝚛𝚎𝚊 = 𝚊𝚛𝚎𝚊 (𝟼)

What needs to change in order for the height in the area function to be 12 if a height is not specified when calling the function?


○ 𝚑𝚎𝚒𝚐𝚑𝚝 𝚟𝚊𝚛𝚒𝚊𝚋𝚕𝚎 𝚜𝚎𝚝𝚝𝚒𝚗𝚐 𝚑𝚎𝚒𝚐𝚑𝚝 =𝟷𝟸 𝚒𝚗𝚜𝚒𝚍𝚎 𝚝𝚑𝚎 𝚏𝚞𝚗𝚌𝚝𝚒𝚘𝚗

○ 𝚑𝚎𝚒𝚐𝚑𝚝 𝚟𝚊𝚛𝚒𝚊𝚋𝚕𝚎 𝚜𝚎𝚝𝚝𝚒𝚗𝚐 𝚑𝚎𝚒𝚐𝚑𝚝 =𝟷𝟸 𝚋𝚎𝚏𝚘𝚛𝚎 𝚝𝚑𝚎 𝚏𝚞𝚗𝚌𝚝𝚒𝚘𝚗 𝚒𝚜 𝚍𝚎𝚌𝚕𝚊𝚛𝚎𝚍

○ 𝚍𝚎𝚏 𝚝𝚘 𝚍𝚎𝚏 𝚊𝚛𝚎𝚊 (𝚠𝚒𝚍𝚝𝚑, 𝚑𝚎𝚒𝚐𝚑𝚝 = 𝟷𝟸)

○ 𝚋𝚘𝚡𝟸𝚊𝚛𝚎𝚊 𝚕𝚒𝚗𝚎 𝚝𝚘 𝚋𝚘𝚡𝟸𝚊𝚛𝚎𝚊 = 𝚊𝚛𝚎𝚊 (𝟼,𝟷𝟸)

In order for a function parameter to have a default value, the default value must be specified when defining the function.

Therefore, the function definition line needs to be changed to def area (width, height = 12). The completed code looks like this:

𝚍𝚎𝚏 𝚊𝚛𝚎𝚊 (𝚠𝚒𝚍𝚝𝚑, 𝚑𝚎𝚒𝚐𝚑𝚝=𝟷𝟸):

⇥ 𝚊𝚛𝚎𝚊 = 𝚠𝚒𝚍𝚝𝚑 * 𝚑𝚎𝚒𝚐𝚑𝚝

⇥ 𝚛𝚎𝚝𝚞𝚛𝚗 𝚊𝚛𝚎𝚊

𝚋𝚘𝚡𝟷𝚊𝚛𝚎𝚊 = 𝚊𝚛𝚎𝚊 (𝟻, 𝟸)

𝚋𝚘𝚡𝟸𝚊𝚛𝚎𝚊 = 𝚊𝚛𝚎𝚊 (𝟼)

46
New cards

For each of the statements regarding pydoc, select Yes if the statement is true and No if the statement is false.

  1. The proper syntax for pydoc is Python -m pydoc module where module represents the name of a Python module.

  2. Pydoc is a self-contained executable that can be run from a command-line prompt.

  3. Pydoc generates documentation on Python modules.

  1. Yes

  2. No

  3. Yes


Explanation:

  • Pydoc, which generates documentation on Python modules, is part of the Python command prompt.

  • The -m after the Python command prompt with the Pydoc command and the name of the module will produce documentation on the module.
    For example, the code:
    Python -m pydoc pass
    will show documentation on the pass module within Python.

47
New cards

A junior programmer enters the following code:

𝚑𝚎𝚒𝚐𝚑𝚝 = 𝟻

𝚠𝚒𝚍𝚝𝚑 = 𝟻

𝚒𝚏 𝚑𝚎𝚒𝚐𝚑𝚝 == 𝚠𝚒𝚍𝚝𝚑

𝚙𝚛𝚒𝚗𝚝("𝚈𝚘𝚞 𝚑𝚊𝚟𝚎 𝚊 𝚜𝚚𝚞𝚊𝚛𝚎")

When trying to run the code, errors are generated. Which two fixes are necessary in order for this code to work? (Choose two)


☐ The if condition needs to be in parentheses.

☐ A colon needs to be added at the end of the if condition.

☐ The print statement needs to be indented.

☐ The if condition needs to have a single equals sign, not a double equals sign.

A colon needs to be added at the end of the if condition.

The print statement needs to be indented.


A colon terminates the condition at the end of an if statement, so a colon needs to be added after the word width.
Any action to take place if a condition is true needs to be indented. So the Print statement should be indented.

Thus, the print statement on the last line of the code sample needs to be indented. The correct code looks like this:

𝚑𝚎𝚒𝚐𝚑𝚝 = 𝟻

𝚠𝚒𝚍𝚝𝚑 = 𝟻

𝚒𝚏 𝚑𝚎𝚒𝚐𝚑𝚝 == 𝚠𝚒𝚍𝚝𝚑:

⇥ 𝚙𝚛𝚒𝚗𝚝("𝚈𝚘𝚞 𝚑𝚊𝚟𝚎 𝚊 𝚜𝚚𝚞𝚊𝚛𝚎")

48
New cards

A new Python developer is learning the code necessary to display dates in multiple formats. The developer has been tasked to create the following display with dates:

𝟸𝟶𝟸𝟹-𝟶𝟷-𝟶𝟷 𝟶𝟹:𝟷𝟿:𝟺𝟹.𝟺𝟷𝟸𝟺𝟽𝟼

𝚃𝚑𝚎 𝚌𝚞𝚛𝚛𝚎𝚗𝚝 𝚍𝚊𝚝𝚎 𝚒𝚜 𝟶𝟷/𝟶𝟷/𝟸𝟶𝟸𝟹

𝚃𝚑𝚎 𝚌𝚞𝚛𝚛𝚎𝚗𝚝 𝚠𝚎𝚎𝚔𝚍𝚊𝚢 𝚒𝚜: 𝟼

Using the dropdown arrows, complete the code snippet necessary to create the display, leaving open the possibility of the time being displayed in the future.

(1) [ 𝚒𝚖𝚙𝚘𝚛𝚝 𝚍𝚊𝚝𝚎𝚝𝚒𝚖𝚎 / 𝚒𝚖𝚙𝚘𝚛𝚝 𝚍𝚊𝚝𝚎 / 𝚒𝚖𝚙𝚘𝚛𝚝 𝚝𝚒𝚖𝚎 ]

𝚌𝚞𝚛𝚛𝚎𝚗𝚝_𝚍𝚊𝚝𝚎 = (2) [ 𝚍𝚊𝚝𝚎𝚝𝚒𝚖𝚎.𝚍𝚊𝚝𝚎𝚝𝚒𝚖𝚎.𝚝𝚘𝚍𝚊𝚢() / 𝚍𝚊𝚝𝚎𝚝𝚒𝚖𝚎.𝚍𝚊𝚝𝚎𝚝𝚒𝚖𝚎.𝚍𝚊𝚢() / 𝚍𝚊𝚝𝚎𝚝𝚒𝚖𝚎.𝚍𝚊𝚝𝚎𝚝𝚒𝚖𝚎.𝚌𝚞𝚛𝚛𝚎𝚗𝚝𝚍𝚊𝚝𝚎() ]

𝚙𝚛𝚒𝚗𝚝(𝚌𝚞𝚛𝚛𝚎𝚗𝚝_𝚍𝚊𝚝𝚎)

𝚙𝚛𝚒𝚗𝚝("𝚃𝚑𝚎 𝚌𝚞𝚛𝚛𝚎𝚗𝚝 𝚍𝚊𝚝𝚎 𝚒𝚜", 𝚍𝚊𝚝𝚎𝚝𝚒𝚖𝚎.𝚍𝚊𝚝𝚎𝚝𝚒𝚖𝚎.𝚜𝚝𝚛𝚏𝚝𝚒𝚖𝚎(𝚌𝚞𝚛𝚛𝚎𝚗𝚝_𝚍𝚊𝚝𝚎," (3) [ %𝚖/%𝚍/%𝚈 / %𝙼𝙼%𝙳𝙳%𝚈𝚈𝚈𝚈 / %𝚖𝚖%𝚍𝚍%𝚢𝚢𝚢𝚢 ] "))

𝚙𝚛𝚒𝚗𝚝("𝚃𝚑𝚎 𝚌𝚞𝚛𝚛𝚎𝚗𝚝 𝚠𝚎𝚎𝚔𝚍𝚊𝚢 𝚒𝚜", 𝚌𝚞𝚛𝚛𝚎𝚗𝚝_𝚍𝚊𝚝𝚎. (4) [ 𝚠𝚎𝚎𝚔𝚍𝚊𝚢() / 𝚠𝚎𝚎𝚔𝚍𝚊𝚢 / 𝚍𝚊𝚢() ] )

For datetime functions to be used, the datetime module must be imported. Thus, import datetime is the first line of code needed in this example.

To capture today's date, the today() method, which is part of the date category of the datetime module, is needed. Thus, the correct code is datetime.datetime.today() and that code displays the date in yyyy-mm-dd format.

The strftime function formats a data as a string, using two arguments: the date to format (or time) and the format.

The weekday function returns a weekday number, with 1 being Monday and 7 being Sunday.

%m gives us a two-digit month.

%d gives us a two-digit day.

%Y gives us a four-digit year.

The weekday() method returns the current day of the week, Monday being 0 and Sunday being 6.

The completed code looks like this:

𝚒𝚖𝚙𝚘𝚛𝚝 𝚍𝚊𝚝𝚎𝚝𝚒𝚖𝚎

𝚌𝚞𝚛𝚛𝚎𝚗𝚝_𝚍𝚊𝚝𝚎 = 𝚍𝚊𝚝𝚎𝚝𝚒𝚖𝚎.𝚍𝚊𝚝𝚎𝚝𝚒𝚖𝚎.𝚝𝚘𝚍𝚊𝚢()

𝚙𝚛𝚒𝚗𝚝(𝚌𝚞𝚛𝚛𝚎𝚗𝚝_𝚍𝚊𝚝𝚎)

𝚙𝚛𝚒𝚗𝚝("𝚃𝚑𝚎 𝚌𝚞𝚛𝚛𝚎𝚗𝚝 𝚍𝚊𝚝𝚎 𝚒𝚜", 𝚍𝚊𝚝𝚎𝚝𝚒𝚖𝚎.𝚍𝚊𝚝𝚎𝚝𝚒𝚖𝚎.𝚜𝚝𝚛𝚏𝚝𝚒𝚖𝚎(𝚌𝚞𝚛𝚛𝚎𝚗𝚝_𝚍𝚊𝚝𝚎, "%𝚖/%𝚍/%𝚈"))

𝚙𝚛𝚒𝚗𝚝("𝚃𝚑𝚎 𝚌𝚞𝚛𝚛𝚎𝚗𝚝 𝚠𝚎𝚎𝚔𝚍𝚊𝚢 𝚒𝚜", 𝚌𝚞𝚛𝚛𝚎𝚗𝚝_𝚍𝚊𝚝𝚎.𝚠𝚎𝚎𝚔𝚍𝚊𝚢())

49
New cards

Evaluate the following code example, which is an attempt to loop through and print a tuple of products by serial number:

𝚙𝚛𝚘𝚍𝚞𝚌𝚝𝚜 = ("𝟷𝟷𝟷𝟷","𝟸𝟸𝟸𝟸","𝟹𝟹𝟹𝟹","𝟺𝟺𝟺𝟺","𝟻𝟻𝟻𝟻","𝟼𝟼𝟼𝟼","𝟽𝟽𝟽𝟽","𝟾𝟾𝟾𝟾","𝟿𝟿𝟿𝟿")

𝚏𝚘𝚛 𝚙𝚛𝚘𝚍𝚞𝚌𝚝 𝚒𝚗 𝚙𝚛𝚘𝚍𝚞𝚌𝚝𝚜:

𝚙𝚛𝚒𝚗𝚝(𝚙𝚛𝚘𝚍𝚞𝚌𝚝𝚜[𝚙𝚛𝚘𝚍𝚞𝚌𝚝])

The code, when run, is generating errors. Which fixes are needed to make the code work properly and return the list of products? (Choose two)


☐ Change the print statement to print(product)

☐ Remove the colon from the for statement

☐ Indent the print statement

☐ Indent the for statement

Change the print statement to print(product)

Indent the print statement


You need to Change the print statement to print(product), and Indent the for statement.
In evaluating the code, the products list is a tuple, not an array, so the print statement does not need square brackets nor a reference. However, because the print statement is part of a for loop, it needs to be indented. And, in Python, the beginning of a for statement ends in a colon. Thus, the code should look like this:

𝚙𝚛𝚘𝚍𝚞𝚌𝚝𝚜 = ("𝟷𝟷𝟷𝟷","𝟸𝟸𝟸𝟸","𝟹𝟹𝟹𝟹","𝟺𝟺𝟺𝟺","𝟻𝟻𝟻𝟻","𝟼𝟼𝟼𝟼","𝟽𝟽𝟽𝟽","𝟾𝟾𝟾𝟾","𝟿𝟿𝟿𝟿")

𝚏𝚘𝚛 𝚙𝚛𝚘𝚍𝚞𝚌𝚝 𝚒𝚗 𝚙𝚛𝚘𝚍𝚞𝚌𝚝𝚜:

⇥ 𝚙𝚛𝚒𝚗𝚝(𝚙𝚛𝚘𝚍𝚞𝚌𝚝)

50
New cards

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 either tried to divide by zero or used an invalid number.

Using the dropdown arrows, fill in the missing keywords to make this happen.

𝚊 = 𝚏𝚕𝚘𝚊𝚝(𝚒𝚗𝚙𝚞𝚝("𝙴𝚗𝚝𝚎𝚛 𝚊 𝚗𝚞𝚖𝚋𝚎𝚛. "))

𝚋 = 𝚏𝚕𝚘𝚊𝚝(𝚒𝚗𝚙𝚞𝚝("𝙴𝚗𝚝𝚎𝚛 𝚊 𝚗𝚞𝚖𝚋𝚎𝚛 𝚝𝚘 𝚍𝚒𝚟𝚒𝚍𝚎 𝚋𝚢. "))

(1) [ 𝚊𝚝𝚝𝚎𝚖𝚙𝚝 / 𝚝𝚛𝚢 / 𝚌𝚊𝚝𝚌𝚑 ] :

⇥ 𝚙𝚛𝚒𝚗𝚝(𝚏"𝚃𝚑𝚎 𝚊𝚗𝚜𝚠𝚎𝚛 𝚒𝚜 {𝚊/𝚋}.")

(2) [ 𝚌𝚊𝚝𝚌𝚑 / 𝚎𝚕𝚜𝚎 / 𝚎𝚡𝚌𝚎𝚙𝚝 / 𝚏𝚒𝚗𝚊𝚕𝚕𝚢 ] :

⇥ 𝚙𝚛𝚒𝚗𝚝("𝚃𝚑𝚒𝚜 𝚍𝚒𝚍 𝚗𝚘𝚝 𝚠𝚘𝚛𝚔. 𝙳𝚒𝚍 𝚢𝚘𝚞 𝚝𝚛𝚢 𝚝𝚘 𝚍𝚒𝚟𝚒𝚍𝚎 𝚋𝚢 𝚣𝚎𝚛𝚘?")

In order to display an error message when an error occurs, the code to run should be in a try block, and the code to run if there is an error should be in an except block.

The finished code looks like this:

𝚊 = 𝚏𝚕𝚘𝚊𝚝(𝚒𝚗𝚙𝚞𝚝("𝙴𝚗𝚝𝚎𝚛 𝚊 𝚗𝚞𝚖𝚋𝚎𝚛. "))

𝚋 = 𝚏𝚕𝚘𝚊𝚝(𝚒𝚗𝚙𝚞𝚝("𝙴𝚗𝚝𝚎𝚛 𝚊 𝚗𝚞𝚖𝚋𝚎𝚛 𝚝𝚘 𝚍𝚒𝚟𝚒𝚍𝚎 𝚋𝚢. "))

𝚝𝚛𝚢:

⇥ 𝚙𝚛𝚒𝚗𝚝(𝚏"𝚃𝚑𝚎 𝚊𝚗𝚜𝚠𝚎𝚛 𝚒𝚜 {𝚊/𝚋}.")

𝚎𝚡𝚌𝚎𝚙𝚝:

⇥ 𝚙𝚛𝚒𝚗𝚝("𝚃𝚑𝚒𝚜 𝚍𝚒𝚍 𝚗𝚘𝚝 𝚠𝚘𝚛𝚔. 𝙳𝚒𝚍 𝚢𝚘𝚞 𝚝𝚛𝚢 𝚝𝚘 𝚍𝚒𝚟𝚒𝚍𝚎 𝚋𝚢 𝚣𝚎𝚛𝚘 𝚘𝚛 𝚎𝚗𝚝𝚎𝚛 𝚊𝚗 𝚒𝚗𝚟𝚊𝚕𝚒𝚍 𝚗𝚞𝚖𝚋𝚎𝚛?")

51
New cards

Analyze the following code and then answer Yes for the statements that are true and No for the statements that are false.


𝚊 = 𝚏𝚕𝚘𝚊𝚝(𝚒𝚗𝚙𝚞𝚝("𝙴𝚗𝚝𝚎𝚛 𝚊 𝚗𝚞𝚖𝚋𝚎𝚛. "))

𝚋 = 𝚏𝚕𝚘𝚊𝚝(𝚒𝚗𝚙𝚞𝚝("𝙴𝚗𝚝𝚎𝚛 𝚊 𝚗𝚞𝚖𝚋𝚎𝚛 𝚝𝚘 𝚍𝚒𝚟𝚒𝚍𝚎 𝚋𝚢. "))

𝚝𝚛𝚢:

⇥ 𝚙𝚛𝚒𝚗𝚝(𝚏"𝚃𝚑𝚎 𝚊𝚗𝚜𝚠𝚎𝚛 𝚒𝚜 {𝚊/𝚋}.")

𝚎𝚡𝚌𝚎𝚙𝚝:

⇥ 𝚙𝚛𝚒𝚗𝚝("𝚃𝚑𝚒𝚜 𝚍𝚒𝚍 𝚗𝚘𝚝 𝚠𝚘𝚛𝚔. 𝙳𝚒𝚍 𝚢𝚘𝚞 𝚝𝚛𝚢 𝚝𝚘 𝚍𝚒𝚟𝚒𝚍𝚎 𝚋𝚢 𝚣𝚎𝚛𝚘?")

𝚎𝚕𝚜𝚎:

⇥ 𝚙𝚛𝚒𝚗𝚝("𝚈𝚘𝚞 𝚜𝚞𝚌𝚌𝚎𝚜𝚜𝚏𝚞𝚕𝚕𝚢 𝚍𝚒𝚟𝚒𝚍𝚎𝚍 𝚝𝚠𝚘 𝚗𝚞𝚖𝚋𝚎𝚛𝚜.")

𝚏𝚒𝚗𝚊𝚕𝚕𝚢:

⇥ 𝚙𝚛𝚒𝚗𝚝("𝚃𝚑𝚊𝚗𝚔 𝚢𝚘𝚞 𝚏𝚘𝚛 𝚙𝚕𝚊𝚢𝚒𝚗𝚐.")


  1. The finally statement will run regardless of which pieces of code above it run.

  2. Both the try and except parts will run if a = 0 and b != 0.

  3. The else part will run if a = 0 and b != 0.

  1. Yes

  2. No

  3. Yes

True statements:

  • The finally statement will run regardless of which pieces of code above it run.

  • The else part will run if a = 0 and b != 0.

False statements:

  • Both the try and except parts will run if a = 0 and b != 0.

Explanation:

  • The finally statement always runs when part of a try/except/finally block. Thus, the finally statement will print regardless of whether the try or except part runs.

  • The try and except parts do not both run as part of a try/except/finally block. Either the try part works or the except part runs.

  • The else part runs if the try part succeeds. Thus, with a = 0 no errors are introduced and the try part runs, so the else part runs, too.

52
New cards

A junior programmer writes the following code to store a list and then retrieve an item from the list:

trees = ['Douglas fir', 'Oak', 'Balsam fir']

last_tree = trees[3]

print(f'The last tree is {last_tree}.')

What type of error message (if this code generates an error) will this code produce?


○ A logic error

○ A runtime error

○ A syntax error

○ No error will be generated

A runtime error


The code example will generate a runtime error, specifically a list index out of range error, because lists are zero-based and thus the index items in the list are index 0, 1, and 2. There is no item 3 in the list.

The code will not generate a syntax error because there are no punctuation or spelling errors within the code.

Logic errors are not generated. They are a result of a misuse of operators or code being in the wrong order versus a desired result for code.

53
New cards

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 the correct order to create this code. Not all statements will be used.

𝚙𝚛𝚒𝚗𝚝 (𝚏"𝚂𝚝𝚞𝚍𝚎𝚗𝚝{𝚜𝚝𝚞𝚍𝚎𝚗𝚝}__")

𝚙𝚛𝚒𝚗𝚝(𝚏"𝙳𝚊𝚢: {𝚌𝚕𝚊𝚜𝚜𝙳𝚊𝚢}")

𝚏𝚘𝚛 𝚌𝚕𝚊𝚜𝚜𝙳𝚊𝚢 𝚒𝚗 𝚛𝚊𝚗𝚐𝚎 (𝟷,𝟽):

𝚏𝚘𝚛 𝚜𝚝𝚞𝚍𝚎𝚗𝚝 𝚒𝚗 𝚛𝚊𝚗𝚐𝚎 (𝟷,𝟷𝟶):

𝚏𝚘𝚛 𝚜𝚝𝚞𝚍𝚎𝚗𝚝 𝚒𝚗 𝚛𝚊𝚗𝚐𝚎 (𝟷,𝟷𝟷):

𝚏𝚘𝚛 𝚌𝚕𝚊𝚜𝚜𝙳𝚊𝚢 𝚒𝚗 𝚛𝚊𝚗𝚐𝚎 (𝟷,𝟾):

For this, two for loops are needed. The first loop is for the days. The second loop is for the students each day. And, the loops need to be nested so that each student can print within each day. Indentation is important as statements inside of a for statement on the same level of indentation run in tandem.

Python ranges are zero-based, so the range needs to end one number after the number of times the code needs to run (eight for seven days, 11 for 10 days).

Thus, the correct code looks like this:

𝚏𝚘𝚛 𝚌𝚕𝚊𝚜𝚜𝙳𝚊𝚢 𝚒𝚗 𝚛𝚊𝚗𝚐𝚎 (𝟷,𝟾):

⇥ 𝚙𝚛𝚒𝚗𝚝(𝚏"𝙳𝚊𝚢: {𝚌𝚕𝚊𝚜𝚜𝙳𝚊𝚢}")

⇥ 𝚏𝚘𝚛 𝚜𝚝𝚞𝚍𝚎𝚗𝚝 𝚒𝚗 𝚛𝚊𝚗𝚐𝚎 (𝟷,𝟷𝟷):

⇥ ⇥ 𝚙𝚛𝚒𝚗𝚝 (𝚏"𝚂𝚝𝚞𝚍𝚎𝚗𝚝{𝚜𝚝𝚞𝚍𝚎𝚗𝚝} _____________")

54
New cards

You are writing code to have activity for every day in a 30-day program. There should be no activity on day 15.

Using the dropdown arrows, fill in the missing pieces to the code.

(1) [ 𝚍𝚘 / 𝚏𝚘𝚛 / 𝚠𝚑𝚒𝚕𝚎 ] 𝚍𝚊𝚒𝚕𝚢𝙿𝚛𝚘𝚐𝚛𝚊𝚖 𝚒𝚗 𝚛𝚊𝚗𝚐𝚎(𝟷, (2) [ 𝟹𝟶 / 𝟹𝟷 / 𝟹𝟷, ] ):

⇥ 𝚒𝚏 𝚍𝚊𝚒𝚕𝚢𝙿𝚛𝚘𝚐𝚛𝚊𝚖 == 𝟷𝟻:

⇥ ⇥ 𝚙𝚛𝚒𝚗𝚝("𝙽𝚘 𝚊𝚌𝚝𝚒𝚟𝚒𝚝𝚢 𝚘𝚗 𝚍𝚊𝚢 𝟷𝟻")

⇥ ⇥ (3) [ 𝚜𝚔𝚒𝚙 / 𝚋𝚛𝚎𝚊𝚔 / 𝚌𝚘𝚗𝚝𝚒𝚗𝚞𝚎 ]

⇥ 𝚙𝚛𝚒𝚗𝚝(𝚏"𝚃𝚑𝚒𝚜 𝚒𝚜 𝚍𝚊𝚢 {𝚍𝚊𝚒𝚕𝚢𝙿𝚛𝚘𝚐𝚛𝚊𝚖}")

This loop should run a defined number of times, so it is a for loop.

As it needs to run 30 times, the range needs to extend to 31, as Python in a zero-based language.

The continue keyword skips an iteration in the loop.

The finished code looks like this:

𝚏𝚘𝚛 𝚍𝚊𝚒𝚕𝚢𝙿𝚛𝚘𝚐𝚛𝚊𝚖 𝚒𝚗 𝚛𝚊𝚗𝚐𝚎(𝟷,𝟹𝟷):

⇥ 𝚒𝚏 𝚍𝚊𝚒𝚕𝚢𝙿𝚛𝚘𝚐𝚛𝚊𝚖 == 𝟷𝟻:

⇥ ⇥ 𝚙𝚛𝚒𝚗𝚝("𝙽𝚘 𝚊𝚌𝚝𝚒𝚟𝚒𝚝𝚢 𝚘𝚗 𝚍𝚊𝚢 𝟷𝟻")

⇥ ⇥ 𝚌𝚘𝚗𝚝𝚒𝚗𝚞𝚎

⇥ 𝚙𝚛𝚒𝚗𝚝(𝚏"𝚃𝚑𝚒𝚜 𝚒𝚜 𝚍𝚊𝚢 {𝚍𝚊𝚒𝚕𝚢𝙿𝚛𝚘𝚐𝚛𝚊𝚖}")

55
New cards

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.

The staff should only get skis if the season is winter.

Using drag and drop, arrange the code pieces needed in the correct order. Not every code snippet will be used.

𝚒𝚏 𝚛𝚎𝚐𝚒𝚘𝚗 == "𝙽𝚘𝚛𝚝𝚑" && 𝚜𝚎𝚊𝚜𝚘𝚗 =="𝚆𝚒𝚗𝚝𝚎𝚛":

𝚎𝚕𝚜𝚎;

𝚎𝚕𝚜𝚎:

𝚒𝚏 𝚛𝚎𝚐𝚒𝚘𝚗 𝙸𝚂 "𝙽𝚘𝚛𝚝𝚑":

𝚒𝚏 𝚖𝚘𝚗𝚝𝚑𝚕𝚢𝚂𝚊𝚕𝚎𝚜 > 𝟷𝟶𝟶𝟶𝟶𝟶:

𝚙𝚛𝚒𝚗𝚝("𝚂𝚎𝚗𝚍 𝚐𝚘𝚕𝚏 𝚋𝚊𝚕𝚕𝚜")

𝚙𝚛𝚒𝚗𝚝("𝚂𝚎𝚗𝚍 𝚜𝚔𝚒𝚜")

𝚒𝚏 𝚖𝚘𝚗𝚝𝚑𝚕𝚢𝚂𝚊𝚕𝚎𝚜 = 𝟷𝟶𝟶𝟶𝟶𝟶:

The sales staff get no reward if they do not sell over $100,000 no matter their location, so that should be the first condition checked.

Then, if they sold over $100,000, the location and season should be checked to determine the reward they get. This is an example of a nested if with an else statement.

The finished code looks like this:

𝚒𝚏 𝚖𝚘𝚗𝚝𝚑𝚕𝚢𝚂𝚊𝚕𝚎𝚜 > 𝟷𝟶𝟶𝟶𝟶𝟶:

⇥ 𝚒𝚏 𝚛𝚎𝚐𝚒𝚘𝚗 == "𝙽𝚘𝚛𝚝𝚑" && 𝚜𝚎𝚊𝚜𝚘𝚗=="𝚆𝚒𝚗𝚝𝚎𝚛":

⇥ ⇥ 𝚙𝚛𝚒𝚗𝚝("𝚂𝚎𝚗𝚍 𝚜𝚔𝚒𝚜")

⇥ 𝚎𝚕𝚜𝚎:

⇥ ⇥ 𝚙𝚛𝚒𝚗𝚝("𝚂𝚎𝚗𝚍 𝚐𝚘𝚕𝚏 𝚋𝚊𝚕𝚕𝚜")

56
New cards

You are writing code to list cities and then delete, from the list, any city that has more than five letters in the name.

Using the dropdown arrows, fill in the missing pieces of code needed to complete the code needed to fulfill this task.

𝚌𝚒𝚝𝚒𝚎𝚜 = ['𝙰𝚗𝚌𝚑𝚘𝚛𝚊𝚐𝚎','𝙹𝚞𝚗𝚎𝚊𝚞','𝙵𝚊𝚒𝚛𝚋𝚊𝚗𝚔𝚜','𝙺𝚎𝚝𝚌𝚑𝚒𝚔𝚊𝚗','𝚂𝚒𝚝𝚔𝚊','𝚆𝚊𝚜𝚒𝚕𝚕𝚊']

𝚏𝚘𝚛 𝚌𝚒𝚝𝚢 𝚒𝚗 𝚌𝚒𝚝𝚒𝚎𝚜:

⇥ 𝚒𝚏 [ 𝚕𝚎𝚗 / 𝚕𝚎𝚗𝚐𝚝𝚑 / 𝚊𝚙𝚙𝚎𝚗𝚍 ] (𝚌𝚒𝚝𝚢) > 𝟻:

⇥ ⇥ 𝚌𝚒𝚝𝚒𝚎𝚜. 𝙲𝚑𝚘𝚘𝚜𝚎 𝙾𝚗𝚎...𝚛𝚎𝚖𝚘𝚟𝚎𝚍𝚎𝚕𝚎𝚝𝚎𝚜𝚕𝚒𝚌𝚎 (𝚌𝚒𝚝𝚢)

The len function returns the length of a value.

The remove function removes an item from a list.

The finished code looks like this:

𝚌𝚒𝚝𝚒𝚎𝚜 = ['𝙰𝚗𝚌𝚑𝚘𝚛𝚊𝚐𝚎','𝙹𝚞𝚗𝚎𝚊𝚞','𝙵𝚊𝚒𝚛𝚋𝚊𝚗𝚔𝚜','𝙺𝚎𝚝𝚌𝚑𝚒𝚔𝚊𝚗','𝚂𝚒𝚝𝚔𝚊','𝚆𝚊𝚜𝚒𝚕𝚕𝚊']

𝚏𝚘𝚛 𝚌𝚒𝚝𝚢 𝚒𝚗 𝚌𝚒𝚝𝚒𝚎𝚜:

⇥ 𝚒𝚏 𝚕𝚎𝚗(𝚌𝚒𝚝𝚢) > 𝟻:

⇥ ⇥ 𝚌𝚒𝚝𝚒𝚎𝚜.𝚛𝚎𝚖𝚘𝚟𝚎(𝚌𝚒𝚝𝚢)

57
New cards

Evaluate the following code:

𝚍𝚎𝚏 𝚜𝚌𝚘𝚛𝚎_𝚊𝚍𝚓 (𝚜𝚌𝚘𝚛𝚎, 𝚛𝚊𝚗𝚔):

⇥ 𝚗𝚎𝚠_𝚜𝚌𝚘𝚛𝚎 = 𝚜𝚌𝚘𝚛𝚎

⇥ 𝚒𝚏 𝚜𝚌𝚘𝚛𝚎 > 𝟹𝟶𝟶𝟶 𝚊𝚗𝚍 𝚛𝚊𝚗𝚔 > 𝟹:

⇥ ⇥ 𝚗𝚎𝚠_𝚜𝚌𝚘𝚛𝚎 += 𝟹𝟶𝟶

⇥ 𝚎𝚕𝚒𝚏 𝚜𝚌𝚘𝚛𝚎 > 𝟸𝟻𝟶𝟶 𝚊𝚗𝚍 𝚛𝚊𝚗𝚔 > 𝟸:

⇥ ⇥ 𝚗𝚎𝚠_𝚜𝚌𝚘𝚛𝚎 += 𝟸𝟻𝟶

⇥ 𝚎𝚕𝚜𝚎:

⇥ ⇥ 𝚗𝚎𝚠_𝚜𝚌𝚘𝚛𝚎 += 𝟻𝟶

⇥ 𝚛𝚎𝚝𝚞𝚛𝚗 𝚗𝚎𝚠_𝚜𝚌𝚘𝚛𝚎

𝚜𝚌𝚘𝚛𝚎𝟷 = 𝚜𝚌𝚘𝚛𝚎_𝚊𝚍𝚓 (𝟹𝟶𝟶𝟶, 𝟹)

𝚜𝚌𝚘𝚛𝚎𝟸 = 𝚜𝚌𝚘𝚛𝚎_𝚊𝚍𝚓 (𝟸𝟶𝟶𝟶, 𝟸)

𝚜𝚌𝚘𝚛𝚎𝟹 = 𝚜𝚌𝚘𝚛𝚎_𝚊𝚍𝚓 (𝟻𝟶𝟶𝟶, 𝟻)

𝚙𝚛𝚒𝚗𝚝(𝚜𝚌𝚘𝚛𝚎𝟷, 𝚜𝚌𝚘𝚛𝚎𝟸, 𝚜𝚌𝚘𝚛𝚎𝟹)

For each statement regarding the results of the code, indicate a Yes if the statement is true and No if the statement is false.

  1. score1 = 3300

  2. score3 = 5300

  3. score2 = 2250

  1. No

  2. Yes

  3. No


True statements:

  • score3 = 5300

False statements:

  • score2 = 2250

  • score1 = 3300

Explanation:

  • score1 does not meet the criteria in the if statement as it does not exceed 3000 and its rank does not exceed 3. It does match the elif statement and thus 250 is added to its score, making the score 3250.

  • score2 does not meet the criteria in the if or elif statements as it does not exceed 2000 and its rank does not exceed 2. Thus, 50 is added to the score, making the score 2050.

  • score3 exceeds the criteria in the if statement, thus 300 is added to the score to make score3 5300.

58
New cards

You are trying to generate 10 sets of random numbers. One random number is between 3 and 99, with the number being a multiple of 3.

The other random number needs to be between 0 and 1.

Drag the snippets of code needed to complete the code block, which a small part of a game app.

𝚏𝚘𝚛 𝚒 𝚒𝚗 𝚛𝚊𝚗𝚐𝚎(𝟷𝟶):

⇥ 𝚙𝚛𝚒𝚗𝚝( [ (1) ] )

⇥ 𝚙𝚛𝚒𝚗𝚝( [ (2) ] )


𝚛𝚊𝚗𝚍𝚛𝚊𝚗𝚐𝚎(𝟹,𝟷𝟶𝟸,𝟹)

𝚛𝚊𝚗𝚍𝚒𝚗𝚝(𝟹,𝟷𝟶𝟸,𝟹)

𝚛𝚊𝚗𝚍𝚘𝚖()

𝚏𝚘𝚛 𝚒 𝚒𝚗 𝚛𝚊𝚗𝚐𝚎(𝟿):

𝚛𝚊𝚗𝚍𝚛𝚊𝚗𝚐𝚎(𝟹,𝟿𝟿,𝟹)

𝚛𝚊𝚗𝚍𝚒𝚗𝚝()

To generate a number between 3 and 99 and have it be multiples of 3, the randrange function is used, starting at 3, ending at 102 (ending before the last number in the range) and adding a step of 3.

The random function generates a random number between 0 and 1

Thus, the completed code block looks like this:

𝚏𝚘𝚛 𝚒 𝚒𝚗 𝚛𝚊𝚗𝚐𝚎(𝟷𝟶):

⇥ 𝚙𝚛𝚒𝚗𝚝(𝚛𝚊𝚗𝚍𝚛𝚊𝚗𝚐𝚎(𝟹,𝟷𝟶𝟸,𝟹))

⇥ 𝚙𝚛𝚒𝚗𝚝(𝚛𝚊𝚗𝚍𝚘𝚖())

59
New cards

A developer wants to print the first line of a configuration file. The code to perform this task is written as follows:

#𝚒𝚖𝚙𝚘𝚛𝚝 𝚖𝚘𝚍𝚞𝚕𝚎

𝚒𝚏 𝚘𝚜.𝚙𝚊𝚝𝚑.𝚒𝚜𝚏𝚒𝚕𝚎('𝚌𝚘𝚗𝚏𝚒𝚐.𝚝𝚡𝚝'):

⇥ 𝚠𝚒𝚝𝚑 𝚘𝚙𝚎𝚗('𝚌𝚘𝚗𝚏𝚒𝚐.𝚝𝚡𝚝','𝚛') 𝚊𝚜 𝚏𝚒𝚕𝚎:

⇥ ⇥ 𝚙𝚛𝚒𝚗𝚝(𝚏𝚒𝚕𝚎.#𝚛𝚎𝚊𝚍 𝚝𝚑𝚎 𝚏𝚒𝚛𝚜𝚝 𝚕𝚒𝚗𝚎)

Which code snippets are needed to replace the comments and finish this code example? (Choose two)

☐ 𝚛𝚎𝚊𝚍()

☐ 𝚒𝚖𝚙𝚘𝚛𝚝 𝚒𝚘

☐ 𝚛𝚎𝚊𝚍𝚕𝚒𝚗𝚎()

☐ 𝚒𝚖𝚙𝚘𝚛𝚝 𝚘𝚜.𝚙𝚊𝚝𝚑

𝚛𝚎𝚊𝚍𝚕𝚒𝚗𝚎()

𝚒𝚖𝚙𝚘𝚛𝚝 𝚘𝚜.𝚙𝚊𝚝𝚑


The os.path module needs to be imported to use the .isfile method to check to see if a file exists. The io module does not check to see if a file exists. The readline method reads a line within a file and the first line of the file the first time readline is invoked. The read method reads an entire file.

#𝚒𝚖𝚙𝚘𝚛𝚝 𝚖𝚘𝚍𝚞𝚕𝚎

𝚒𝚏 𝚘𝚜.𝚙𝚊𝚝𝚑.𝚒𝚜𝚏𝚒𝚕𝚎('𝚌𝚘𝚗𝚏𝚒𝚐.𝚝𝚡𝚝'):

⇥ 𝚠𝚒𝚝𝚑 𝚘𝚙𝚎𝚗('𝚌𝚘𝚗𝚏𝚒𝚐.𝚝𝚡𝚝','𝚛') 𝚊𝚜 𝚏𝚒𝚕𝚎:

⇥ ⇥ 𝚙𝚛𝚒𝚗𝚝(𝚏𝚒𝚕𝚎.#𝚛𝚎𝚊𝚍 𝚝𝚑𝚎 𝚏𝚒𝚛𝚜𝚝 𝚕𝚒𝚗𝚎)

60
New cards

A developer is setting up several assert tests for an app. The tests are as follows:

test1 should test to see if a calculation is true or false.

test2 should test to see if an item is within a list of items.

test3 should test to see if an object belongs to a class.

Using the dropdown lists, choose the correct assert test for each unit test.

𝚍𝚎𝚏 𝚝𝚎𝚜𝚝𝟷(𝚜𝚎𝚕𝚏):

⇥ 𝚜𝚎𝚕𝚏. [ (1) ] (𝟻+𝟹 * 𝟺 == 𝟷𝟽)

𝚍𝚎𝚏 𝚝𝚎𝚜𝚝𝟸(𝚜𝚎𝚕𝚏):

⇥ 𝚊 = '𝚌𝚕𝚘𝚌𝚔'

⇥ 𝚋 = ['𝚌𝚕𝚘𝚌𝚔','𝚠𝚊𝚝𝚌𝚑','𝚙𝚑𝚘𝚗𝚎']

⇥ 𝚜𝚎𝚕𝚏. [ (2) ] (𝚊,𝚋)

𝚍𝚎𝚏 𝚝𝚎𝚜𝚝𝟹(𝚜𝚎𝚕𝚏):

⇥ 𝚐𝚊𝚖𝚎 = 𝙶𝚊𝚖𝚎()

⇥ 𝚜𝚎𝚕𝚏. [ (3) ] (𝚐𝚊𝚖𝚎, 𝙶𝚊𝚖𝚎)


𝚊𝚜𝚜𝚎𝚛𝚝𝙸𝚗𝚜𝚝𝚊𝚗𝚌𝚎

𝚊𝚜𝚜𝚎𝚛𝚝𝚕𝚜

𝚊𝚜𝚜𝚎𝚛𝚝𝙴𝚚𝚞𝚊𝚕

𝚊𝚜𝚜𝚎𝚛𝚝𝚃𝚛𝚞𝚎

𝚊𝚜𝚜𝚎𝚛𝚝𝙸𝚜𝙸𝚗𝚜𝚝𝚊𝚗𝚌𝚎

𝚊𝚜𝚜𝚎𝚛𝚝𝚒𝚗𝙸𝚗𝚜𝚝𝚊𝚗𝚌𝚎

𝚊𝚜𝚜𝚎𝚛𝚝𝚒𝚗

test1 needs the assertTrue testing method to see if a calculation is true or false

test2 needs the assertIn testing method to see if an item is within a list of items.

test3 needs the assertIsInstance testing method to see if an object is an instance of a class.

The completed code looks like this:

𝚍𝚎𝚏 𝚝𝚎𝚜𝚝𝟷(𝚜𝚎𝚕𝚏):

⇥ 𝚜𝚎𝚕𝚏.𝚊𝚜𝚜𝚎𝚛𝚝𝚃𝚛𝚞𝚎(𝟻+𝟹 * 𝟺 == 𝟷𝟽)

𝚍𝚎𝚏 𝚝𝚎𝚜𝚝𝟸(𝚜𝚎𝚕𝚏):

⇥ 𝚊 = '𝚌𝚕𝚘𝚌𝚔'

⇥ 𝚋 = ['𝚌𝚕𝚘𝚌𝚔','𝚠𝚊𝚝𝚌𝚑','𝚙𝚑𝚘𝚗𝚎']

⇥ 𝚜𝚎𝚕𝚏.𝚊𝚜𝚜𝚎𝚛𝚝𝙸𝚗(𝚊,𝚋)

𝚍𝚎𝚏 𝚝𝚎𝚜𝚝𝟹(𝚜𝚎𝚕𝚏):

⇥ 𝚐𝚊𝚖𝚎 = 𝙶𝚊𝚖𝚎()

⇥ 𝚜𝚎𝚕𝚏.𝚊𝚜𝚜𝚎𝚛𝚝𝙸𝚜𝙸𝚗𝚜𝚝𝚊𝚗𝚌𝚎(𝚐𝚊𝚖𝚎, 𝙶𝚊𝚖𝚎)

61
New cards

You are trying to calculate the area of a rectangle given a width and height input from a user. The width and height need to be integers. The calculated square footage needs to be right-aligned in the line and be no more than six characters long.

Use the dropdown arrows to complete the code to allow a user to input the height and width and then calculate and display the area of the ensuing rectangle.

𝚠𝚒𝚍𝚝𝚑 = (1) [ 𝚒𝚗𝚝 / 𝚏𝚕𝚘𝚊𝚝 / 𝚒𝚗𝚙𝚞𝚝 ] ( (2) [ 𝚒𝚗𝚝 / 𝚏𝚕𝚘𝚊𝚝 / 𝚒𝚗𝚙𝚞𝚝 ] ("𝙴𝚗𝚝𝚎𝚛 𝚊 𝚠𝚒𝚍𝚝𝚑: "))

𝚑𝚎𝚒𝚐𝚑𝚝 = (3) [ 𝚒𝚗𝚝 / 𝚏𝚕𝚘𝚊𝚝 / 𝚒𝚗𝚙𝚞𝚝 ] ( (4) [ 𝚒𝚗𝚝 / 𝚏𝚕𝚘𝚊𝚝 /. 𝚒𝚗𝚙𝚞𝚝 ] ("𝙴𝚗𝚝𝚎𝚛 𝚊 𝚑𝚎𝚒𝚐𝚑𝚝: "))

𝚊𝚛𝚎𝚊 = "𝚆𝚎 𝚑𝚊𝚟𝚎 (5) [ {:>𝟼} / {𝚊𝚕𝚒𝚐𝚗.𝚛𝚒𝚐𝚑𝚝(𝟼)}. /. {𝚛𝚊𝚕𝚒𝚐𝚗(𝟼)} ] 𝚜𝚚𝚞𝚊𝚛𝚎 𝚏𝚎𝚎𝚝 𝚏𝚘𝚛 𝚝𝚑𝚎 𝚊𝚛𝚎𝚊."

𝚙𝚛𝚒𝚗𝚝( (6) [ 𝚊𝚛𝚎𝚊.𝚏𝚘𝚛𝚖𝚊𝚝(𝚠𝚒𝚍𝚝𝚑*𝚑𝚎𝚒𝚐𝚑𝚝) / 𝚏 / 𝚛𝚒𝚐𝚑𝚝𝚊𝚕𝚒𝚐𝚗(𝚊𝚛𝚎𝚊) ] )

To limit the inputs to whole numbers, the int keyword needs to be specified before the input keyword.

The :>6 indicator is a string format mechanism

The string.format() method is invoked on the area variable to print the area in the format specified in the area variable declaration.

The completed code is as follows:

𝚠𝚒𝚍𝚝𝚑 = 𝚒𝚗𝚝(𝚒𝚗𝚙𝚞𝚝("𝙴𝚗𝚝𝚎𝚛 𝚊 𝚠𝚒𝚍𝚝𝚑: "))

𝚑𝚎𝚒𝚐𝚑𝚝 = 𝚒𝚗𝚝(𝚒𝚗𝚙𝚞𝚝("𝙴𝚗𝚝𝚎𝚛 𝚊 𝚑𝚎𝚒𝚐𝚑𝚝: "))

𝚊𝚛𝚎𝚊 = "𝚆𝚎 𝚑𝚊𝚟𝚎 {:>𝟼} 𝚜𝚚𝚞𝚊𝚛𝚎 𝚏𝚎𝚎𝚝 𝚏𝚘𝚛 𝚝𝚑𝚎 𝚊𝚛𝚎𝚊."

𝚙𝚛𝚒𝚗𝚝(𝚊𝚛𝚎𝚊.𝚏𝚘𝚛𝚖𝚊𝚝(𝚠𝚒𝚍𝚝𝚑*𝚑𝚎𝚒𝚐𝚑𝚝))

62
New cards

Evaluate the following code:

𝚒𝚖𝚙𝚘𝚛𝚝 𝚜𝚢𝚜

𝚡 = 𝚕𝚎𝚗(𝚜𝚢𝚜.𝚊𝚛𝚐𝚟)

𝚙𝚛𝚒𝚗𝚝(𝚡)

𝚙𝚛𝚒𝚗𝚝(𝚜𝚢𝚜.𝚊𝚛𝚐𝚟[𝟶])

𝚙𝚛𝚒𝚗𝚝(𝚜𝚢𝚜.𝚊𝚛𝚐𝚟[𝟷])

What does sys.argv[0] represent when a command is run to refer to this file as:

𝙿𝚢𝚝𝚑𝚘𝚗 𝚝𝚎𝚜𝚝𝚊𝚛𝚐𝚜.𝚙𝚢 𝙷𝚎𝚕𝚕𝚘


○ The number of arguments passed through the file.

○ Hello

○ 𝚝𝚎𝚜𝚝𝚊𝚛𝚐𝚜.𝚙𝚢

○ 𝚜𝚢𝚜.𝚊𝚛𝚐𝚟

𝚝𝚎𝚜𝚝𝚊𝚛𝚐𝚜.𝚙𝚢


When using command-line arguments, sys.argv is a list structure that contains the name of a .py file and the arguments that pass through the file. sys.argv[0] represents the name of the file. sys.argv[1] represents the first argument passed through a file, which is the word, Hello. The number of arguments passed through the file is represented in the variable x, not in the command-line call to the file.

63
New cards

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 dropdown arrow, complete the code to fulfill this task, making sure the file does not take up memory after the write is complete.

𝚠𝚛𝚒𝚝𝚎𝙵𝚒𝚕𝚎 = (1) [ 𝚏𝚎𝚝𝚌𝚑 / 𝚘𝚙𝚎𝚗 / 𝚠𝚛𝚒𝚝𝚎. ] ('𝚛𝚎𝚜𝚞𝚕𝚝𝚜.𝚝𝚡𝚝', ' (2) [ 𝚊 / 𝚛 / 𝚠 ] ')

𝚝𝚘𝚁𝚎𝚜𝚞𝚕𝚝𝚜 = 𝚒𝚗𝚙𝚞𝚝("𝚆𝚑𝚊𝚝 𝚍𝚘 𝚢𝚘𝚞 𝚠𝚊𝚗𝚝 𝚝𝚘 𝚠𝚛𝚒𝚝𝚎 𝚝𝚘 𝚝𝚑𝚎 𝚛𝚎𝚜𝚞𝚕𝚝𝚜? ")

𝚠𝚛𝚒𝚝𝚎𝙵𝚒𝚕𝚎. (3) [ 𝚏𝚎𝚎𝚍 / 𝚠𝚛𝚒𝚝𝚎 / 𝚠𝚛𝚒𝚝𝚎𝚕𝚗 ] (𝚝𝚘𝚁𝚎𝚜𝚞𝚕𝚝𝚜)

𝚠𝚛𝚒𝚝𝚎𝙵𝚒𝚕𝚎. (4) [ 𝚌𝚕𝚘𝚜𝚎() / 𝚍𝚛𝚘𝚙() / 𝚎𝚗𝚍() ]

To write to a file, it must first be opened.

Then, the write command writes to the file.

The w command indicates the write should go to a new file.

The close() command removes the file from memory.

Thus, the completed code looks like this:

𝚠𝚛𝚒𝚝𝚎𝙵𝚒𝚕𝚎 = 𝚘𝚙𝚎𝚗('𝚛𝚎𝚜𝚞𝚕𝚝𝚜.𝚝𝚡𝚝', '𝚠')

𝚝𝚘𝚁𝚎𝚜𝚞𝚕𝚝𝚜 = 𝚒𝚗𝚙𝚞𝚝("𝚆𝚑𝚊𝚝 𝚍𝚘 𝚢𝚘𝚞 𝚠𝚊𝚗𝚝 𝚝𝚘 𝚠𝚛𝚒𝚝𝚎 𝚝𝚘 𝚝𝚑𝚎 𝚛𝚎𝚜𝚞𝚕𝚝𝚜? ")

𝚠𝚛𝚒𝚝𝚎𝙵𝚒𝚕𝚎.𝚠𝚛𝚒𝚝𝚎(𝚝𝚘𝚁𝚎𝚜𝚞𝚕𝚝𝚜)

𝚠𝚛𝚒𝚝𝚎𝙵𝚒𝚕𝚎.𝚌𝚕𝚘𝚜𝚎()

64
New cards

You want to write text from a user input. 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.

If the file does exist, new text to needs to be written to a new line.

Using drag and drop, finish the code. Not all options will be used.

𝚒𝚏 𝚘𝚜.𝚙𝚊𝚝𝚑.𝚒𝚜𝚏𝚒𝚕𝚎('𝚛𝚎𝚜𝚞𝚕𝚝𝚜.𝚝𝚡𝚝'):

⇥ 𝚠𝚛𝚒𝚝𝚎𝙵𝚒𝚕𝚎 = 𝚘𝚙𝚎𝚗('𝚛𝚎𝚜𝚞𝚕𝚝𝚜.𝚝𝚡𝚝', ' [ (1) ] ')

𝚎𝚕𝚜𝚎:

⇥ 𝚠𝚛𝚒𝚝𝚎𝙵𝚒𝚕𝚎 = 𝚘𝚙𝚎𝚗('𝚛𝚎𝚜𝚞𝚕𝚝𝚜.𝚝𝚡𝚝', ' [ (2) ] ')

𝚝𝚘𝚁𝚎𝚜𝚞𝚕𝚝𝚜 = 𝚒𝚗𝚙𝚞𝚝("𝚆𝚑𝚊𝚝 𝚍𝚘 𝚢𝚘𝚞 𝚠𝚊𝚗𝚝 𝚝𝚘 𝚠𝚛𝚒𝚝𝚎 𝚝𝚘 𝚝𝚑𝚎 𝚕𝚘𝚐? ")

𝚠𝚛𝚒𝚝𝚎𝙵𝚒𝚕𝚎.𝚠𝚛𝚒𝚝𝚎("\ [ (3) ] " + 𝚝𝚘𝚁𝚎𝚜𝚞𝚕𝚝𝚜)

𝚠𝚛𝚒𝚝𝚎𝙵𝚒𝚕𝚎.𝚌𝚕𝚘𝚜𝚎()


𝙸

𝚠

𝚎

𝚛

𝚗

𝚊

When writing to file that may exist, the open command with the 'a' attribute opens an existing file and appends to that file.

The 'w' attribute creates a new file and writes to it.

The \n command writes text to a new line.

Thus, the code to write to a file with the code to account for either an existing file or creation of a new file is as follows:

𝚒𝚖𝚙𝚘𝚛𝚝 𝚘𝚜

𝚒𝚏 𝚘𝚜.𝚙𝚊𝚝𝚑.𝚒𝚜𝚏𝚒𝚕𝚎('𝚛𝚎𝚜𝚞𝚕𝚝𝚜.𝚝𝚡𝚝'):

⇥ 𝚠𝚛𝚒𝚝𝚎𝙵𝚒𝚕𝚎 = 𝚘𝚙𝚎𝚗('𝚛𝚎𝚜𝚞𝚕𝚝𝚜.𝚝𝚡𝚝', '𝚊')

𝚎𝚕𝚜𝚎:

⇥ 𝚠𝚛𝚒𝚝𝚎𝙵𝚒𝚕𝚎 = 𝚘𝚙𝚎𝚗('𝚛𝚎𝚜𝚞𝚕𝚝𝚜.𝚝𝚡𝚝', '𝚠')

𝚝𝚘𝚁𝚎𝚜𝚞𝚕𝚝𝚜 = 𝚒𝚗𝚙𝚞𝚝("𝚆𝚑𝚊𝚝 𝚍𝚘 𝚢𝚘𝚞 𝚠𝚊𝚗𝚝 𝚝𝚘 𝚠𝚛𝚒𝚝𝚎 𝚝𝚘 𝚝𝚑𝚎 𝚕𝚘𝚐? ")

𝚠𝚛𝚒𝚝𝚎𝙵𝚒𝚕𝚎.𝚠𝚛𝚒𝚝𝚎("\𝚗" + 𝚝𝚘𝚁𝚎𝚜𝚞𝚕𝚝𝚜)

𝚠𝚛𝚒𝚝𝚎𝙵𝚒𝚕𝚎.𝚌𝚕𝚘𝚜𝚎()

65
New cards

You are in the 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 dropdown arrows, select the proper code pieces to finish this code example.

𝚒𝚖𝚙𝚘𝚛𝚝 (1) [ 𝚘𝚜 / 𝚘𝚜.𝚙𝚊𝚝𝚑. /. 𝚘𝚜.𝚏𝚒𝚕𝚎. ]

𝚒𝚏 𝚘𝚜.𝚙𝚊𝚝𝚑. (2) [ 𝚒𝚜𝚏𝚒𝚕𝚎 / 𝚎𝚡𝚒𝚜𝚝𝚜 / 𝚙𝚛𝚎𝚜𝚎𝚗𝚝 ] ('𝚛𝚎𝚜𝚞𝚕𝚝𝚜.𝚝𝚡𝚝'):

⇥ 𝚘𝚜. (3) [ 𝚍𝚎𝚕𝚎𝚝𝚎 / 𝚛𝚎𝚖𝚘𝚟𝚎 / 𝚌𝚕𝚘𝚜𝚎 ] ('𝚛𝚎𝚜𝚞𝚕𝚝𝚜.𝚝𝚡𝚝')

⇥ 𝚙𝚛𝚒𝚗𝚝("𝚃𝚑𝚎 𝚛𝚎𝚜𝚞𝚕𝚝𝚜 𝚏𝚒𝚕𝚎 𝚑𝚊𝚜 𝚋𝚎𝚎𝚗 𝚛𝚎𝚖𝚘𝚟𝚎𝚍.")

𝚎𝚕𝚜𝚎:

⇥ 𝚙𝚛𝚒𝚗𝚝("𝚃𝚑𝚎𝚛𝚎 𝚠𝚊𝚜 𝚗𝚘 𝚛𝚎𝚜𝚞𝚕𝚝𝚜 𝚏𝚒𝚕𝚎 𝚝𝚘 𝚛𝚎𝚖𝚘𝚟𝚎.")

In order to delete a file, the os module must first be imported.

Then, the os.path.isfile command runs as a boolean test to see if the file indicated exists.

If it does, the os.remove command deletes the file.

The completed code example looks like this:

𝚒𝚖𝚙𝚘𝚛𝚝 𝚘𝚜

𝚒𝚏 𝚘𝚜.𝚙𝚊𝚝𝚑.𝚒𝚜𝚏𝚒𝚕𝚎('𝚛𝚎𝚜𝚞𝚕𝚝𝚜.𝚝𝚡𝚝'):

⇥ 𝚘𝚜.𝚛𝚎𝚖𝚘𝚟𝚎('𝚛𝚎𝚜𝚞𝚕𝚝𝚜.𝚝𝚡𝚝')

⇥ 𝚙𝚛𝚒𝚗𝚝("𝚃𝚑𝚎 𝚛𝚎𝚜𝚞𝚕𝚝𝚜 𝚏𝚒𝚕𝚎 𝚑𝚊𝚜 𝚋𝚎𝚎𝚗 𝚛𝚎𝚖𝚘𝚟𝚎𝚍.")

𝚎𝚕𝚜𝚎:

⇥ 𝚙𝚛𝚒𝚗𝚝("𝚃𝚑𝚎𝚛𝚎 𝚠𝚊𝚜 𝚗𝚘 𝚛𝚎𝚜𝚞𝚕𝚝𝚜 𝚏𝚒𝚕𝚎 𝚝𝚘 𝚛𝚎𝚖𝚘𝚟𝚎.")

66
New cards

What would be the value of result that prints at the end of this code?

𝚊 = 𝟹

𝚋 = 𝟽

𝚌 = 𝟻

𝚛𝚎𝚜𝚞𝚕𝚝 = 𝚊 + 𝚋 * 𝚌

𝚙𝚛𝚒𝚗𝚝 (𝚛𝚎𝚜𝚞𝚕𝚝)


○ 36

○ 38

○ 26

○ 50

38


Order of operations dictates that multiplication and division calculate before addition and subtraction unless there are parentheses present.

In this code, there are no parentheses, so b * c, which equals 35, is calculated first.

Then, 3 is added, making the total 38. The answer is not 50, as the result would need to be (a+b) c to make 10 5 = 50.

a * b + c = 26

a * (b + c) = 36

67
New cards

Evaluate the following code and then select Yes for any true statement related to the code and No for any false statement related to the code:

𝚒𝚖𝚙𝚘𝚛𝚝 𝚖𝚊𝚝𝚑

𝚊 = -𝟷𝟺

𝚋 = 𝟸𝟷

𝚌 = 𝚖𝚊𝚝𝚑.𝚏𝚊𝚋𝚜(𝚊)

𝚍 = 𝚖𝚊𝚝𝚑.𝚏𝚖𝚘𝚍(𝚋,𝚊)

𝚎 = 𝚖𝚊𝚝𝚑.𝚏𝚛𝚎𝚡𝚙(𝚋)

  1. frexp returns the mantissa and exponent of a number

  2. c == 14.0

  3. d == 1.5

  1. Yes

  2. No

  3. Yes


True statements:

  • frexp returns the mantissa and exponent of a number

  • c == 14.0

False statements:

  • d == 1.5

Explanation:

  • d = 7.0 as fmod returns the floating modulus of two numbers. 21/-14 = -1 with a remainder of 7, and a floating number carries a decimal, hence 7.0 is the result.

68
New cards

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

1) Boolean

2) Integer

3) String

4) Float

a) "False"

b) 1.0

c) False

d) 1

1) + c) = Boolean - False

2) + d) = Integer - 1

3) + a) = String - “False“

4) + b) = Float - 1.0


The word False represents a boolean unless it is in quotes, in which case it is a string.

Whole numbers are integers.

Numbers with decimals are a float data type, even if there are nothing but zeros after the decimal point.

69
New cards

Evaluate the following code:

𝚍𝚎𝚏 𝚒𝚜_𝚚𝚞𝚊𝚛𝚝𝚎𝚛(𝚗𝚞𝚖):

⇥ 𝚒𝚏 𝚗𝚞𝚖 % 𝟺 == 𝟶:

⇥ ⇥ 𝚛𝚎𝚝𝚞𝚛𝚗 𝚃𝚛𝚞𝚎

⇥ 𝚎𝚕𝚜𝚎:

⇥ ⇥ 𝚛𝚎𝚝𝚞𝚛𝚗 𝙵𝚊𝚕𝚜𝚎

𝚊𝚜𝚜𝚎𝚛𝚝 𝚒𝚜_𝚚𝚞𝚊𝚛𝚝𝚎𝚛(𝟾) == 𝚃𝚛𝚞𝚎

What will the code return?

○ 𝚃𝚛𝚞𝚎

An AssertionError error

○ 𝙵𝚊𝚕𝚜𝚎

Nothing

Nothing

The code will return nothing as, when an assert test on a function returns true, as is the case with 8 % 4 = 0, nothing is returned.

Asserts do not return True or False.

If an assert is false, an AssertionError instance is raised.

70
New cards

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.

Using the dropdown arrow, choose the correct operator to finish the code.

𝚊 = 𝟹

𝚋 = 𝟽

𝚛𝚎𝚜𝚞𝚕𝚝 = 𝚒𝚗𝚝(𝚒𝚗𝚙𝚞𝚝("𝚆𝚑𝚊𝚝 𝚒𝚜 𝚊 + 𝚋?"))

𝚒𝚏 𝚛𝚎𝚜𝚞𝚕𝚝 (1) [ != / = / ! / > ] 𝟷𝟶:

⇥ 𝚙𝚛𝚒𝚗𝚝("𝚈𝚘𝚞 𝚊𝚛𝚎 𝚒𝚗𝚌𝚘𝚛𝚛𝚎𝚌𝚝. 𝚃𝚛𝚢 𝚊𝚐𝚊𝚒𝚗")

𝚎𝚕𝚜𝚎:

⇥ 𝚙𝚛𝚒𝚗𝚝("𝚈𝚘𝚞 𝚊𝚛𝚎 𝚌𝚘𝚛𝚛𝚎𝚌𝚝.")

!=


The Python operator for not equals is the != symbol.

Therefore, the finished code looks like this:

𝚊 = 𝟹

𝚋 = 𝟽

𝚛𝚎𝚜𝚞𝚕𝚝 = 𝚒𝚗𝚝(𝚒𝚗𝚙𝚞𝚝("𝚆𝚑𝚊𝚝 𝚒𝚜 𝚊 + 𝚋?"))

𝚒𝚏 𝚛𝚎𝚜𝚞𝚕𝚝 != 𝟷𝟶:

⇥ 𝚙𝚛𝚒𝚗𝚝("𝚈𝚘𝚞 𝚊𝚛𝚎 𝚒𝚗𝚌𝚘𝚛𝚛𝚎𝚌𝚝. 𝚃𝚛𝚢 𝚊𝚐𝚊𝚒𝚗")

𝚎𝚕𝚜𝚎:

⇥ 𝚙𝚛𝚒𝚗𝚝("𝚈𝚘𝚞 𝚊𝚛𝚎 𝚌𝚘𝚛𝚛𝚎𝚌𝚝.")

71
New cards

Evaluate the following code. For each statement regarding the results of the calculations, select Yes if the statement is true and No if the statement is false.

𝚊 = 𝟹

𝚋 = 𝟸

𝚌 = -(𝚊**𝚋)

𝚍 = +(𝚌*𝚋)

  1. 𝚌 = -𝟿

  2. 𝚋**𝚊 = 𝟾

  3. 𝚍 = 𝟷𝟾

  1. Yes

  2. Yes

  3. No


True statements:

  • c = -9

  • b**a = 8

False statements:

  • d = 18

Explanation:

  • a**b is a raised to the power of b. 3**2 = 9 and the - in front of the calculation makes c -9

  • b**a is b raised to the power of a. 2**3 = 8.

  • d is not 18, because the + unary operator leaves an existing sign the same, - flips the sign from positive to negative or negative to positive. Thus, d = -18.

72
New cards

A tester wants to select every fifth item in the products tuple for functional testing, starting with the second item in the tuple and ending at the ninth item in the tuple.

The result of that selection should be in the print statement.

Using drag and drop, fill in the missing pieces to the code to create the selection and the print statement. Not all code pieces will be used.

𝚙𝚛𝚘𝚍𝚞𝚌𝚝𝚜 =('𝟷𝟷𝟷𝟷','𝟸𝟸𝟸𝟸','𝟹𝟹𝟹𝟹','𝟺𝟺𝟺𝟺','𝟻𝟻𝟻𝟻','𝟼𝟼𝟼𝟼','𝟽𝟽𝟽𝟽','𝟾𝟾𝟾𝟾','𝟿𝟿𝟿𝟿')

[ (1) ]

𝚙𝚛𝚒𝚗𝚝( [ (2) ] )


𝚝𝚎𝚜𝚝𝚎𝚛 = 𝚜𝚕𝚒𝚌𝚎(𝟸,𝟿,𝟻)

𝚝𝚎𝚜𝚝𝚎𝚛 = 𝚙𝚛𝚘𝚍𝚞𝚌𝚝𝚜.𝚜𝚕𝚒𝚌𝚎(𝟷,𝟾,𝟻)

𝚙𝚛𝚘𝚍𝚞𝚌𝚝𝚜[𝚝𝚎𝚜𝚝𝚎𝚛]

𝚏𝚘𝚛 𝚝𝚎𝚜𝚝𝚎𝚛 𝚒𝚗 𝚙𝚛𝚘𝚍𝚞𝚌𝚝𝚜:

𝚙𝚛𝚘𝚍𝚞𝚌𝚝𝚜(𝚝𝚎𝚜𝚝𝚎𝚛)

𝚊𝚙𝚙𝚎𝚗𝚍

𝚝𝚎𝚜𝚝𝚎𝚛 = 𝚜𝚕𝚒𝚌𝚎(𝟷,𝟾,𝟻)

The slice method obtains items from a list, array, or tuple. The code technically presents a tuple, not a list.

The slice method is used with this syntax: (start, end, interval). With Python being zero-based, the start point in this example is 1 and the end is 8 with the interval being 5.

Since slice is a method, the parentheses are needed. Slice knows to slice the array, list, or tuple above it, so the array, list or tuple does not need to be referred to by name.

A for loop is not necessary in this code, but the result is a list and thus the [] are needed. The complete code looks like this:

𝚙𝚛𝚘𝚍𝚞𝚌𝚝𝚜 = ('𝟷𝟷𝟷𝟷','𝟸𝟸𝟸𝟸','𝟹𝟹𝟹𝟹','𝟺𝟺𝟺𝟺','𝟻𝟻𝟻𝟻','𝟼𝟼𝟼𝟼','𝟽𝟽𝟽𝟽','𝟾𝟾𝟾𝟾','𝟿𝟿𝟿𝟿')

𝚝𝚎𝚜𝚝𝚎𝚛 = 𝚜𝚕𝚒𝚌𝚎(𝟷,𝟾,𝟻)

𝚙𝚛𝚒𝚗𝚝(𝚙𝚛𝚘𝚍𝚞𝚌𝚝𝚜[𝚝𝚎𝚜𝚝𝚎𝚛])

73
New cards

A set of softball leagues has two divisions merging into one division. The completed list should look like this:

['Angels', 'Dodgers', 'Padres', 'Sox', 'Wheelers']

Using the dropdown arrows, complete the code necessary to show the end result.

𝚕𝚎𝚊𝚐𝚞𝚎𝟷 = ['𝙰𝚗𝚐𝚎𝚕𝚜','𝙳𝚘𝚍𝚐𝚎𝚛𝚜','𝙿𝚊𝚍𝚛𝚎𝚜']

𝚕𝚎𝚊𝚐𝚞𝚎𝟸 = ['𝚆𝚑𝚎𝚎𝚕𝚎𝚛𝚜','𝙱𝚕𝚊𝚜𝚝𝚎𝚛𝚜']

𝚕𝚎𝚊𝚐𝚞𝚎𝟶 = 𝚕𝚎𝚊𝚐𝚞𝚎𝟷 (1) [ + / & / 𝚊𝚗𝚍 ] 𝚕𝚎𝚊𝚐𝚞𝚎𝟸

𝚕𝚎𝚊𝚐𝚞𝚎𝟶. (2) [ 𝚒𝚗𝚜𝚎𝚛𝚝 / 𝚊𝚍𝚍 / 𝚙𝚞𝚜𝚑. ] ( (3) [ 𝟸 / 𝟹 / 𝟺 ] ,'𝚂𝚘𝚡')

𝚕𝚎𝚊𝚐𝚞𝚎𝟶. (4) [ 𝚛𝚎𝚖𝚘𝚟𝚎 / 𝚍𝚎𝚕𝚎𝚝𝚎 / 𝚙𝚘𝚙 ] ('𝙱𝚕𝚊𝚜𝚝𝚎𝚛𝚜')

𝚙𝚛𝚒𝚗𝚝(𝚕𝚎𝚊𝚐𝚞𝚎𝟶)

Two lists can be merged into a single list through using the + sign.

The insert function inserts a value at a specific position in a list. As lists are zero-based, to add 'Sox' to be the fourth item in the list, the item needs to be inserted at position 3.

The remove function deletes an item from a list.

The completed code, then, is as follows:

𝚕𝚎𝚊𝚐𝚞𝚎𝟷 = ['𝙰𝚗𝚐𝚎𝚕𝚜','𝙳𝚘𝚍𝚐𝚎𝚛𝚜','𝙿𝚊𝚍𝚛𝚎𝚜']

𝚕𝚎𝚊𝚐𝚞𝚎𝟸 = ['𝚆𝚑𝚎𝚎𝚕𝚎𝚛𝚜','𝙱𝚕𝚊𝚜𝚝𝚎𝚛𝚜']

𝚕𝚎𝚊𝚐𝚞𝚎𝟶 = 𝚕𝚎𝚊𝚐𝚞𝚎𝟷 + 𝚕𝚎𝚊𝚐𝚞𝚎𝟸

𝚕𝚎𝚊𝚐𝚞𝚎𝟶.𝚒𝚗𝚜𝚎𝚛𝚝(𝟹,'𝚂𝚘𝚡')

𝚕𝚎𝚊𝚐𝚞𝚎𝟶.𝚛𝚎𝚖𝚘𝚟𝚎('𝙱𝚕𝚊𝚜𝚝𝚎𝚛𝚜')

𝚙𝚛𝚒𝚗𝚝(𝚕𝚎𝚊𝚐𝚞𝚎𝟶)

74
New cards

Analyze the following snippet of code:

𝚊 = 𝚏𝚕𝚘𝚊𝚝("𝚗𝚊𝚗")

𝚋 = 𝚏𝚕𝚘𝚊𝚝(𝟹𝟹)

𝚌 = 𝚒𝚜𝚗𝚊𝚗(𝚊)

𝚍 = 𝚒𝚜𝚗𝚊𝚗(𝚋)

For each statement regarding this code, select Yes if the statement is true and No if the statement is false.


  1. d will return 33.0

  2. The code will fail due to a runtime error.

  3. c will return True

  1. No

  2. No

  3. Yes


True statements:

  • c will return True.

False statements:

  • The code will fail due to a runtime error.

  • d will return 33.0.

Explanations:

  • nan means "not a number", so c will return True because float("nan") is not a number. isnan is a built-in function that returns True if a value being evaluated is not a number.

  • Because isnan returns True or False, it will not return a number, so d = False as b is a number. d is not going to return 33.0

  • nan is a legitimate expression, so float("nan") will not produce a runtime error.

75
New cards

A developer is writing code to iterate printing cities and state, with the end result looking like this:

Orange Florida
Orange Ohio
Orange Illinois
Springfield Florida
Springfield Ohio
Springfield Illinois
Capital
Auburn Florida
Auburn Ohio
Auburn Illinois

The first two lines of code are as follows:

𝚌𝚒𝚝𝚒𝚎𝚜 = ["𝙾𝚛𝚊𝚗𝚐𝚎", "𝚂𝚙𝚛𝚒𝚗𝚐𝚏𝚒𝚎𝚕𝚍", "𝙰𝚞𝚋𝚞𝚛𝚗"]

𝚜𝚝𝚊𝚝𝚎𝚜 = ["𝙵𝚕𝚘𝚛𝚒𝚍𝚊", "𝙾𝚑𝚒𝚘", "𝙸𝚕𝚕𝚒𝚗𝚘𝚒𝚜"]

Using the dropdown arrows, complete the code necessary to generate the above output.


𝚌𝚒𝚝𝚒𝚎𝚜 = ["𝙾𝚛𝚊𝚗𝚐𝚎", "𝚂𝚙𝚛𝚒𝚗𝚐𝚏𝚒𝚎𝚕𝚍", "𝙰𝚞𝚋𝚞𝚛𝚗"]

𝚜𝚝𝚊𝚝𝚎𝚜 = ["𝙵𝚕𝚘𝚛𝚒𝚍𝚊", "𝙾𝚑𝚒𝚘", "𝙸𝚕𝚕𝚒𝚗𝚘𝚒𝚜"]

(1) [ 𝚏𝚘𝚛 𝚡 𝚒𝚗 𝚌𝚒𝚝𝚒𝚎𝚜: / 𝚏𝚘𝚛 𝚢 𝚒𝚗 𝚜𝚝𝚊𝚝𝚎𝚜: / 𝚒𝚏 𝚡 == "𝚂𝚙𝚛𝚒𝚗𝚐𝚏𝚒𝚎𝚕𝚍" 𝚊𝚗𝚍 𝚢 == "𝙸𝚕𝚕𝚒𝚗𝚘𝚒𝚜": ]

⇥ (2) [ 𝚏𝚘𝚛 𝚢 𝚒𝚗 𝚜𝚝𝚊𝚝𝚎𝚜: / 𝚏𝚘𝚛 𝚡 𝚒𝚗 𝚌𝚒𝚝𝚒𝚎𝚜: / 𝚒𝚏 𝚡 == "𝚂𝚙𝚛𝚒𝚗𝚐𝚏𝚒𝚎𝚕𝚍" 𝚊𝚗𝚍 𝚢 == "𝙸𝚕𝚕𝚒𝚗𝚘𝚒𝚜": ]

⇥ ⇥ 𝚙𝚛𝚒𝚗𝚝(𝚡, 𝚢)

⇥ ⇥ (3) [ 𝚒𝚏 𝚡 == "𝚂𝚙𝚛𝚒𝚗𝚐𝚏𝚒𝚎𝚕𝚍" 𝚊𝚗𝚍 𝚢 == "𝙸𝚕𝚕𝚒𝚗𝚘𝚒𝚜": / 𝚏𝚘𝚛 𝚡 𝚒𝚗 𝚌𝚒𝚝𝚒𝚎𝚜: / 𝚏𝚘𝚛 𝚢 𝚒𝚗 𝚜𝚝𝚊𝚝𝚎𝚜:

⇥ ⇥ ⇥ 𝚙𝚛𝚒𝚗𝚝("𝙲𝚊𝚙𝚒𝚝𝚊𝚕")

This code example requires multiple for loops and a compound conditional statement. The cities should be looped through first as the print before the states. The finished code looks like this:

Thus, the finished code looks like this:

𝚌𝚒𝚝𝚒𝚎𝚜 = ["𝙾𝚛𝚊𝚗𝚐𝚎", "𝚂𝚙𝚛𝚒𝚗𝚐𝚏𝚒𝚎𝚕𝚍", "𝙰𝚞𝚋𝚞𝚛𝚗"]

𝚜𝚝𝚊𝚝𝚎𝚜 = ["𝙵𝚕𝚘𝚛𝚒𝚍𝚊", "𝙾𝚑𝚒𝚘", "𝙸𝚕𝚕𝚒𝚗𝚘𝚒𝚜"]

𝚏𝚘𝚛 𝚡 𝚒𝚗 𝚌𝚒𝚝𝚒𝚎𝚜:

⇥ 𝚏𝚘𝚛 𝚢 𝚒𝚗 𝚜𝚝𝚊𝚝𝚎𝚜:

⇥ ⇥ 𝚙𝚛𝚒𝚗𝚝(𝚡, 𝚢)

⇥ ⇥ 𝚒𝚏 𝚡 == "𝚂𝚙𝚛𝚒𝚗𝚐𝚏𝚒𝚎𝚕𝚍" 𝚊𝚗𝚍 𝚢 == "𝙸𝚕𝚕𝚒𝚗𝚘𝚒𝚜":

⇥ ⇥ ⇥ 𝚙𝚛𝚒𝚗𝚝("𝙲𝚊𝚙𝚒𝚝𝚊𝚕")

76
New cards

You are demonstrating how to use Python to perform advanced math calculations.

Using drag and drop, fill in the missing pieces of the code to accomplish these goals:

c = 3 raised to the second power

d = the square root of a

e = the whole number of the square root of b

Not every code piece will be used.

𝚒𝚖𝚙𝚘𝚛𝚝 [ (1) ]

𝚊 = 𝟹

𝚋 = 𝟸

𝚌 = 𝚖𝚊𝚝𝚑. [ (2) ] ( [ (3) ] )

𝚍 = 𝚖𝚊𝚝𝚑. [ (4) ]

𝚎 = 𝚖𝚊𝚝𝚑. [ (5) ]


𝚖𝚊𝚝𝚑

𝚙𝚘𝚠

𝚜𝚚(𝚊)

𝚒𝚜𝚚(𝚋)

𝟷,𝟹

𝚊, 𝚋

𝚜𝚚(𝚋)

𝚒𝚜𝚚(𝚊)

**

𝚒𝚜𝚚𝚛𝚝(𝚋)

𝚜𝚚𝚛𝚝(𝚊)

𝚒𝚜𝚚𝚛𝚝(𝚊)

Pow is a built-in math function that raises the first number indicated to the power of the second number indicated.

Sqrt returns the square root of a number.

Isqrt returns the integer of a square root of a number.

To use these functions, the math module must first be imported.

Thus, the correct code is as follows:

𝚒𝚖𝚙𝚘𝚛𝚝 𝚖𝚊𝚝𝚑

𝚊 = 𝟹

𝚋 = 𝟸

𝚌 = 𝚖𝚊𝚝𝚑.𝚙𝚘𝚠(𝚊, 𝚋)

𝚍 = 𝚖𝚊𝚝𝚑.𝚜𝚚𝚛𝚝(𝚊)

𝚎 = 𝚖𝚊𝚝𝚑.𝚒𝚜𝚚𝚛𝚝(𝚋)

77
New cards

A junior programmer is building a function to compute the area of a rectangle. Which function definition statement is needed to replace the comment to make this code work?

#𝚍𝚎𝚌𝚕𝚊𝚛𝚎 𝚏𝚞𝚗𝚌𝚝𝚒𝚘𝚗 𝚑𝚎𝚛𝚎

⇥ 𝚣 = 𝚡 * 𝚢

⇥ 𝚙𝚛𝚒𝚗𝚝 (𝚣)

𝚊𝚛𝚎𝚊 (𝟻,𝟷𝟶)


○ 𝚍𝚎𝚏 𝚊𝚛𝚎𝚊 (𝚡,𝚢):

○ 𝚍𝚎𝚏 𝚊𝚛𝚎𝚊 (𝚡,𝚢,𝚣):

○ 𝚍𝚎𝚏 𝚌𝚊𝚕𝚌𝚞𝚕𝚊𝚝𝚎_𝚊𝚛𝚎𝚊 (𝚡,𝚢):

○ 𝚍𝚎𝚏 𝚌𝚊𝚕𝚌𝚞𝚕𝚊𝚝𝚎_𝚊𝚛𝚎𝚊 (𝚡,𝚢,𝚣):

The def keyword is used to build a function. The function needs to be named area to match the call to the function at the end of the code. And, x and y are being multiplied, so those two arguments need to be in the function declaration as well.

Any other name for the function will not work nor will having z is an argument as z is what is being printed, not calculated.

Thus, the finished code is as follows:

𝚍𝚎𝚏 𝚊𝚛𝚎𝚊 (𝚡,𝚢):

⇥ 𝚣 = 𝚡 * 𝚢

⇥ 𝚙𝚛𝚒𝚗𝚝 (𝚣)

𝚊𝚛𝚎𝚊 (𝟻,𝟷𝟶)

78
New cards

Evaluate the following code, used to set up a unit test between two variables.

𝚒𝚖𝚙𝚘𝚛𝚝 𝚞𝚗𝚒𝚝𝚝𝚎𝚜𝚝

𝚌𝚕𝚊𝚜𝚜 𝚃𝚎𝚜𝚝𝙼𝚊𝚒𝚗(𝚞𝚗𝚒𝚝𝚝𝚎𝚜𝚝.𝚃𝚎𝚜𝚝𝙲𝚊𝚜𝚎):

⇥ 𝚍𝚎𝚏 _______________(𝚜𝚎𝚕𝚏):

⇥ ⇥ 𝚊 = '𝙽𝚘𝚛𝚝𝚑'

⇥ ⇥ 𝚋 = '𝙽𝚘𝚛𝚝𝚑'

⇥ ⇥ 𝚜𝚎𝚕𝚏.𝚊𝚜𝚜𝚎𝚛𝚝𝙴𝚚𝚞𝚊𝚕(𝚊,𝚋)

𝚒𝚏 __𝚗𝚊𝚖𝚎__ == '__𝚖𝚊𝚒𝚗__':

⇥ 𝚞𝚗𝚒𝚝𝚝𝚎𝚜𝚝.𝚖𝚊𝚒𝚗()

Of the function names given, which function name is a valid name for the function building the unit test?


○ 𝚝𝚎𝚜𝚝_𝚝𝚎𝚛𝚛𝚒𝚝𝚘𝚛𝚢

𝚝𝚎𝚜𝚝𝚝𝚎𝚛𝚛𝚒𝚝𝚘𝚛𝚢

○ 𝚝𝚎𝚜𝚝𝚌𝚊𝚜𝚎_𝚝𝚎𝚛𝚛𝚒𝚝𝚘𝚛𝚢

○ 𝚝𝚎𝚛𝚛𝚒𝚝𝚘𝚛𝚢_𝚝𝚎𝚜𝚝

𝚝𝚎𝚜𝚝_𝚝𝚎𝚛𝚛𝚒𝚝𝚘𝚛𝚢


Functions used to run unit tests need to start with the word, test. Thus, test_territory is a valid name.

These names are not valid:

_test_territory

territory_test

testcase_territory

79
New cards

Examine the following code, used to calculate total cost for renting storage space for a day:

𝚕𝚎𝚗𝚐𝚝𝚑 = 𝟷𝟸.𝟻

𝚙𝚛𝚒𝚌𝚎 = 𝟸.𝟿𝟿

𝚌𝚘𝚜𝚝 = 𝚕𝚎𝚗𝚐𝚝𝚑 * 𝚙𝚛𝚒𝚌𝚎

𝚙𝚛𝚒𝚗𝚝("𝚈𝚘𝚞𝚛 𝚝𝚘𝚝𝚊𝚕 𝚌𝚘𝚜𝚝 𝚒𝚜", 𝚌𝚘𝚜𝚝)

The total cost is not printing as a dollar amount, as the number of decimal places vary. Which print statement would print the total cost as two decimals, using the Modulo operator?


○ 𝚙𝚛𝚒𝚗𝚝("𝚈𝚘𝚞𝚛 𝚝𝚘𝚝𝚊𝚕 𝚌𝚘𝚜𝚝 𝚒𝚜 ", 𝚛𝚘𝚞𝚗𝚍(𝚌𝚘𝚜𝚝)

○ 𝚙𝚛𝚒𝚗𝚝 ("𝚈𝚘𝚞𝚛 𝚝𝚘𝚝𝚊𝚕 𝚌𝚘𝚜𝚝 𝚒𝚜 {:𝟶.𝟸𝚏}" % (𝚌𝚘𝚜𝚝))

○ 𝚙𝚛𝚒𝚗𝚝 ("𝚈𝚘𝚞𝚛 𝚝𝚘𝚝𝚊𝚕 𝚌𝚘𝚜𝚝 𝚒𝚜 %.𝟸𝚏" % (𝚌𝚘𝚜𝚝))

○ 𝚙𝚛𝚒𝚗𝚝("𝚈𝚘𝚞𝚛 𝚝𝚘𝚝𝚊𝚕 𝚌𝚘𝚜𝚝 𝚒𝚜 ",𝚖𝚊𝚝𝚑.𝚏𝚕𝚘𝚘𝚛(𝚌𝚘𝚜𝚝)

𝚙𝚛𝚒𝚗𝚝 ("𝚈𝚘𝚞𝚛 𝚝𝚘𝚝𝚊𝚕 𝚌𝚘𝚜𝚝 𝚒𝚜 %.𝟸𝚏" % (𝚌𝚘𝚜𝚝))


%.2f is used as part of the Modulo operator (%) to format a number to two floating decimals.

{:0.2f} would be used if the string.format method of formatting was being used.

math.floor rounds a number down to the nearest integer.

round rounds a number to the nearest integer.

Thus, the correct code is as follows:

𝚕𝚎𝚗𝚐𝚝𝚑 = 𝟷𝟸.𝟻

𝚙𝚛𝚒𝚌𝚎 = 𝟸.𝟿𝟿

𝚌𝚘𝚜𝚝 = 𝚕𝚎𝚗𝚐𝚝𝚑 * 𝚙𝚛𝚒𝚌𝚎

𝚙𝚛𝚒𝚗𝚝 ("𝚈𝚘𝚞𝚛 𝚝𝚘𝚝𝚊𝚕 𝚌𝚘𝚜𝚝 𝚒𝚜 %.𝟸𝚏" % (𝚌𝚘𝚜𝚝))

80
New cards

Using drag and drop, arrange each group of operators in the correct order of precedence, with the highest precedence on top.

Comparison

Logical

Arithmetic

Identity

Containment

Assignments

  1. Arithmetic

  2. Containment

  3. Comparison

  4. Identity

  5. Logical

  6. Assignments

81
New cards

An app needs to read information from a configuration file, named config.txt and possibly add to the configuration file if the file is present in a home directory. If the file does not exist, it needs to be open for write purposes.

Using the drop-down arrows, fill in the code necessary to complete this task.

𝚒𝚖𝚙𝚘𝚛𝚝 (1) [ 𝚘𝚜 / 𝚒𝚘 / 𝚜𝚢𝚜 ]

𝚒𝚏 (2) [ 𝚘𝚜.𝚙𝚊𝚝𝚑.𝚒𝚜𝚏𝚒𝚕𝚎 / 𝚘𝚜.𝚙𝚊𝚝𝚑 / 𝚘𝚜.𝚒𝚜𝚏𝚒𝚕𝚎 ] ('𝚌𝚘𝚗𝚏𝚒𝚐.𝚝𝚡𝚝'):

⇥ 𝚠𝚛𝚒𝚝𝚎𝙵𝚒𝚕𝚎 = 𝚘𝚙𝚎𝚗('𝚌𝚘𝚗𝚏𝚒𝚐.𝚝𝚡𝚝', ' (3) [ 𝚛+ / 𝚛 / 𝚛𝚠 ] ')

𝚎𝚕𝚜𝚎:

⇥ 𝚠𝚛𝚒𝚝𝚎𝙵𝚒𝚕𝚎 = 𝚘𝚙𝚎𝚗('𝚌𝚘𝚗𝚏𝚒𝚐.𝚝𝚡𝚝', ' (4) [ 𝚠 / 𝚠+ / 𝚊 ] ')

The os library is necessary to see if a file exists, as the os.path.isfile submodule is used to see if a file exists in the current path. os.path is insufficient because it checks for the existence of a file or folder.

If the file does exist, the file needs to be opened in r+ mode, which gives one the ability to read the file and possibly write to it. If the file does not exist, it needs to be opened in w mode, so that the file can be written to.

The completed code is as follows:

𝚒𝚖𝚙𝚘𝚛𝚝 𝚘𝚜

𝚒𝚏 𝚘𝚜.𝚙𝚊𝚝𝚑.𝚒𝚜𝚏𝚒𝚕𝚎('𝚌𝚘𝚗𝚏𝚒𝚐.𝚝𝚡𝚝'):

⇥ 𝚠𝚛𝚒𝚝𝚎𝙵𝚒𝚕𝚎 = 𝚘𝚙𝚎𝚗('𝚌𝚘𝚗𝚏𝚒𝚐.𝚝𝚡𝚝', '𝚛+')

𝚎𝚕𝚜𝚎:

⇥ 𝚠𝚛𝚒𝚝𝚎𝙵𝚒𝚕𝚎 = 𝚘𝚙𝚎𝚗('𝚌𝚘𝚗𝚏𝚒𝚐.𝚝𝚡𝚝', '𝚠')

82
New cards

A new Python developer is trying to understand the differences among data structures. Using drag and drop, match each code example with a characteristic of the type of list being generated from the code.

1) Values are editable

2) Values can be added or removed, but not edited

3) An immutable list

4) Represents a dictionary

a) 𝚘𝚏𝚏𝚒𝚌𝚎𝚜 = {'𝚕𝚘𝚌𝚊𝚝𝚒𝚘𝚗': '𝙴𝚊𝚜𝚝', '𝚜𝚒𝚣𝚎': '𝟺𝟻𝟶𝟶'}

b) 𝚋𝚊𝚌𝚔𝚐𝚛𝚘𝚞𝚗𝚍_𝚌𝚘𝚕𝚘𝚛𝚜= ['𝚢𝚎𝚕𝚕𝚘𝚠','𝚜𝚕𝚊𝚝𝚎', '𝚕𝚒𝚐𝚑𝚝𝚋𝚕𝚞𝚎']

c) 𝚍𝚎𝚙𝚊𝚛𝚝𝚖𝚎𝚗𝚝𝚜 = ('𝚂𝚊𝚕𝚎𝚜','𝙼𝚊𝚛𝚔𝚎𝚝𝚒𝚗𝚐','𝙾𝚙𝚎𝚛𝚊𝚝𝚒𝚘𝚗𝚜','𝙸𝚃')

d) 𝚕𝚘𝚌𝚊𝚝𝚒𝚘𝚗𝚜 = {'𝙷𝚀', '𝚆𝚎𝚜𝚝', '𝚁𝚎𝚖𝚘𝚝𝚎𝟷'}

1) + b) = Values are editable - 𝚋𝚊𝚌𝚔𝚐𝚛𝚘𝚞𝚗𝚍_𝚌𝚘𝚕𝚘𝚛𝚜= ['𝚢𝚎𝚕𝚕𝚘𝚠','𝚜𝚕𝚊𝚝𝚎', '𝚕𝚒𝚐𝚑𝚝𝚋𝚕𝚞𝚎']

2) + d) = Values can be added or removed, but not edited - 𝚕𝚘𝚌𝚊𝚝𝚒𝚘𝚗𝚜 = {'𝙷𝚀', '𝚆𝚎𝚜𝚝', '𝚁𝚎𝚖𝚘𝚝𝚎𝟷'}

3) + c) = An immutable list - 𝚍𝚎𝚙𝚊𝚛𝚝𝚖𝚎𝚗𝚝𝚜 = ('𝚂𝚊𝚕𝚎𝚜','𝙼𝚊𝚛𝚔𝚎𝚝𝚒𝚗𝚐','𝙾𝚙𝚎𝚛𝚊𝚝𝚒𝚘𝚗𝚜','𝙸𝚃')

4) + a) = Represents a dictionary - 𝚘𝚏𝚏𝚒𝚌𝚎𝚜 = {'𝚕𝚘𝚌𝚊𝚝𝚒𝚘𝚗': '𝙴𝚊𝚜𝚝', '𝚜𝚒𝚣𝚎': '𝟺𝟻𝟶𝟶'}


background_colors = ['yellow','slate','lightblue'] represents a list, which stores multiple values in a single variable and the values can be editable.

locations = {'HQ','West','Remote1'} represents a set, which has values that can be added or removed, but not edited.

departments = ('Sales','Marketing','Operations','IT') represents a tuple, which is an immutable list, meaning it must be redefined for any values to be changed.

offices = {'location': 'East', 'size': '4500'} represents a dictionary, which stores values in key-value pairs.

83
New cards

You have daily attendance figures for a conference and want to know the highest attendance for the conference and the lowest attendance for the conference.

Using the dropdown arrows, fill in the missing code pieces necessary to obtain those amounts.

𝚊𝚝𝚝𝚎𝚗𝚍𝚊𝚗𝚌𝚎 = [𝟹𝟶𝟶,𝟸𝟻𝟶,𝟸𝟶𝟶,𝟺𝟶𝟶,𝟷𝟻𝟶,𝟸𝟸𝟻,𝟹𝟸𝟻]

𝚙𝚛𝚒𝚗𝚝( (1) [ 𝚖𝚊𝚡 / 𝚖𝚊𝚡𝚒𝚖𝚞𝚖 / 𝚖𝚘𝚜𝚝 ] (𝚊𝚝𝚝𝚎𝚗𝚍𝚊𝚗𝚌𝚎))

𝚙𝚛𝚒𝚗𝚝( (2) [ 𝚖𝚒𝚗 / 𝚖𝚒𝚗𝚒𝚖𝚞𝚖 / 𝚕𝚎𝚊𝚜𝚝 ] (𝚊𝚝𝚝𝚎𝚗𝚍𝚊𝚗𝚌𝚎))

The max function returns the highest value within a list of values.

The min function returns the lowest value within a list of values.

Thus, the completed code is as follows:

𝚊𝚝𝚝𝚎𝚗𝚍𝚊𝚗𝚌𝚎 = [𝟹𝟶𝟶,𝟸𝟻𝟶,𝟸𝟶𝟶,𝟺𝟶𝟶,𝟷𝟻𝟶,𝟸𝟸𝟻,𝟹𝟸𝟻]

𝚙𝚛𝚒𝚗𝚝(𝚖𝚊𝚡(𝚊𝚝𝚝𝚎𝚗𝚍𝚊𝚗𝚌𝚎))

𝚙𝚛𝚒𝚗𝚝(𝚖𝚒𝚗(𝚊𝚝𝚝𝚎𝚗𝚍𝚊𝚗𝚌𝚎))

84
New cards

Evaluate the following code:

𝚊 = 𝟷𝟶𝟶

𝚋 = 𝟷𝟻

𝚌 = 𝟷𝟶

𝚊 += 𝚋

𝚋 -= 𝚌

𝚊 %=𝚋

For each statement that is true regarding the results, indicate a Yes. If not, indicate a No.


  1. a = 115

  2. b = 5

  3. c = 10

  1. No

  2. Yes

  3. Yes


True statements:

b = 5

c = 10

False statements:

a = 115

Explanation:

The first calculation adds b to a, meaning 15 + 100 = 115.

b, which starts at 15, then subtracts from itself c, leaving 5.

a then is calculated as a%b, which calculates the remainder of a / b. 115 divided by 5 is 23 with a remainder of 0, leaving the following values: a = 0, b = 5, and c = 10 as c was never changed.