In Python, which of the following is the correct code to open a file named "results.txt" for the purpose of adding new data to the end of the existing content without overwriting it?
GCE O-Level · Computing (7155)
Python Code:练习题
5 道选择题即时批改,另有 2 道文字题附完整解题步骤,全部围绕「Python Code」。
A programmer wants to generate a random integer between 1 and 10 (inclusive). Which Python command should be used after importing the random module?
What is the output of the following Python code?data = [2, 4, 6]
result = 0
for i in range(len(data)):
result += data[i] * i
print(result)
Which of the following is the correct Python expression to calculate the remainder when 25 is divided by 4?
What is the output of the following Python code segment?x = 17
y = 5
print(x // y, x % y)
A student wrote a Python script to calculate the average score of a class. The script asks for the number of students, then prompts for each student's score one by one.
(a) Write a Python code snippet using a for loop to input 5 student scores and store them in a list named scores.
(b) Explain the difference between using a for loop and a while loop in this context.
先自己写一遍答案,再对照解题步骤。
Consider a scenario where you need to validate user passwords. A valid password must be at least 8 characters long and must not contain any spaces.
(a) Define a function validate_password(pw) that takes a string as a parameter. It should return True if the password meets both criteria and False otherwise.
(b) Inside the function, use len() and the not in operator or a string method to perform the checks.
(c) Demonstrate how to call this function with an input string provided by the user.
先自己写一遍答案,再对照解题步骤。
* thinka提供的内容由AI生成,可能并非总是准确或最新。请将其用作辅助资源,并与官方材料进行核实。
想多做几道同类题目?立即开始练习这个课题,边做边批改。
立即练习