Làm thế nào để bạn thực hiện nhiều lựa chọn trong python?

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
15

questions = ["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ày

class 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 sai

class Question:
    def __init__[self, question, answer, choices, correct]:
        self.question = question
        self.answer = answer
        self.choices = choices
        self.correct = correct
5

before 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'}
0

Lư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ử

>>> 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'}
01 và
class Question:
    def __init__[self, question, answer, choices, correct]:
        self.question = question
        self.answer = answer
        self.choices = choices
        self.correct = correct
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ề

  • >>> 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'}
    
    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
  • class Question:
        def __init__[self, question, answer, choices, correct]:
            self.question = question
            self.answer = answer
            self.choices = choices
            self.correct = correct
    
    99. tương đương với
  • >>> 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'}
    
    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

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 đú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[]
4

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[]
5

trong 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à đú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[]
7

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[]
8

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[]
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

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[]
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 khi
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[]
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[]
43

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ằng

not greater
done
0

Từ 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
1

Chú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
7

not greater
done
2

Và 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àng

not greater
done
3

Hãy kiểm tra điều đó

not greater
done
4

not greater
done
5

not greater
done
6

not greater
done
7

Bằ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 đây

Có bao nhiêu con đường?

Hãy xem xét mã này

not greater
done
8

Nội dung nào sau đây sẽ được in nếu bạn chạy mã này?

  1. A
  2. B
  3. C
  4. B và C

Dung dịch

C được in vì hai điều kiện đầu tiên,

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[]
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ư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[]
58 là đúng

Sự thật là gì?

Các phép toán luận

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[]
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 trong
before conditional...
...after conditional
6 hoặc
before 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à sai

not greater
done
9

Đó 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

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[]
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ệnh
before conditional...
...after conditional
6 sử dụ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[]
73 để kiểm tra quy tắc mà bạn đã xây dựng trong thử thách trước

class Question:
    def __init__[self, question, answer, choices, correct]:
        self.question = question
        self.answer = answer
        self.choices = choices
        self.correct = correct
50

Đủ gần

Viết một số điều kiện để in ra

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[]
43 nếu biến
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[]
77 nằm trong phạm vi 10% của biến
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[]
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?

Dấu

Có một hàm tích hợp sẵn

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[]
80 trả về giá trị tuyệt đối 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
51

class Question:
    def __init__[self, question, answer, choices, correct]:
        self.question = question
        self.answer = answer
        self.choices = choices
        self.correct = correct
52

Giải pháp 1

class Question:
    def __init__[self, question, answer, choices, correct]:
        self.question = question
        self.answer = answer
        self.choices = choices
        self.correct = correct
53

Giải pháp 2

class Question:
    def __init__[self, question, answer, choices, correct]:
        self.question = question
        self.answer = answer
        self.choices = choices
        self.correct = correct
54

Điều này hoạt động vì Booleans

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[]
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 in

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

class Question:
    def __init__[self, question, answer, choices, correct]:
        self.question = question
        self.answer = answer
        self.choices = choices
        self.correct = correct
55

class Question:
    def __init__[self, question, answer, choices, correct]:
        self.question = question
        self.answer = answer
        self.choices = choices
        self.correct = correct
56

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

class Question:
    def __init__[self, question, answer, choices, correct]:
        self.question = question
        self.answer = answer
        self.choices = choices
        self.correct = correct
57

Ở đây

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[]
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ếu
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[]
84 thì tổng không cần thay đổi, nhưng nó minh họa việc sử dụng
before conditional...
...after conditional
7 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[]
83

Sắp xếp một danh sách vào nhóm

Trong thư mục

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[]
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à
not greater
done
00

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

not greater
done
01 trả 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[]
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à

class Question:
    def __init__[self, question, answer, choices, correct]:
        self.question = question
        self.answer = answer
        self.choices = choices
        self.correct = correct
58

class Question:
    def __init__[self, question, answer, choices, correct]:
        self.question = question
        self.answer = answer
        self.choices = choices
        self.correct = correct
59

Nhưng mà

before conditional...
...after conditional
0

before conditional...
...after conditional
1

Sử dụng mã Python sau làm điểm xuất phát của bạn

before conditional...
...after conditional
2

Giải pháp của bạn nên

  1. lặp qua tên của các tập tin
  2. tìm ra nhóm mà mỗi tên tệp thuộc về
  3. nối tên tệp vào danh sách đó

Cuối cùng, ba danh sách nên được

before conditional...
...after conditional
3

Dung dịch

before conditional...
...after conditional
4

đếm nguyên âm

  1. Viết vòng lặp đếm số nguyên âm trong một chuỗi ký tự
  2. Kiểm tra nó trên một vài từ riêng lẻ và câu đầy đủ
  3. 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

before conditional...
...after conditional
5

Những điểm chính

  • Sử dụng

    not greater
    done
    
    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à
    before conditional...
    ...after conditional
    
    8 để cung cấp một mặc định

  • 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

    class Question:
        def __init__[self, question, answer, choices, correct]:
            self.question = question
            self.answer = answer
            self.choices = choices
            self.correct = correct
    
    99 để kiểm tra sự bình đẳng

  • not greater
    done
    
    07 chỉ đúng nếu cả
    not greater
    done
    
    08 và
    not greater
    done
    
    09 đều đúng

  • not greater
    done
    
    10 là đúng nếu
    not greater
    done
    
    08 hoặc
    not greater
    done
    
    09 hoặc cả hai đều đúng

  • Số không, chuỗi trống và danh sách trống được coi là sai;

Chủ Đề