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生成,可能並非總是準確或最新。請將其用作輔助資源,並與官方材料進行核實。
想多做幾條同類題目?立即開始練習呢個課題,即做即批改。
立即練習