Bạn nên xác định vùng chứa cho câu hỏi và câu trả lời của mình. Bằng cách này, bạn có thể tránh phải lặp lại toàn bộ logic cho mọi câu hỏi
Bước đầu tiên, tôi khuyên bạn nên đặt tất cả các văn bản câu hỏi, văn bản trả lời, văn bản lựa chọn và văn bản câu trả lời đúng trong danh sách
Sau đó, bạn có thể viết một hàm lặp qua các danh sách này [cùng một lúc] và thực hiện trò chơi thực tế. Đối với điều này, bạn có thể sử dụng
class Question:
def __init__[self, question, answer, choices, correct]:
self.question = question
self.answer = answer
self.choices = choices
self.correct = correct
15questions = ["What is 1 + 1",
"Who is the 45th president of the United States?",
"True or False.. The Toronto Maple Leafs have won 13 Stanley Cups?",
"What was the last year the Toronto Maple Leafs won the Stanley Cup?",
"True or False.. The current Prime Minister of Canada is Pierre Elliot Tredeau?"]
answer_choices = ["a]1\nb]2\nc]3\nd]4\n:",
"a]Barack Obama\nb]Hillary Clinton\nc]Donald Trump\nd]Tom Brady\n:",
":",
"a]1967\nb]1955\nc]1987\nd]1994\n:",
":"]
correct_choices = [{"b", "2"},
{"c", "donald trump"},
{"true", "t"},
{"a", "1967"},
{"false", "f"}]
answers = ["1 + 1 is 2",
"The 45th president is Donald Trump",
"",
"The last time the Toronto Maple Leafs won the Stanley Cup was 1967",
"The current Prime Minster of Canada is Justin Tredeau"]
def quiz[]:
score = 0
for question, choices, correct_choice, answer in zip[questions, answer_choices, correct_choices, answers]:
print[question]
user_answer = input[choices].lower[]
if user_answer in correct_choice:
print["Correct"]
score += 1
else:
print["Incorrect", answer]
print[score, "out of", len[questions], "that is", float[score / len[questions]] * 100, "%"]
if __name__ == "__main__":
quiz[]
2. OOP [Lập trình hướng đối tượng]
Bước thứ hai có thể là cố gắng gói gọn toàn bộ đối tượng câu hỏi này vào một đối tượng Python thực tế. Đó là, một câu hỏi là một đối tượng có một câu hỏi [văn bản], một câu trả lời, lựa chọn câu trả lời và lựa chọn câu trả lời đúng
Bạn có thể định nghĩa một
class Question:
def __init__[self, question, answer, choices, correct]:
self.question = question
self.answer = answer
self.choices = choices
self.correct = correct
16 như thế nàyclass Question:
def __init__[self, question, answer, choices, correct]:
self.question = question
self.answer = answer
self.choices = choices
self.correct = correct
Nhưng lớp này sẽ chẳng là gì ngoài việc lưu trữ dữ liệu cho các chuỗi này. Bất cứ khi nào bạn có cái này, bạn có thể sử dụng một
class Question:
def __init__[self, question, answer, choices, correct]:
self.question = question
self.answer = answer
self.choices = choices
self.correct = correct
17>>> from collections import namedtuple
>>> Question = namedtuple["Question", "question answer choices correct"]
>>> q = Question["What is 1 + 1", "1 + 1 is 2", "a]1\nb]2\nc]3\nd]4\n:", {"b", "2"}]
>>> q.question
'What is 1 + 1'
>>> q[3]
{'b', '2'}
Vì vậy, một
class Question:
def __init__[self, question, answer, choices, correct]:
self.question = question
self.answer = answer
self.choices = choices
self.correct = correct
18 chỉ là một bộ có tên đẹp, để sau đó là class Question:
def __init__[self, question, answer, choices, correct]:
self.question = question
self.answer = answer
self.choices = choices
self.correct = correct
19, v.v.from collections import namedtuple
Question = namedtuple["Question", "question answer choices correct"]
questions = [Question["What is 1 + 1", "1 + 1 is 2", "a]1\nb]2\nc]3\nd]4\n:", {"b", "2"}],
...]
def quiz[]:
score = 0
for question in questions:
print[question.question]
user_answer = input[question.choices].lower[]
if user_answer in question.correct:
print["Correct"]
score += 1
else:
print["Incorrect", question.answer]
print[score, "out of", len[questions], "that is",
float[score / len[questions]] * 100, "%"]
if __name__ == "__main__":
quiz[]
3. Cú đẩy cuối cùng
Cuối cùng, nhưng không kém phần quan trọng, bạn có thể làm cho việc liệt kê các lựa chọn trở nên tự động, sử dụng
class Question:
def __init__[self, question, answer, choices, correct]:
self.question = question
self.answer = answer
self.choices = choices
self.correct = correct
0 [đơn giản là bảng chữ cái viết thường] và đánh dấu nó bằng các dòng mới và class Question:
def __init__[self, question, answer, choices, correct]:
self.question = question
self.answer = answer
self.choices = choices
self.correct = correct
2 được sử dụng để tách một chữ cái khỏi lựa chọn liên quan đến chữ cái đóBạn cũng có thể chuyển trực tiếp danh sách các
class Question:
def __init__[self, question, answer, choices, correct]:
self.question = question
self.answer = answer
self.choices = choices
self.correct = correct
18 cho hàm, cho phép thực hiện các câu đố khác nhau [ví dụ: bằng cách chọn ngẫu nhiên 5 câu hỏi trong số 100 câu hỏi] Trong bài học trước, chúng tôi đã phát hiện ra điều gì đó đáng ngờ đang diễn ra trong dữ liệu về tình trạng viêm của mình bằng cách vẽ một số biểu đồ. Làm cách nào chúng ta có thể sử dụng Python để tự động nhận ra các tính năng khác nhau mà chúng ta đã thấy và thực hiện một hành động khác nhau cho từng tính năng?
điều kiện
Chúng ta có thể yêu cầu Python thực hiện các hành động khác nhau, tùy thuộc vào điều kiện, với câu lệnh
before conditional...
...after conditional
6________số 8
not greater
done
Dòng thứ hai của mã này sử dụng từ khóa
before conditional...
...after conditional
6 để nói với Python rằng chúng tôi muốn đưa ra lựa chọn. Nếu phép thử theo sau câu lệnh before conditional...
...after conditional
6 là đúng, thì phần thân của câu lệnh before conditional...
...after conditional
6 [i. e. , tập hợp các dòng được thụt vào bên dưới nó] được thực thi và "lớn hơn" được in. Nếu kiểm tra là sai, thay vào đó, phần thân của before conditional...
...after conditional
8 sẽ được thực thi và dòng chữ “không lớn hơn” được in ra. Chỉ cái này hoặc cái kia được thực thi trước khi tiếp tục thực hiện chương trình để in “xong”Câu điều kiện không nhất thiết phải bao gồm một
before conditional...
...after conditional
8. Nếu không có, Python đơn giản là không làm gì nếu kiểm tra saiclass Question:
def __init__[self, question, answer, choices, correct]:
self.question = question
self.answer = answer
self.choices = choices
self.correct = correct
5before conditional...
...after conditional
Chúng tôi cũng có thể xâu chuỗi một số bài kiểm tra lại với nhau bằng cách sử dụng
before conditional...
...after conditional
7, viết tắt của “else if”. Mã Python sau đây sử dụng before conditional...
...after conditional
7 để in dấu của một sốclass Question:
def __init__[self, question, answer, choices, correct]:
self.question = question
self.answer = answer
self.choices = choices
self.correct = correct
9>>> from collections import namedtuple
>>> Question = namedtuple["Question", "question answer choices correct"]
>>> q = Question["What is 1 + 1", "1 + 1 is 2", "a]1\nb]2\nc]3\nd]4\n:", {"b", "2"}]
>>> q.question
'What is 1 + 1'
>>> q[3]
{'b', '2'}
0Lưu ý rằng để kiểm tra sự bằng nhau, chúng tôi sử dụng dấu bằng kép _______ 199 thay vì dấu bằng đơn ________ 200 được sử dụng để gán giá trị
So sánh trong Python
Cùng với các toán tử
01 và>>> from collections import namedtuple >>> Question = namedtuple["Question", "question answer choices correct"] >>> q = Question["What is 1 + 1", "1 + 1 is 2", "a]1\nb]2\nc]3\nd]4\n:", {"b", "2"}] >>> q.question 'What is 1 + 1' >>> q[3] {'b', '2'}
99 mà chúng ta đã sử dụng để so sánh các giá trị trong điều kiện của mình, có một số tùy chọn khác để biết vềclass Question: def __init__[self, question, answer, choices, correct]: self.question = question self.answer = answer self.choices = choices self.correct = correct
01. lớn hơn>>> from collections import namedtuple >>> Question = namedtuple["Question", "question answer choices correct"] >>> q = Question["What is 1 + 1", "1 + 1 is 2", "a]1\nb]2\nc]3\nd]4\n:", {"b", "2"}] >>> q.question 'What is 1 + 1' >>> q[3] {'b', '2'}
04. ít hơn>>> from collections import namedtuple >>> Question = namedtuple["Question", "question answer choices correct"] >>> q = Question["What is 1 + 1", "1 + 1 is 2", "a]1\nb]2\nc]3\nd]4\n:", {"b", "2"}] >>> q.question 'What is 1 + 1' >>> q[3] {'b', '2'}
99. tương đương vớiclass Question: def __init__[self, question, answer, choices, correct]: self.question = question self.answer = answer self.choices = choices self.correct = correct
06. không bằng>>> from collections import namedtuple >>> Question = namedtuple["Question", "question answer choices correct"] >>> q = Question["What is 1 + 1", "1 + 1 is 2", "a]1\nb]2\nc]3\nd]4\n:", {"b", "2"}] >>> q.question 'What is 1 + 1' >>> q[3] {'b', '2'}
07. lớn hơn hoặc bằng>>> from collections import namedtuple >>> Question = namedtuple["Question", "question answer choices correct"] >>> q = Question["What is 1 + 1", "1 + 1 is 2", "a]1\nb]2\nc]3\nd]4\n:", {"b", "2"}] >>> q.question 'What is 1 + 1' >>> q[3] {'b', '2'}
08. ít hơn hoặc bằng>>> from collections import namedtuple >>> Question = namedtuple["Question", "question answer choices correct"] >>> q = Question["What is 1 + 1", "1 + 1 is 2", "a]1\nb]2\nc]3\nd]4\n:", {"b", "2"}] >>> q.question 'What is 1 + 1' >>> q[3] {'b', '2'}
Chúng tôi cũng có thể kết hợp các bài kiểm tra bằng cách sử dụng
before conditional...
...after conditional
9 và class Question:
def __init__[self, question, answer, choices, correct]:
self.question = question
self.answer = answer
self.choices = choices
self.correct = correct
90. before conditional...
...after conditional
9 chỉ đúng nếu cả hai phần đều đúngfrom collections import namedtuple
Question = namedtuple["Question", "question answer choices correct"]
questions = [Question["What is 1 + 1", "1 + 1 is 2", "a]1\nb]2\nc]3\nd]4\n:", {"b", "2"}],
...]
def quiz[]:
score = 0
for question in questions:
print[question.question]
user_answer = input[question.choices].lower[]
if user_answer in question.correct:
print["Correct"]
score += 1
else:
print["Incorrect", question.answer]
print[score, "out of", len[questions], "that is",
float[score / len[questions]] * 100, "%"]
if __name__ == "__main__":
quiz[]
4from collections import namedtuple
Question = namedtuple["Question", "question answer choices correct"]
questions = [Question["What is 1 + 1", "1 + 1 is 2", "a]1\nb]2\nc]3\nd]4\n:", {"b", "2"}],
...]
def quiz[]:
score = 0
for question in questions:
print[question.question]
user_answer = input[question.choices].lower[]
if user_answer in question.correct:
print["Correct"]
score += 1
else:
print["Incorrect", question.answer]
print[score, "out of", len[questions], "that is",
float[score / len[questions]] * 100, "%"]
if __name__ == "__main__":
quiz[]
5trong khi
class Question:
def __init__[self, question, answer, choices, correct]:
self.question = question
self.answer = answer
self.choices = choices
self.correct = correct
90 là đúng nếu ít nhất một phần là đúngfrom collections import namedtuple
Question = namedtuple["Question", "question answer choices correct"]
questions = [Question["What is 1 + 1", "1 + 1 is 2", "a]1\nb]2\nc]3\nd]4\n:", {"b", "2"}],
...]
def quiz[]:
score = 0
for question in questions:
print[question.question]
user_answer = input[question.choices].lower[]
if user_answer in question.correct:
print["Correct"]
score += 1
else:
print["Incorrect", question.answer]
print[score, "out of", len[questions], "that is",
float[score / len[questions]] * 100, "%"]
if __name__ == "__main__":
quiz[]
7from collections import namedtuple
Question = namedtuple["Question", "question answer choices correct"]
questions = [Question["What is 1 + 1", "1 + 1 is 2", "a]1\nb]2\nc]3\nd]4\n:", {"b", "2"}],
...]
def quiz[]:
score = 0
for question in questions:
print[question.question]
user_answer = input[question.choices].lower[]
if user_answer in question.correct:
print["Correct"]
score += 1
else:
print["Incorrect", question.answer]
print[score, "out of", len[questions], "that is",
float[score / len[questions]] * 100, "%"]
if __name__ == "__main__":
quiz[]
8
43 vàfrom collections import namedtuple Question = namedtuple["Question", "question answer choices correct"] questions = [Question["What is 1 + 1", "1 + 1 is 2", "a]1\nb]2\nc]3\nd]4\n:", {"b", "2"}], ...] def quiz[]: score = 0 for question in questions: print[question.question] user_answer = input[question.choices].lower[] if user_answer in question.correct: print["Correct"] score += 1 else: print["Incorrect", question.answer] print[score, "out of", len[questions], "that is", float[score / len[questions]] * 100, "%"] if __name__ == "__main__": quiz[]
44from collections import namedtuple Question = namedtuple["Question", "question answer choices correct"] questions = [Question["What is 1 + 1", "1 + 1 is 2", "a]1\nb]2\nc]3\nd]4\n:", {"b", "2"}], ...] def quiz[]: score = 0 for question in questions: print[question.question] user_answer = input[question.choices].lower[] if user_answer in question.correct: print["Correct"] score += 1 else: print["Incorrect", question.answer] print[score, "out of", len[questions], "that is", float[score / len[questions]] * 100, "%"] if __name__ == "__main__": quiz[]
43 vàfrom collections import namedtuple Question = namedtuple["Question", "question answer choices correct"] questions = [Question["What is 1 + 1", "1 + 1 is 2", "a]1\nb]2\nc]3\nd]4\n:", {"b", "2"}], ...] def quiz[]: score = 0 for question in questions: print[question.question] user_answer = input[question.choices].lower[] if user_answer in question.correct: print["Correct"] score += 1 else: print["Incorrect", question.answer] print[score, "out of", len[questions], "that is", float[score / len[questions]] * 100, "%"] if __name__ == "__main__": quiz[]
44 là những từ đặc biệt trong Python có tên làfrom collections import namedtuple Question = namedtuple["Question", "question answer choices correct"] questions = [Question["What is 1 + 1", "1 + 1 is 2", "a]1\nb]2\nc]3\nd]4\n:", {"b", "2"}], ...] def quiz[]: score = 0 for question in questions: print[question.question] user_answer = input[question.choices].lower[] if user_answer in question.correct: print["Correct"] score += 1 else: print["Incorrect", question.answer] print[score, "out of", len[questions], "that is", float[score / len[questions]] * 100, "%"] if __name__ == "__main__": quiz[]
47, đại diện cho giá trị thực. Một câu lệnh chẳng hạn nhưfrom collections import namedtuple Question = namedtuple["Question", "question answer choices correct"] questions = [Question["What is 1 + 1", "1 + 1 is 2", "a]1\nb]2\nc]3\nd]4\n:", {"b", "2"}], ...] def quiz[]: score = 0 for question in questions: print[question.question] user_answer = input[question.choices].lower[] if user_answer in question.correct: print["Correct"] score += 1 else: print["Incorrect", question.answer] print[score, "out of", len[questions], "that is", float[score / len[questions]] * 100, "%"] if __name__ == "__main__": quiz[]
48 trả về giá trịfrom collections import namedtuple Question = namedtuple["Question", "question answer choices correct"] questions = [Question["What is 1 + 1", "1 + 1 is 2", "a]1\nb]2\nc]3\nd]4\n:", {"b", "2"}], ...] def quiz[]: score = 0 for question in questions: print[question.question] user_answer = input[question.choices].lower[] if user_answer in question.correct: print["Correct"] score += 1 else: print["Incorrect", question.answer] print[score, "out of", len[questions], "that is", float[score / len[questions]] * 100, "%"] if __name__ == "__main__": quiz[]
44, trong khifrom collections import namedtuple Question = namedtuple["Question", "question answer choices correct"] questions = [Question["What is 1 + 1", "1 + 1 is 2", "a]1\nb]2\nc]3\nd]4\n:", {"b", "2"}], ...] def quiz[]: score = 0 for question in questions: print[question.question] user_answer = input[question.choices].lower[] if user_answer in question.correct: print["Correct"] score += 1 else: print["Incorrect", question.answer] print[score, "out of", len[questions], "that is", float[score / len[questions]] * 100, "%"] if __name__ == "__main__": quiz[]
50 trả về giá trịfrom collections import namedtuple Question = namedtuple["Question", "question answer choices correct"] questions = [Question["What is 1 + 1", "1 + 1 is 2", "a]1\nb]2\nc]3\nd]4\n:", {"b", "2"}], ...] def quiz[]: score = 0 for question in questions: print[question.question] user_answer = input[question.choices].lower[] if user_answer in question.correct: print["Correct"] score += 1 else: print["Incorrect", question.answer] print[score, "out of", len[questions], "that is", float[score / len[questions]] * 100, "%"] if __name__ == "__main__": quiz[]
43from collections import namedtuple Question = namedtuple["Question", "question answer choices correct"] questions = [Question["What is 1 + 1", "1 + 1 is 2", "a]1\nb]2\nc]3\nd]4\n:", {"b", "2"}], ...] def quiz[]: score = 0 for question in questions: print[question.question] user_answer = input[question.choices].lower[] if user_answer in question.correct: print["Correct"] score += 1 else: print["Incorrect", question.answer] print[score, "out of", len[questions], "that is", float[score / len[questions]] * 100, "%"] if __name__ == "__main__": quiz[]
Kiểm tra dữ liệu của chúng tôi
Bây giờ, chúng ta đã thấy cách thức hoạt động của các điều kiện, chúng ta có thể sử dụng chúng để kiểm tra các tính năng đáng ngờ mà chúng ta đã thấy trong dữ liệu về tình trạng viêm nhiễm của mình. Chúng tôi sắp sử dụng lại các chức năng do mô-đun
from collections import namedtuple
Question = namedtuple["Question", "question answer choices correct"]
questions = [Question["What is 1 + 1", "1 + 1 is 2", "a]1\nb]2\nc]3\nd]4\n:", {"b", "2"}],
...]
def quiz[]:
score = 0
for question in questions:
print[question.question]
user_answer = input[question.choices].lower[]
if user_answer in question.correct:
print["Correct"]
score += 1
else:
print["Incorrect", question.answer]
print[score, "out of", len[questions], "that is",
float[score / len[questions]] * 100, "%"]
if __name__ == "__main__":
quiz[]
52 cung cấp. Do đó, nếu bạn đang làm việc trong phiên Python mới, hãy đảm bảo tải mô-đun bằngnot greater
done
0Từ một vài lô đầu tiên, chúng tôi đã thấy rằng mức viêm tối đa hàng ngày biểu hiện một hành vi kỳ lạ và tăng một đơn vị mỗi ngày. Sẽ không phải là một ý tưởng hay nếu phát hiện hành vi như vậy và báo cáo hành vi đó là đáng ngờ? . Tuy nhiên, thay vì kiểm tra từng ngày của nghiên cứu, chúng ta hãy chỉ kiểm tra xem mức độ viêm tối đa vào đầu [ngày 0] và giữa [ngày 20] của nghiên cứu có bằng với số ngày tương ứng hay không
not greater
done
1Chúng tôi cũng thấy một vấn đề khác trong tập dữ liệu thứ ba; . Chúng ta cũng có thể kiểm tra điều này với điều kiện
before conditional...
...after conditional
7not greater
done
2Và nếu cả hai điều kiện này đều không đúng, chúng ta có thể sử dụng
before conditional...
...after conditional
8 để đưa ra câu trả lời rõ ràngnot greater
done
3Hãy kiểm tra điều đó
not greater
done
4not greater
done
5not greater
done
6not greater
done
7Bằng cách này, chúng tôi đã yêu cầu Python làm điều gì đó khác tùy thuộc vào tình trạng dữ liệu của chúng tôi. Ở đây, chúng tôi đã in các thông báo trong mọi trường hợp, nhưng chúng tôi cũng có thể hình dung việc không sử dụng tính năng bắt tất cả của
before conditional...
...after conditional
8 để các thông báo chỉ được in khi có sự cố xảy ra, giúp chúng tôi không phải kiểm tra thủ công mọi biểu đồ để tìm các tính năng mà chúng tôi đã thấy trước đâyCó bao nhiêu con đường?
Hãy xem xét mã này
8not greater done
Nội dung nào sau đây sẽ được in nếu bạn chạy mã này?
- A
- B
- C
- B và C
Dung dịch
C được in vì hai điều kiện đầu tiên,
56 vàfrom collections import namedtuple Question = namedtuple["Question", "question answer choices correct"] questions = [Question["What is 1 + 1", "1 + 1 is 2", "a]1\nb]2\nc]3\nd]4\n:", {"b", "2"}], ...] def quiz[]: score = 0 for question in questions: print[question.question] user_answer = input[question.choices].lower[] if user_answer in question.correct: print["Correct"] score += 1 else: print["Incorrect", question.answer] print[score, "out of", len[questions], "that is", float[score / len[questions]] * 100, "%"] if __name__ == "__main__": quiz[]
57, không đúng, nhưngfrom collections import namedtuple Question = namedtuple["Question", "question answer choices correct"] questions = [Question["What is 1 + 1", "1 + 1 is 2", "a]1\nb]2\nc]3\nd]4\n:", {"b", "2"}], ...] def quiz[]: score = 0 for question in questions: print[question.question] user_answer = input[question.choices].lower[] if user_answer in question.correct: print["Correct"] score += 1 else: print["Incorrect", question.answer] print[score, "out of", len[questions], "that is", float[score / len[questions]] * 100, "%"] if __name__ == "__main__": quiz[]
58 là đúngfrom collections import namedtuple Question = namedtuple["Question", "question answer choices correct"] questions = [Question["What is 1 + 1", "1 + 1 is 2", "a]1\nb]2\nc]3\nd]4\n:", {"b", "2"}], ...] def quiz[]: score = 0 for question in questions: print[question.question] user_answer = input[question.choices].lower[] if user_answer in question.correct: print["Correct"] score += 1 else: print["Incorrect", question.answer] print[score, "out of", len[questions], "that is", float[score / len[questions]] * 100, "%"] if __name__ == "__main__": quiz[]
Sự thật là gì?
Các phép toán luận
43 vàfrom collections import namedtuple Question = namedtuple["Question", "question answer choices correct"] questions = [Question["What is 1 + 1", "1 + 1 is 2", "a]1\nb]2\nc]3\nd]4\n:", {"b", "2"}], ...] def quiz[]: score = 0 for question in questions: print[question.question] user_answer = input[question.choices].lower[] if user_answer in question.correct: print["Correct"] score += 1 else: print["Incorrect", question.answer] print[score, "out of", len[questions], "that is", float[score / len[questions]] * 100, "%"] if __name__ == "__main__": quiz[]
44 không phải là giá trị duy nhất trong Python là đúng và sai. Trên thực tế, bất kỳ giá trị nào cũng có thể được sử dụng trongfrom collections import namedtuple Question = namedtuple["Question", "question answer choices correct"] questions = [Question["What is 1 + 1", "1 + 1 is 2", "a]1\nb]2\nc]3\nd]4\n:", {"b", "2"}], ...] def quiz[]: score = 0 for question in questions: print[question.question] user_answer = input[question.choices].lower[] if user_answer in question.correct: print["Correct"] score += 1 else: print["Incorrect", question.answer] print[score, "out of", len[questions], "that is", float[score / len[questions]] * 100, "%"] if __name__ == "__main__": quiz[]
6 hoặcbefore conditional... ...after conditional
7. Sau khi đọc và chạy đoạn mã bên dưới, hãy giải thích quy tắc dành cho giá trị nào được coi là đúng và giá trị nào được coi là saibefore conditional... ...after conditional
9not greater done
Đó không phải là điều tôi muốn nói
Đôi khi rất hữu ích để kiểm tra xem một số điều kiện có đúng không. Toán tử Boolean
73 có thể làm điều này một cách rõ ràng. Sau khi đọc và chạy mã bên dưới, hãy viết một số câu lệnhfrom collections import namedtuple Question = namedtuple["Question", "question answer choices correct"] questions = [Question["What is 1 + 1", "1 + 1 is 2", "a]1\nb]2\nc]3\nd]4\n:", {"b", "2"}], ...] def quiz[]: score = 0 for question in questions: print[question.question] user_answer = input[question.choices].lower[] if user_answer in question.correct: print["Correct"] score += 1 else: print["Incorrect", question.answer] print[score, "out of", len[questions], "that is", float[score / len[questions]] * 100, "%"] if __name__ == "__main__": quiz[]
6 sử dụngbefore conditional... ...after conditional
73 để kiểm tra quy tắc mà bạn đã xây dựng trong thử thách trướcfrom collections import namedtuple Question = namedtuple["Question", "question answer choices correct"] questions = [Question["What is 1 + 1", "1 + 1 is 2", "a]1\nb]2\nc]3\nd]4\n:", {"b", "2"}], ...] def quiz[]: score = 0 for question in questions: print[question.question] user_answer = input[question.choices].lower[] if user_answer in question.correct: print["Correct"] score += 1 else: print["Incorrect", question.answer] print[score, "out of", len[questions], "that is", float[score / len[questions]] * 100, "%"] if __name__ == "__main__": quiz[]
50class Question: def __init__[self, question, answer, choices, correct]: self.question = question self.answer = answer self.choices = choices self.correct = correct
Đủ gần
Viết một số điều kiện để in ra
43 nếu biếnfrom collections import namedtuple Question = namedtuple["Question", "question answer choices correct"] questions = [Question["What is 1 + 1", "1 + 1 is 2", "a]1\nb]2\nc]3\nd]4\n:", {"b", "2"}], ...] def quiz[]: score = 0 for question in questions: print[question.question] user_answer = input[question.choices].lower[] if user_answer in question.correct: print["Correct"] score += 1 else: print["Incorrect", question.answer] print[score, "out of", len[questions], "that is", float[score / len[questions]] * 100, "%"] if __name__ == "__main__": quiz[]
77 nằm trong phạm vi 10% của biếnfrom collections import namedtuple Question = namedtuple["Question", "question answer choices correct"] questions = [Question["What is 1 + 1", "1 + 1 is 2", "a]1\nb]2\nc]3\nd]4\n:", {"b", "2"}], ...] def quiz[]: score = 0 for question in questions: print[question.question] user_answer = input[question.choices].lower[] if user_answer in question.correct: print["Correct"] score += 1 else: print["Incorrect", question.answer] print[score, "out of", len[questions], "that is", float[score / len[questions]] * 100, "%"] if __name__ == "__main__": quiz[]
78 và ngược lại làfrom collections import namedtuple Question = namedtuple["Question", "question answer choices correct"] questions = [Question["What is 1 + 1", "1 + 1 is 2", "a]1\nb]2\nc]3\nd]4\n:", {"b", "2"}], ...] def quiz[]: score = 0 for question in questions: print[question.question] user_answer = input[question.choices].lower[] if user_answer in question.correct: print["Correct"] score += 1 else: print["Incorrect", question.answer] print[score, "out of", len[questions], "that is", float[score / len[questions]] * 100, "%"] if __name__ == "__main__": quiz[]
44. So sánh việc triển khai của bạn với đối tác của bạn. bạn có nhận được cùng một câu trả lời cho tất cả các cặp số có thể không?from collections import namedtuple Question = namedtuple["Question", "question answer choices correct"] questions = [Question["What is 1 + 1", "1 + 1 is 2", "a]1\nb]2\nc]3\nd]4\n:", {"b", "2"}], ...] def quiz[]: score = 0 for question in questions: print[question.question] user_answer = input[question.choices].lower[] if user_answer in question.correct: print["Correct"] score += 1 else: print["Incorrect", question.answer] print[score, "out of", len[questions], "that is", float[score / len[questions]] * 100, "%"] if __name__ == "__main__": quiz[]
Dấu
Có một hàm tích hợp sẵn
80 trả về giá trị tuyệt đối của một sốfrom collections import namedtuple Question = namedtuple["Question", "question answer choices correct"] questions = [Question["What is 1 + 1", "1 + 1 is 2", "a]1\nb]2\nc]3\nd]4\n:", {"b", "2"}], ...] def quiz[]: score = 0 for question in questions: print[question.question] user_answer = input[question.choices].lower[] if user_answer in question.correct: print["Correct"] score += 1 else: print["Incorrect", question.answer] print[score, "out of", len[questions], "that is", float[score / len[questions]] * 100, "%"] if __name__ == "__main__": quiz[]
51class Question: def __init__[self, question, answer, choices, correct]: self.question = question self.answer = answer self.choices = choices self.correct = correct
52class Question: def __init__[self, question, answer, choices, correct]: self.question = question self.answer = answer self.choices = choices self.correct = correct
Giải pháp 1
53class Question: def __init__[self, question, answer, choices, correct]: self.question = question self.answer = answer self.choices = choices self.correct = correct
Giải pháp 2
54class Question: def __init__[self, question, answer, choices, correct]: self.question = question self.answer = answer self.choices = choices self.correct = correct
Điều này hoạt động vì Booleans
43 vàfrom collections import namedtuple Question = namedtuple["Question", "question answer choices correct"] questions = [Question["What is 1 + 1", "1 + 1 is 2", "a]1\nb]2\nc]3\nd]4\n:", {"b", "2"}], ...] def quiz[]: score = 0 for question in questions: print[question.question] user_answer = input[question.choices].lower[] if user_answer in question.correct: print["Correct"] score += 1 else: print["Incorrect", question.answer] print[score, "out of", len[questions], "that is", float[score / len[questions]] * 100, "%"] if __name__ == "__main__": quiz[]
44 có các biểu diễn chuỗi có thể được infrom collections import namedtuple Question = namedtuple["Question", "question answer choices correct"] questions = [Question["What is 1 + 1", "1 + 1 is 2", "a]1\nb]2\nc]3\nd]4\n:", {"b", "2"}], ...] def quiz[]: score = 0 for question in questions: print[question.question] user_answer = input[question.choices].lower[] if user_answer in question.correct: print["Correct"] score += 1 else: print["Incorrect", question.answer] print[score, "out of", len[questions], "that is", float[score / len[questions]] * 100, "%"] if __name__ == "__main__": quiz[]
Người vận hành tại chỗ
Python [và hầu hết các ngôn ngữ khác trong họ C] cung cấp các toán tử tại chỗ hoạt động như thế này
55class Question: def __init__[self, question, answer, choices, correct]: self.question = question self.answer = answer self.choices = choices self.correct = correct
56class Question: def __init__[self, question, answer, choices, correct]: self.question = question self.answer = answer self.choices = choices self.correct = correct
Viết một số mã tính tổng các số dương và số âm trong một danh sách riêng biệt, sử dụng các toán tử tại chỗ. Bạn có nghĩ rằng kết quả dễ đọc hơn hoặc ít hơn so với viết giống nhau mà không có toán tử tại chỗ không?
Dung dịch
57class Question: def __init__[self, question, answer, choices, correct]: self.question = question self.answer = answer self.choices = choices self.correct = correct
Ở đây
83 có nghĩa là “đừng làm gì cả”. Trong trường hợp cụ thể này, nó không thực sự cần thiết, vì nếufrom collections import namedtuple Question = namedtuple["Question", "question answer choices correct"] questions = [Question["What is 1 + 1", "1 + 1 is 2", "a]1\nb]2\nc]3\nd]4\n:", {"b", "2"}], ...] def quiz[]: score = 0 for question in questions: print[question.question] user_answer = input[question.choices].lower[] if user_answer in question.correct: print["Correct"] score += 1 else: print["Incorrect", question.answer] print[score, "out of", len[questions], "that is", float[score / len[questions]] * 100, "%"] if __name__ == "__main__": quiz[]
84 thì tổng không cần thay đổi, nhưng nó minh họa việc sử dụngfrom collections import namedtuple Question = namedtuple["Question", "question answer choices correct"] questions = [Question["What is 1 + 1", "1 + 1 is 2", "a]1\nb]2\nc]3\nd]4\n:", {"b", "2"}], ...] def quiz[]: score = 0 for question in questions: print[question.question] user_answer = input[question.choices].lower[] if user_answer in question.correct: print["Correct"] score += 1 else: print["Incorrect", question.answer] print[score, "out of", len[questions], "that is", float[score / len[questions]] * 100, "%"] if __name__ == "__main__": quiz[]
7 vàbefore conditional... ...after conditional
83from collections import namedtuple Question = namedtuple["Question", "question answer choices correct"] questions = [Question["What is 1 + 1", "1 + 1 is 2", "a]1\nb]2\nc]3\nd]4\n:", {"b", "2"}], ...] def quiz[]: score = 0 for question in questions: print[question.question] user_answer = input[question.choices].lower[] if user_answer in question.correct: print["Correct"] score += 1 else: print["Incorrect", question.answer] print[score, "out of", len[questions], "that is", float[score / len[questions]] * 100, "%"] if __name__ == "__main__": quiz[]
Sắp xếp một danh sách vào nhóm
Trong thư mục
87 của chúng tôi, các tập dữ liệu lớn được lưu trữ trong các tệp có tên bắt đầu bằng “viêm-“ và các tập dữ liệu nhỏ – trong các tệp có tên bắt đầu bằng “nhỏ-“. Chúng tôi cũng có một số tệp khác mà chúng tôi không quan tâm vào thời điểm này. Chúng tôi muốn chia tất cả các tệp này thành ba danh sách có tên lần lượt làfrom collections import namedtuple Question = namedtuple["Question", "question answer choices correct"] questions = [Question["What is 1 + 1", "1 + 1 is 2", "a]1\nb]2\nc]3\nd]4\n:", {"b", "2"}], ...] def quiz[]: score = 0 for question in questions: print[question.question] user_answer = input[question.choices].lower[] if user_answer in question.correct: print["Correct"] score += 1 else: print["Incorrect", question.answer] print[score, "out of", len[questions], "that is", float[score / len[questions]] * 100, "%"] if __name__ == "__main__": quiz[]
88,from collections import namedtuple Question = namedtuple["Question", "question answer choices correct"] questions = [Question["What is 1 + 1", "1 + 1 is 2", "a]1\nb]2\nc]3\nd]4\n:", {"b", "2"}], ...] def quiz[]: score = 0 for question in questions: print[question.question] user_answer = input[question.choices].lower[] if user_answer in question.correct: print["Correct"] score += 1 else: print["Incorrect", question.answer] print[score, "out of", len[questions], "that is", float[score / len[questions]] * 100, "%"] if __name__ == "__main__": quiz[]
89 vàfrom collections import namedtuple Question = namedtuple["Question", "question answer choices correct"] questions = [Question["What is 1 + 1", "1 + 1 is 2", "a]1\nb]2\nc]3\nd]4\n:", {"b", "2"}], ...] def quiz[]: score = 0 for question in questions: print[question.question] user_answer = input[question.choices].lower[] if user_answer in question.correct: print["Correct"] score += 1 else: print["Incorrect", question.answer] print[score, "out of", len[questions], "that is", float[score / len[questions]] * 100, "%"] if __name__ == "__main__": quiz[]
00not greater done
Thêm mã vào mẫu bên dưới để thực hiện việc này. Lưu ý rằng phương thức chuỗi
01 trả vềnot greater done
43 khi và chỉ khi chuỗi mà nó được gọi bắt đầu bằng chuỗi được truyền dưới dạng đối số, nghĩa làfrom collections import namedtuple Question = namedtuple["Question", "question answer choices correct"] questions = [Question["What is 1 + 1", "1 + 1 is 2", "a]1\nb]2\nc]3\nd]4\n:", {"b", "2"}], ...] def quiz[]: score = 0 for question in questions: print[question.question] user_answer = input[question.choices].lower[] if user_answer in question.correct: print["Correct"] score += 1 else: print["Incorrect", question.answer] print[score, "out of", len[questions], "that is", float[score / len[questions]] * 100, "%"] if __name__ == "__main__": quiz[]
58class Question: def __init__[self, question, answer, choices, correct]: self.question = question self.answer = answer self.choices = choices self.correct = correct
59class Question: def __init__[self, question, answer, choices, correct]: self.question = question self.answer = answer self.choices = choices self.correct = correct
Nhưng mà
0before conditional... ...after conditional
1before conditional... ...after conditional
Sử dụng mã Python sau làm điểm xuất phát của bạn
2before conditional... ...after conditional
Giải pháp của bạn nên
- lặp qua tên của các tập tin
- tìm ra nhóm mà mỗi tên tệp thuộc về
- nối tên tệp vào danh sách đó
Cuối cùng, ba danh sách nên được
3before conditional... ...after conditional
Dung dịch
4before conditional... ...after conditional
đếm nguyên âm
- Viết vòng lặp đếm số nguyên âm trong một chuỗi ký tự
- Kiểm tra nó trên một vài từ riêng lẻ và câu đầy đủ
- Sau khi hoàn thành, hãy so sánh giải pháp của bạn với giải pháp của hàng xóm. Bạn có đưa ra quyết định tương tự về cách xử lý chữ cái 'y' [mà một số người cho là nguyên âm, còn một số thì không]?
Dung dịch
5before conditional... ...after conditional
Những điểm chính
Sử dụng
03 để bắt đầu một tuyên bố có điều kiện,not greater done
04 để cung cấp các phép kiểm tra bổ sung vànot greater done
8 để cung cấp một mặc địnhbefore conditional... ...after conditional
Phần thân của các nhánh của câu lệnh điều kiện phải được thụt vào
Sử dụng
99 để kiểm tra sự bình đẳngclass Question: def __init__[self, question, answer, choices, correct]: self.question = question self.answer = answer self.choices = choices self.correct = correct
07 chỉ đúng nếu cảnot greater done
08 vànot greater done
09 đều đúngnot greater done
10 là đúng nếunot greater done
08 hoặcnot greater done
09 hoặc cả hai đều đúngnot greater done
Số không, chuỗi trống và danh sách trống được coi là sai;