SIMULATION
Complete the function calculate_tip(bill, tip_percent) that calculates and returns the tip amount based on the bill and tip percentage.
For example, calculate_tip(50, 20) should return 10.0.
def calculate_tip(bill, tip_percent):
# TODO: Calculate and return the tip amount
pass
Answer : A
==========
Step 1: The function receives two parameters: bill and tip_percent.
Step 2: Convert the percentage into a decimal by dividing tip_percent by 100.
Step 3: Multiply the bill amount by that decimal value.
Correct code:
def calculate_tip(bill, tip_percent):
return bill * tip_percent / 100
Example:
print(calculate_tip(50, 20))
Output:
10.0
Which punctuation mark must appear at the end of an if statement line?
Answer : B
In Python, an if statement line must end with a colon.
Example:
age = 18
if age >= 18:
print('Adult')
The colon : tells Python that an indented block of code follows. That indented block contains the statements that should run when the condition is true.
A semicolon, period, or comma is not used to begin the body of an if statement in Python.
Therefore, the correct answer is B. : colon.
Which index position is returned when the string method .find('python') is applied to the string 'learning python programming'?
Answer : B
The .find() method returns the lowest index where the searched substring is found.
Example:
'learning python programming'.find('python')
Output:
9
Indexing starts at 0 in Python. In the string 'learning python programming', the word 'python' begins at index 9. Python's built-in types documentation describes str.find() as returning the lowest index where the substring is found.
Therefore, the correct answer isB. 9.
SIMULATION
Fix the indentation error in this function that should return a greeting message.
def greet(name):
return "Hello " + name
Answer : A
==========
Step 1: In Python, the code inside a function must be indented.
Step 2: The return statement belongs inside the function body.
Step 3: Add indentation before the return statement.
Correct code:
def greet(name):
return 'Hello ' + name
Example:
print(greet('Alice'))
Output:
Hello Alice
Which type of loop repeatedly checks a condition to determine whether to continue?
Answer : B
Awhile looprepeatedly executes a block of code as long as its condition remains true.
Example:
count = 1
while count <= 3:
print(count)
count += 1
In this example, Python checks the condition count <= 3 before each loop iteration. If the condition is true, the loop continues. When the condition becomes false, the loop stops.
A for loop is usually used to iterate over a sequence, such as a list, string, or range. Python does not have a built-in repeat loop construct.
Therefore, the correct answer isB. while loop.
SIMULATION
Complete the function double_number(num) that takes one number parameter and returns double that number.
For example, double_number(5) should return 10.
def double_number(num):
# TODO: Return double the input number
# Example: double_number(5) should return 10
pass
Answer : A
==========
Step 1: The function receives one parameter named num.
Step 2: To double a number, multiply it by 2.
Step 3: The function should return the result using the return statement.
Correct code:
def double_number(num):
return num * 2
Example:
print(double_number(5))
Output:
10
Which data type is the value 3.14 in Python?
Answer : B
The value 3.14 is afloatbecause it contains a decimal point.
Example:
type(3.14)
This returns:
<class 'float'>
Python's built-in numeric types include integers and floating-point numbers. A value with a decimal part, such as 3.14, is treated as a floating-point number.
Therefore, the correct answer isB. Float.