Hướng dẫn simple python quiz code with score - mã câu đố python đơn giản với điểm số

Trong bài đăng này, bạn tìm hiểu cách tạo một chương trình trò chơi đố đơn giản trong Python với một lời giải thích và thuật toán thích hợp.

Chương trình Python này được coi là một dự án nhỏ cho người mới bắt đầu và cũng giúp họ tăng cường kiến ​​thức và sự tự tin của họ [dự án này giúp bạn tự tin tìm hiểu ngày càng nhiều] nhưng trước khi viết chương trình, hãy để hiểu thuật toán cho một trò chơi đố vui.

Thuật toán cho một trò chơi bài kiểm tra Python đơn giản

  1. Tạo một biến để đếm điểm, ví dụ
    # quiz.py
    
    answer = input["When was the first known use of the word 'quiz'? "]
    if answer == "1781":
        print["Correct!"]
    else:
        print[f"The answer is '1781', not {answer!r}"]
    
    answer = input["Which built-in function can get information from the user? "]
    if answer == "input":
        print["Correct!"]
    else:
        print[f"The answer is 'input', not {answer!r}"]
    
    8.
    # quiz.py
    
    answer = input["When was the first known use of the word 'quiz'? "]
    if answer == "1781":
        print["Correct!"]
    else:
        print[f"The answer is '1781', not {answer!r}"]
    
    answer = input["Which built-in function can get information from the user? "]
    if answer == "input":
        print["Correct!"]
    else:
        print[f"The answer is 'input', not {answer!r}"]
    
    8
    .
  2. Đặt câu hỏi cho người dùng [sử dụng chức năng
    # quiz.py
    
    answer = input["When was the first known use of the word 'quiz'? "]
    if answer == "1781":
        print["Correct!"]
    else:
        print[f"The answer is '1781', not {answer!r}"]
    
    answer = input["Which built-in function can get information from the user? "]
    if answer == "input":
        print["Correct!"]
    else:
        print[f"The answer is 'input', not {answer!r}"]
    
    9]
    # quiz.py
    
    answer = input["When was the first known use of the word 'quiz'? "]
    if answer == "1781":
        print["Correct!"]
    else:
        print[f"The answer is '1781', not {answer!r}"]
    
    answer = input["Which built-in function can get information from the user? "]
    if answer == "input":
        print["Correct!"]
    else:
        print[f"The answer is 'input', not {answer!r}"]
    
    9
    function]
  3. Nếu người dùng đưa ra câu trả lời đúng thì hãy tăng
    $ python quiz.py
    When was the first known use of the word 'quiz'? 1781
    Correct!
    Which built-in function can get information from the user? get
    The answer is 'input', not 'get'
    
    0 lên 1 và ____711 and
    $ python quiz.py
    When was the first known use of the word 'quiz'? 1781
    Correct!
    Which built-in function can get information from the user? get
    The answer is 'input', not 'get'
    
    1
  4. Nếu câu trả lời cho người dùng không chính xác thì
    $ python quiz.py
    When was the first known use of the word 'quiz'? 1781
    Correct!
    Which built-in function can get information from the user? get
    The answer is 'input', not 'get'
    
    2
  5. và ở bản in cuối cùng
    # quiz.py
    
    answer = input["When was the first known use of the word 'quiz'? "]
    if answer == "1781":
        print["Correct!"]
    else:
        print[f"The answer is '1781', not {answer!r}"]
    
    answer = input["Which built-in function can get information from the user? "]
    if answer == "input":
        print["Correct!"]
    else:
        print[f"The answer is 'input', not {answer!r}"]
    
    8 của người dùng.
    # quiz.py
    
    answer = input["When was the first known use of the word 'quiz'? "]
    if answer == "1781":
        print["Correct!"]
    else:
        print[f"The answer is '1781', not {answer!r}"]
    
    answer = input["Which built-in function can get information from the user? "]
    if answer == "input":
        print["Correct!"]
    else:
        print[f"The answer is 'input', not {answer!r}"]
    
    8
    of the user.

Bây giờ bạn đã hiểu cách thực hiện một trò chơi đố đơn giản trong Python thông qua một thuật toán trên.

Một vài khái niệm lập trình mà bạn biết cho chương trình này, chẳng hạn như

  1. if-else
  2. Nested if-Else
  3. Cách đưa đầu vào người dùng trong Python.

Mã nguồn

Câu hỏi Hỏi như: Viết một chương trình Python để tạo một trò chơi đố.

print["Wellcome to quiz game !!"]
print['NOTE: if your spelling is incorrect then it is considered as wrong answer']
score = 0
question_no = 0
playing = input['Do you want to play ? '].lower[]
if playing == 'yes':
    question_no += 1
    ques = input[f'\n{question_no}. what does CPU stand for? '].lower[]
    if ques == 'central processing unit':
        score +=1
        print['correct! you got 1 point']
        
    else:
        print['Incorrect!']
        print[f'current answer is --> central processing unit']

# ------1
    question_no += 1
    ques = input[f'\n{question_no}. what does GPU stand for? '].lower[]
    
    if ques == 'graphics processing unit':
        score +=1
        print['correct! you got 1 point']
        
    else:
        print['Incorrect!']
        print[f'current answer is --> graphics processing unit']

# -----2
    question_no += 1
    ques = input[f'\n{question_no}. what does RAM stand for? '].lower[]
    
    if ques == 'random access memory':
        score +=1
        print['correct! you got 1 point']
        
    else:
        print['Incorrect!']
        print[f'current answer is --> random access memory']

# -----3
    question_no += 1
    ques = input[f'\n{question_no}. what does PSU stand for? '].lower[]
    
    if ques == 'power supply unit':
        score +=1
        print['correct! you got 1 point']
        
    else:
        print['Incorrect!']
        print[f'current answer is --> power supply unit']


# -----4
    question_no += 1
    ques = input[f'\n{question_no}. what does ROM stand for? '].lower[]
    
    if ques == 'read only memory':
        score +=1
        print['correct! you got 1 point']
        
    else:
        print['Incorrect!']
        print[f'current answer is --> read only memory']


# ------5 

else:
    print['thankyou you are out of a game.']
    quit[]

print[f'\nnumber of question is {question_no}']
print[f'your score is {score}']
try:
    percentage = [score *100]/question_no
except ZeroDivisionError:
    print['0% quetions are correct']

print[f'{percentage}% questions are correct.']

Đầu ra

Wellcome to quiz game !!
NOTE: if your spelling is incorrect then it is considered as wrong answer
Do you want to play ? yes

1. what does CPU stand for? central processing unit
correct! you got 1 point

2. what does GPU stand for? graphics processing unit
correct! you got 1 point

3. what does RAM stand for? RAM
Incorrect!
current answer is --> random access memory

4. what does PSU stand for? power supplyy unit
Incorrect!
current answer is --> power supply unit

5. what does ROM stand for? read only memory
correct! you got 1 point

number of question is 5
your score is 3
60.0% questions are correct.

Hy vọng bài đăng này thêm một số giá trị cho cuộc sống của bạn - cảm ơn bạn.

Một dự án khác:

Trò chơi Hangman

Máy tính đơn giản

Trò chơi đoán số

Quan điểm đăng: 423 423

Trong hướng dẫn này, bạn sẽ xây dựng một ứng dụng đố vui cho thiết bị đầu cuối. Câu đố từ lần đầu tiên được sử dụng vào năm 1781 để có nghĩa là người lập dị. Ngày nay, nó chủ yếu được sử dụng để mô tả các bài kiểm tra ngắn về những câu đố hoặc kiến ​​thức chuyên môn với các câu hỏi như sau:Python quiz application for the terminal. The word quiz was first used in 1781 to mean eccentric person. Nowadays, it’s mostly used to describe short tests of trivia or expert knowledge with questions like the following:

Lần đầu tiên sử dụng bài kiểm tra từ là khi nào?

Bằng cách làm theo dự án từng bước này, bạn sẽ xây dựng một ứng dụng có thể kiểm tra chuyên môn của một người về một loạt các chủ đề. Bạn có thể sử dụng dự án này để tăng cường kiến ​​thức của riêng bạn hoặc thách thức bạn bè của bạn tham gia một trận chiến trí thông minh vui vẻ.

Trong hướng dẫn này, bạn sẽ học cách:

  • Tương tác với người dùng trong thiết bị đầu cuối with the user in the terminal
  • Cải thiện khả năng sử dụng ứng dụng của bạn the usability of your application
  • Tái cấu trúc ứng dụng của bạn để liên tục cải thiện nó your application to continuously improve it
  • Lưu trữ dữ liệu trong các tệp dữ liệu chuyên dụng data in dedicated data files

Ứng dụng Quiz là một dự án toàn diện cho bất kỳ ai thoải mái với những điều cơ bản của Python. Trong suốt hướng dẫn, bạn sẽ nhận được tất cả các mã bạn cần trong các bước riêng biệt, có kích thước cắn. Bạn cũng có thể tìm thấy mã nguồn đầy đủ của ứng dụng bằng cách nhấp vào liên kết bên dưới:

Cho dù bạn có phải là người lập dị hay không, hãy đọc tiếp để tìm hiểu cách tạo bài kiểm tra của riêng bạn.

Bản demo: Ứng dụng Quiz Python của bạn

Trong dự án từng bước này, bạn sẽ xây dựng một ứng dụng thiết bị đầu cuối có thể đố bạn và bạn bè của bạn trên một loạt các chủ đề:

Trước tiên bạn chọn một chủ đề cho câu hỏi của bạn. Sau đó, với mỗi câu hỏi, bạn sẽ chọn một câu trả lời từ một tập hợp các lựa chọn thay thế. Một số câu hỏi có thể có nhiều câu trả lời đúng. Bạn có thể truy cập một gợi ý để giúp bạn trên đường đi. Sau khi trả lời một câu hỏi, bạn sẽ đọc một lời giải thích có thể cung cấp nhiều bối cảnh hơn cho câu trả lời.

Tổng quan dự án

Bạn sẽ bắt đầu bằng cách tạo một ứng dụng đố Python cơ bản mà chỉ có khả năng đặt câu hỏi, thu thập câu trả lời và kiểm tra xem câu trả lời có chính xác hay không. Từ đó, bạn sẽ thêm ngày càng nhiều tính năng để làm cho ứng dụng của bạn thú vị hơn, thân thiện với người dùng và thú vị.

Bạn sẽ xây dựng ứng dụng đố vui lặp đi lặp lại bằng cách thực hiện các bước sau:

  1. Tạo một ứng dụng cơ bản có thể hỏi các câu hỏi trắc nghiệm.
  2. Làm cho ứng dụng thân thiện với người dùng hơn bằng cách cải thiện cách nó và cách nó xử lý các lỗi của người dùng.
  3. Tái cấu trúc mã để sử dụng các chức năng.
  4. Dữ liệu câu hỏi riêng biệt từ mã nguồn bằng cách lưu trữ các câu hỏi trong một tệp dữ liệu chuyên dụng.
  5. Mở rộng ứng dụng để xử lý nhiều câu trả lời đúng, đưa ra gợi ý và đưa ra giải thích.
  6. Thêm sự quan tâm bằng cách hỗ trợ các chủ đề đố khác nhau để lựa chọn.

Khi bạn theo dõi, bạn sẽ có được kinh nghiệm khi bắt đầu với một kịch bản nhỏ và mở rộng nó. Đây là một kỹ năng quan trọng trong và của chính nó. Chương trình, ứng dụng hoặc trò chơi yêu thích của bạn có thể bắt đầu như một bằng chứng nhỏ về khái niệm mà sau này đã phát triển thành ngày nay.

Điều kiện tiên quyết

Trong hướng dẫn này, bạn sẽ xây dựng một ứng dụng đố bằng cách sử dụng các khối xây dựng cơ bản của Python. Trong khi làm việc thông qua các bước, nó rất hữu ích nếu bạn thoải mái với các khái niệm sau:

  • Đọc đầu vào từ người dùng tại thiết bị đầu cuối
  • Tổ chức dữ liệu trong các cấu trúc như danh sách, bộ dữ liệu và từ điển
  • Sử dụng các câu lệnh
    $ python quiz.py
    When was the first known use of the word 'quiz'? 1781
    Correct!
    Which built-in function can get information from the user? get
    The answer is 'input', not 'get'
    
    4 để kiểm tra các điều kiện khác nhau
  • Lặp lại các hành động với các vòng lặp
    $ python quiz.py
    When was the first known use of the word 'quiz'? 1781
    Correct!
    Which built-in function can get information from the user? get
    The answer is 'input', not 'get'
    
    5 và
    $ python quiz.py
    When was the first known use of the word 'quiz'? 1781
    Correct!
    Which built-in function can get information from the user? get
    The answer is 'input', not 'get'
    
    6
  • Đóng gói mã với các chức năng

Nếu bạn không tự tin vào kiến ​​thức của mình về những điều kiện tiên quyết này, thì điều đó cũng không sao! Trong thực tế, đi qua hướng dẫn này sẽ giúp bạn thực hành các khái niệm này. Bạn luôn có thể dừng lại và xem xét các tài nguyên được liên kết ở trên nếu bạn bị mắc kẹt.

Bước 1: Đặt câu hỏi

Trong bước này, bạn sẽ học cách tạo một chương trình có thể đặt câu hỏi và kiểm tra câu trả lời. Đây sẽ là nền tảng của ứng dụng đố vui của bạn, mà bạn sẽ cải thiện trong phần còn lại của hướng dẫn. Vào cuối bước này, chương trình của bạn sẽ trông như thế này:

Chương trình của bạn sẽ có thể đặt câu hỏi và kiểm tra câu trả lời. Phiên bản này bao gồm chức năng cơ bản mà bạn cần, nhưng bạn sẽ thêm nhiều chức năng hơn trong các bước sau. Nếu bạn thích, thì bạn có thể tải xuống mã nguồn vì nó sẽ xem xét khi bạn đã hoàn thành bước này bằng cách nhấp vào liên kết bên dưới và nhập thư mục

$ python quiz.py
When was the first known use of the word 'quiz'? 1781
Correct!
Which built-in function can get information from the user? get
The answer is 'input', not 'get'
7:

Nhận thông tin người dùng với
# quiz.py

answer = input["When was the first known use of the word 'quiz'? "]
if answer == "1781":
    print["Correct!"]
else:
    print[f"The answer is '1781', not {answer!r}"]

answer = input["Which built-in function can get information from the user? "]
if answer == "input":
    print["Correct!"]
else:
    print[f"The answer is 'input', not {answer!r}"]
9

Một trong những chức năng tích hợp của Python, là

# quiz.py

answer = input["When was the first known use of the word 'quiz'? "]
if answer == "1781":
    print["Correct!"]
else:
    print[f"The answer is '1781', not {answer!r}"]

answer = input["Which built-in function can get information from the user? "]
if answer == "input":
    print["Correct!"]
else:
    print[f"The answer is 'input', not {answer!r}"]
9. Bạn có thể sử dụng nó để lấy thông tin từ người dùng. Ví dụ đầu tiên, hãy chạy những điều sau đây trong Python Repls:

>>>

>>> name = input["What's your name? "]
What's your name? Geir Arne

>>> name
'Geir Arne'

# quiz.py

answer = input["When was the first known use of the word 'quiz'? "]
if answer == "1781":
    print["Correct!"]
else:
    print[f"The answer is '1781', not {answer!r}"]

answer = input["Which built-in function can get information from the user? "]
if answer == "input":
    print["Correct!"]
else:
    print[f"The answer is 'input', not {answer!r}"]
9 lấy một lời nhắc tùy chọn mà LỚN hiển thị cho người dùng trước khi người dùng nhập thông tin. Trong ví dụ trên, lời nhắc được hiển thị trong dòng được tô sáng và người dùng nhập
[
    ["When was the first known use of the word 'quiz'", "1781"],
    ["Which built-in function can get information from the user", "input"],
]
1 trước khi nhấn enter. Bất cứ điều gì người dùng nhập đều được trả về từ
# quiz.py

answer = input["When was the first known use of the word 'quiz'? "]
if answer == "1781":
    print["Correct!"]
else:
    print[f"The answer is '1781', not {answer!r}"]

answer = input["Which built-in function can get information from the user? "]
if answer == "input":
    print["Correct!"]
else:
    print[f"The answer is 'input', not {answer!r}"]
9. Điều này được nhìn thấy trong ví dụ lấy lại, vì chuỗi
[
    ["When was the first known use of the word 'quiz'", "1781"],
    ["Which built-in function can get information from the user", "input"],
]
3 đã được gán cho
[
    ["When was the first known use of the word 'quiz'", "1781"],
    ["Which built-in function can get information from the user", "input"],
]
4.Enter. Whatever the user enters is returned from
# quiz.py

answer = input["When was the first known use of the word 'quiz'? "]
if answer == "1781":
    print["Correct!"]
else:
    print[f"The answer is '1781', not {answer!r}"]

answer = input["Which built-in function can get information from the user? "]
if answer == "input":
    print["Correct!"]
else:
    print[f"The answer is 'input', not {answer!r}"]
9. This is seen in the REPL example, as the string
[
    ["When was the first known use of the word 'quiz'", "1781"],
    ["Which built-in function can get information from the user", "input"],
]
3 has been assigned to
[
    ["When was the first known use of the word 'quiz'", "1781"],
    ["Which built-in function can get information from the user", "input"],
]
4.

Bạn có thể sử dụng

# quiz.py

answer = input["When was the first known use of the word 'quiz'? "]
if answer == "1781":
    print["Correct!"]
else:
    print[f"The answer is '1781', not {answer!r}"]

answer = input["Which built-in function can get information from the user? "]
if answer == "input":
    print["Correct!"]
else:
    print[f"The answer is 'input', not {answer!r}"]
9 để có Python hỏi bạn câu hỏi và kiểm tra câu trả lời của bạn. Hãy thử những điều sau:

>>>

>>> answer = input["When was the first known use of the word 'quiz'? "]
When was the first known use of the word 'quiz'? 1781

>>> answer == 1781
False

>>> answer == "1781"
True

# quiz.py

answer = input["When was the first known use of the word 'quiz'? "]
if answer == "1781":
    print["Correct!"]
else:
    print[f"The answer is '1781', not {answer!r}"]

answer = input["Which built-in function can get information from the user? "]
if answer == "input":
    print["Correct!"]
else:
    print[f"The answer is 'input', not {answer!r}"]
9 lấy một lời nhắc tùy chọn mà LỚN hiển thị cho người dùng trước khi người dùng nhập thông tin. Trong ví dụ trên, lời nhắc được hiển thị trong dòng được tô sáng và người dùng nhập
[
    ["When was the first known use of the word 'quiz'", "1781"],
    ["Which built-in function can get information from the user", "input"],
]
1 trước khi nhấn enter. Bất cứ điều gì người dùng nhập đều được trả về từ
# quiz.py

answer = input["When was the first known use of the word 'quiz'? "]
if answer == "1781":
    print["Correct!"]
else:
    print[f"The answer is '1781', not {answer!r}"]

answer = input["Which built-in function can get information from the user? "]
if answer == "input":
    print["Correct!"]
else:
    print[f"The answer is 'input', not {answer!r}"]
9. Điều này được nhìn thấy trong ví dụ lấy lại, vì chuỗi
[
    ["When was the first known use of the word 'quiz'", "1781"],
    ["Which built-in function can get information from the user", "input"],
]
3 đã được gán cho
[
    ["When was the first known use of the word 'quiz'", "1781"],
    ["Which built-in function can get information from the user", "input"],
]
4.

Bạn có thể sử dụng

# quiz.py

answer = input["When was the first known use of the word 'quiz'? "]
if answer == "1781":
    print["Correct!"]
else:
    print[f"The answer is '1781', not {answer!r}"]

answer = input["Which built-in function can get information from the user? "]
if answer == "input":
    print["Correct!"]
else:
    print[f"The answer is 'input', not {answer!r}"]
9 để có Python hỏi bạn câu hỏi và kiểm tra câu trả lời của bạn. Hãy thử những điều sau:

# quiz.py

answer = input["When was the first known use of the word 'quiz'? "]
if answer == "1781":
    print["Correct!"]
else:
    print[f"The answer is '1781', not {answer!r}"]

Ví dụ này cho thấy một điều mà bạn cần nhận thức được:

# quiz.py

answer = input["When was the first known use of the word 'quiz'? "]
if answer == "1781":
    print["Correct!"]
else:
    print[f"The answer is '1781', not {answer!r}"]

answer = input["Which built-in function can get information from the user? "]
if answer == "input":
    print["Correct!"]
else:
    print[f"The answer is 'input', not {answer!r}"]
9 luôn trả về một chuỗi văn bản, ngay cả khi chuỗi đó chỉ chứa các chữ số. Như bạn sẽ sớm thấy, điều này đã giành được một vấn đề cho ứng dụng đố. Tuy nhiên, nếu bạn muốn sử dụng kết quả của
# quiz.py

answer = input["When was the first known use of the word 'quiz'? "]
if answer == "1781":
    print["Correct!"]
else:
    print[f"The answer is '1781', not {answer!r}"]

answer = input["Which built-in function can get information from the user? "]
if answer == "input":
    print["Correct!"]
else:
    print[f"The answer is 'input', not {answer!r}"]
9 để tính toán toán học, thì bạn sẽ cần phải chuyển đổi nó trước.

$ python quiz.py
When was the first known use of the word 'quiz'? 1871
The answer is '1781', not '1871'

Thời gian để bắt đầu xây dựng ứng dụng đố của bạn. Mở trình chỉnh sửa của bạn và tạo tệp

[
    ["When was the first known use of the word 'quiz'", "1781"],
    ["Which built-in function can get information from the user", "input"],
]
8 với nội dung sau:

Mã này rất giống với những gì bạn đã thử nghiệm trong bản phát hành ở trên. Bạn có thể chạy ứng dụng của mình để kiểm tra kiến ​​thức của bạn:

# quiz.py

answer = input["When was the first known use of the word 'quiz'? "]
if answer == "1781":
    print["Correct!"]
else:
    print[f"The answer is '1781', not {answer!r}"]

answer = input["Which built-in function can get information from the user? "]
if answer == "input":
    print["Correct!"]
else:
    print[f"The answer is 'input', not {answer!r}"]

Nếu bạn tình cờ đưa ra câu trả lời sai, thì bạn sẽ được sửa chữa nhẹ nhàng để bạn hy vọng sẽ làm tốt hơn vào lần tới.

$ python quiz.py
When was the first known use of the word 'quiz'? 1781
Correct!
Which built-in function can get information from the user? get
The answer is 'input', not 'get'

Một bài kiểm tra chỉ có một câu hỏi là rất thú vị! Bạn có thể hỏi một câu hỏi khác bằng cách lặp lại mã của bạn:Don’t Repeat Yourself [DRY], which says that you should usually avoid repeated code because it gets hard to maintain.

Bạn đã thêm một câu hỏi bằng cách sao chép và dán mã trước đó thay đổi văn bản câu hỏi và câu trả lời đúng. Một lần nữa, bạn có thể kiểm tra điều này bằng cách chạy tập lệnh:

Nó hoạt động! Tuy nhiên, sao chép và dán mã như thế này không phải là tuyệt vời. Có một nguyên tắc lập trình có tên là Don Lặp lại bản thân [khô], nói rằng bạn thường nên tránh mã lặp đi lặp lại vì nó khó duy trì.

Tiếp theo, bạn sẽ bắt đầu cải thiện mã của mình để làm cho việc làm việc dễ dàng hơn.

Sử dụng danh sách và bộ dữ liệu để tránh mã lặp đi lặp lại

Python cung cấp một số cấu trúc dữ liệu linh hoạt và mạnh mẽ. Bạn thường có thể thay thế mã lặp lại bằng một tuple, danh sách hoặc từ điển kết hợp với vòng lặp

$ python quiz.py
When was the first known use of the word 'quiz'? 1781
Correct!
Which built-in function can get information from the user? get
The answer is 'input', not 'get'
5 hoặc vòng lặp
$ python quiz.py
When was the first known use of the word 'quiz'? 1781
Correct!
Which built-in function can get information from the user? get
The answer is 'input', not 'get'
6.

Thay vì lặp lại mã, bạn sẽ coi các câu hỏi và câu trả lời của mình là dữ liệu và chuyển chúng vào cấu trúc dữ liệu mà mã của bạn có thể lặp lại. Các câu hỏi trực tiếp ngay lập tức và thường xuyên thách thức câu hỏi của bạn sau đó trở thành cách bạn nên cấu trúc dữ liệu của mình.

  • Có không bao giờ một cấu trúc dữ liệu hoàn hảo duy nhất. Bạn thường chọn giữa một số lựa chọn thay thế. Trong suốt hướng dẫn này, bạn sẽ xem lại sự lựa chọn cấu trúc dữ liệu của bạn nhiều lần khi ứng dụng của bạn phát triển.
  • Hiện tại, chọn một cấu trúc dữ liệu khá đơn giản:

Một danh sách sẽ giữ một số yếu tố câu hỏi.

[
    ["When was the first known use of the word 'quiz'", "1781"],
    ["Which built-in function can get information from the user", "input"],
]

Mỗi yếu tố câu hỏi sẽ là một bộ hai bao gồm văn bản câu hỏi và câu trả lời.

Sau đó, bạn có thể lưu trữ các câu hỏi của bạn như sau:

Điều này phù hợp với cách bạn muốn sử dụng dữ liệu của mình. Bạn sẽ lặp lại từng câu hỏi và đối với mỗi câu hỏi, bạn muốn truy cập vào cả câu hỏi và câu trả lời.

Thay đổi tệp

[
    ["When was the first known use of the word 'quiz'", "1781"],
    ["Which built-in function can get information from the user", "input"],
]
8 của bạn để bạn lưu trữ câu hỏi và câu trả lời của mình trong cấu trúc dữ liệu
# quiz.py

QUESTIONS = [
    ["When was the first known use of the word 'quiz'", "1781"],
    ["Which built-in function can get information from the user", "input"],
    ["Which keyword do you use to loop over a given list of elements", "for"]
]

for question, correct_answer in QUESTIONS:
    answer = input[f"{question}? "]
    if answer == correct_answer:
        print["Correct!"]
    else:
        print[f"The answer is {correct_answer!r}, not {answer!r}"]
2:

# quiz.py

QUESTIONS = [
    ["When was the first known use of the word 'quiz'", "1781"],
    ["Which built-in function can get information from the user", "input"],
    ["Which keyword do you use to loop over a given list of elements", "for"]
]

for question, correct_answer in QUESTIONS:
    answer = input[f"{question}? "]
    if answer == correct_answer:
        print["Correct!"]
    else:
        print[f"The answer is {correct_answer!r}, not {answer!r}"]

Tiếp theo, bạn sẽ làm cho ứng dụng đố của mình dễ sử dụng hơn bằng cách thêm các giải pháp thay thế câu trả lời cho mỗi câu hỏi.

Cung cấp nhiều lựa chọn

Sử dụng

# quiz.py

answer = input["When was the first known use of the word 'quiz'? "]
if answer == "1781":
    print["Correct!"]
else:
    print[f"The answer is '1781', not {answer!r}"]

answer = input["Which built-in function can get information from the user? "]
if answer == "input":
    print["Correct!"]
else:
    print[f"The answer is 'input', not {answer!r}"]
9 là một cách tuyệt vời để đọc đầu vào từ người dùng của bạn. Tuy nhiên, cách mà bạn hiện đang sử dụng nó có thể trở nên bực bội. Ví dụ, ai đó có thể trả lời một trong những câu hỏi của bạn như thế này:

Wellcome to quiz game !!
NOTE: if your spelling is incorrect then it is considered as wrong answer
Do you want to play ? yes

1. what does CPU stand for? central processing unit
correct! you got 1 point

2. what does GPU stand for? graphics processing unit
correct! you got 1 point

3. what does RAM stand for? RAM
Incorrect!
current answer is --> random access memory

4. what does PSU stand for? power supplyy unit
Incorrect!
current answer is --> power supply unit

5. what does ROM stand for? read only memory
correct! you got 1 point

number of question is 5
your score is 3
60.0% questions are correct.
0

Chúng có nên thực sự bị đánh dấu sai vì chúng bao gồm các dấu ngoặc đơn để chỉ ra rằng chức năng có thể gọi được? Bạn có thể lấy đi rất nhiều phỏng đoán cho người dùng bằng cách cung cấp cho họ các lựa chọn thay thế. Ví dụ:

Wellcome to quiz game !!
NOTE: if your spelling is incorrect then it is considered as wrong answer
Do you want to play ? yes

1. what does CPU stand for? central processing unit
correct! you got 1 point

2. what does GPU stand for? graphics processing unit
correct! you got 1 point

3. what does RAM stand for? RAM
Incorrect!
current answer is --> random access memory

4. what does PSU stand for? power supplyy unit
Incorrect!
current answer is --> power supply unit

5. what does ROM stand for? read only memory
correct! you got 1 point

number of question is 5
your score is 3
60.0% questions are correct.
1

Ở đây, các lựa chọn thay thế cho thấy bạn mong đợi câu trả lời được nhập mà không có dấu ngoặc đơn. Trong ví dụ, các lựa chọn thay thế được liệt kê trước câu hỏi. Đây là một chút phản trực giác, nhưng nó dễ dàng thực hiện vào mã hiện tại của bạn. Bạn sẽ cải thiện điều này trong bước tiếp theo.

Để thực hiện các giải pháp thay thế câu trả lời, bạn cần cấu trúc dữ liệu của mình để có thể ghi lại ba phần thông tin cho mỗi câu hỏi:

  1. Văn bản câu hỏi
  2. Câu trả lời đúng
  3. Trả lời các lựa chọn thay thế

Đó là thời gian để xem lại

# quiz.py

QUESTIONS = [
    ["When was the first known use of the word 'quiz'", "1781"],
    ["Which built-in function can get information from the user", "input"],
    ["Which keyword do you use to loop over a given list of elements", "for"]
]

for question, correct_answer in QUESTIONS:
    answer = input[f"{question}? "]
    if answer == correct_answer:
        print["Correct!"]
    else:
        print[f"The answer is {correct_answer!r}, not {answer!r}"]
2 cho lần đầu tiên nhưng không phải là thời gian cuối cùng và thực hiện một số thay đổi đối với nó. Thật hợp lý khi lưu trữ các giải pháp thay thế câu trả lời trong danh sách, vì có thể có bất kỳ số lượng nào trong số chúng và bạn chỉ muốn hiển thị chúng lên màn hình. Hơn nữa, bạn có thể coi câu trả lời chính xác là một trong những lựa chọn thay thế câu trả lời và đưa nó vào danh sách, miễn là bạn có thể lấy nó sau.

Bạn quyết định thay đổi

# quiz.py

QUESTIONS = [
    ["When was the first known use of the word 'quiz'", "1781"],
    ["Which built-in function can get information from the user", "input"],
    ["Which keyword do you use to loop over a given list of elements", "for"]
]

for question, correct_answer in QUESTIONS:
    answer = input[f"{question}? "]
    if answer == correct_answer:
        print["Correct!"]
    else:
        print[f"The answer is {correct_answer!r}, not {answer!r}"]
2 thành một từ điển trong đó các khóa là câu hỏi của bạn và các giá trị là danh sách các giải pháp thay thế trả lời. Bạn liên tục đặt câu trả lời đúng là mục đầu tiên trong danh sách các lựa chọn thay thế để bạn có thể xác định nó.

Bạn cập nhật mã của mình để lặp qua từng mục trong từ điển mới được đúc của bạn. Đối với mỗi câu hỏi, bạn chọn ra câu trả lời đúng từ các lựa chọn thay thế và bạn in ra tất cả các lựa chọn thay thế trước khi đặt câu hỏi:

Wellcome to quiz game !!
NOTE: if your spelling is incorrect then it is considered as wrong answer
Do you want to play ? yes

1. what does CPU stand for? central processing unit
correct! you got 1 point

2. what does GPU stand for? graphics processing unit
correct! you got 1 point

3. what does RAM stand for? RAM
Incorrect!
current answer is --> random access memory

4. what does PSU stand for? power supplyy unit
Incorrect!
current answer is --> power supply unit

5. what does ROM stand for? read only memory
correct! you got 1 point

number of question is 5
your score is 3
60.0% questions are correct.
2

Nếu bạn luôn hiển thị câu trả lời đúng là sự thay thế đầu tiên, thì người dùng của bạn sẽ sớm bắt kịp và có thể đoán câu trả lời đúng mỗi lần. Thay vào đó, bạn thay đổi thứ tự của các lựa chọn thay thế bằng cách sắp xếp chúng. Kiểm tra ứng dụng của bạn:

Wellcome to quiz game !!
NOTE: if your spelling is incorrect then it is considered as wrong answer
Do you want to play ? yes

1. what does CPU stand for? central processing unit
correct! you got 1 point

2. what does GPU stand for? graphics processing unit
correct! you got 1 point

3. what does RAM stand for? RAM
Incorrect!
current answer is --> random access memory

4. what does PSU stand for? power supplyy unit
Incorrect!
current answer is --> power supply unit

5. what does ROM stand for? read only memory
correct! you got 1 point

number of question is 5
your score is 3
60.0% questions are correct.
3

Câu hỏi cuối cùng cho thấy một trải nghiệm khác có thể gây khó chịu cho người dùng. Trong ví dụ này, họ đã chọn giải pháp thay thế chính xác. Tuy nhiên, khi họ đang gõ nó, một lỗi đánh máy đã lẻn vào. Bạn có thể làm cho ứng dụng của bạn tha thứ hơn không?

Bạn biết rằng người dùng sẽ trả lời với một trong những lựa chọn thay thế, vì vậy bạn chỉ cần một cách để họ giao tiếp giải pháp thay thế nào họ chọn. Bạn có thể thêm một nhãn vào mỗi thay thế và chỉ yêu cầu người dùng nhập nhãn.

Cập nhật ứng dụng để sử dụng

# quiz.py

QUESTIONS = [
    ["When was the first known use of the word 'quiz'", "1781"],
    ["Which built-in function can get information from the user", "input"],
    ["Which keyword do you use to loop over a given list of elements", "for"]
]

for question, correct_answer in QUESTIONS:
    answer = input[f"{question}? "]
    if answer == correct_answer:
        print["Correct!"]
    else:
        print[f"The answer is {correct_answer!r}, not {answer!r}"]
7 để in chỉ mục của từng câu trả lời thay thế:

Wellcome to quiz game !!
NOTE: if your spelling is incorrect then it is considered as wrong answer
Do you want to play ? yes

1. what does CPU stand for? central processing unit
correct! you got 1 point

2. what does GPU stand for? graphics processing unit
correct! you got 1 point

3. what does RAM stand for? RAM
Incorrect!
current answer is --> random access memory

4. what does PSU stand for? power supplyy unit
Incorrect!
current answer is --> power supply unit

5. what does ROM stand for? read only memory
correct! you got 1 point

number of question is 5
your score is 3
60.0% questions are correct.
4

Bạn lưu trữ các lựa chọn thay thế được sắp xếp lại là

# quiz.py

QUESTIONS = [
    ["When was the first known use of the word 'quiz'", "1781"],
    ["Which built-in function can get information from the user", "input"],
    ["Which keyword do you use to loop over a given list of elements", "for"]
]

for question, correct_answer in QUESTIONS:
    answer = input[f"{question}? "]
    if answer == correct_answer:
        print["Correct!"]
    else:
        print[f"The answer is {correct_answer!r}, not {answer!r}"]
8 để bạn có thể tra cứu câu trả lời đầy đủ dựa trên nhãn câu trả lời mà người dùng nhập. Hãy nhớ lại rằng
# quiz.py

answer = input["When was the first known use of the word 'quiz'? "]
if answer == "1781":
    print["Correct!"]
else:
    print[f"The answer is '1781', not {answer!r}"]

answer = input["Which built-in function can get information from the user? "]
if answer == "input":
    print["Correct!"]
else:
    print[f"The answer is 'input', not {answer!r}"]
9 luôn trả về một chuỗi, vì vậy bạn cần chuyển đổi nó thành một số nguyên trước khi bạn coi nó như một chỉ mục danh sách.

Bây giờ, nó rất thuận tiện để trả lời các câu hỏi:

Wellcome to quiz game !!
NOTE: if your spelling is incorrect then it is considered as wrong answer
Do you want to play ? yes

1. what does CPU stand for? central processing unit
correct! you got 1 point

2. what does GPU stand for? graphics processing unit
correct! you got 1 point

3. what does RAM stand for? RAM
Incorrect!
current answer is --> random access memory

4. what does PSU stand for? power supplyy unit
Incorrect!
current answer is --> power supply unit

5. what does ROM stand for? read only memory
correct! you got 1 point

number of question is 5
your score is 3
60.0% questions are correct.
5

Tuyệt quá! Bạn đã tạo ra một ứng dụng đố khá có khả năng! Trong bước tiếp theo, bạn đã giành được thêm bất kỳ chức năng nào. Thay vào đó, bạn sẽ làm cho ứng dụng của bạn thân thiện hơn với người dùng.

Bước 2: Làm cho ứng dụng của bạn thân thiện với người dùng

Trong bước thứ hai này, bạn sẽ cải thiện ứng dụng đố của mình để giúp sử dụng dễ dàng hơn. Cụ thể, bạn sẽ cải thiện những điều sau:

  • Ứng dụng trông như thế nào và cảm giác
  • Cách bạn tóm tắt kết quả của người dùng
  • Điều gì xảy ra nếu người dùng của bạn nhập một giải pháp thay thế không tồn tại
  • Thứ tự bạn trình bày các câu hỏi và giải pháp thay thế trong

Vào cuối bước này, ứng dụng của bạn sẽ hoạt động như sau:

Chương trình của bạn vẫn sẽ hoạt động tương tự như bây giờ, nhưng nó sẽ mạnh mẽ và hấp dẫn hơn. Bạn có thể tìm thấy mã nguồn khi nó sẽ nhìn vào phần cuối của bước này trong thư mục

Wellcome to quiz game !!
NOTE: if your spelling is incorrect then it is considered as wrong answer
Do you want to play ? yes

1. what does CPU stand for? central processing unit
correct! you got 1 point

2. what does GPU stand for? graphics processing unit
correct! you got 1 point

3. what does RAM stand for? RAM
Incorrect!
current answer is --> random access memory

4. what does PSU stand for? power supplyy unit
Incorrect!
current answer is --> power supply unit

5. what does ROM stand for? read only memory
correct! you got 1 point

number of question is 5
your score is 3
60.0% questions are correct.
00 bằng cách nhấp vào bên dưới:

Định dạng đầu ra độc đáo hơn

Nhìn lại cách ứng dụng đố của bạn hiện đang được trình bày. Nó không hấp dẫn lắm. Không có dòng trống nào cho bạn biết nơi một câu hỏi mới bắt đầu và các lựa chọn thay thế được liệt kê trên câu hỏi, điều này hơi khó hiểu. Hơn nữa, việc đánh số các lựa chọn khác nhau bắt đầu tại

Wellcome to quiz game !!
NOTE: if your spelling is incorrect then it is considered as wrong answer
Do you want to play ? yes

1. what does CPU stand for? central processing unit
correct! you got 1 point

2. what does GPU stand for? graphics processing unit
correct! you got 1 point

3. what does RAM stand for? RAM
Incorrect!
current answer is --> random access memory

4. what does PSU stand for? power supplyy unit
Incorrect!
current answer is --> power supply unit

5. what does ROM stand for? read only memory
correct! you got 1 point

number of question is 5
your score is 3
60.0% questions are correct.
01 thay vì
Wellcome to quiz game !!
NOTE: if your spelling is incorrect then it is considered as wrong answer
Do you want to play ? yes

1. what does CPU stand for? central processing unit
correct! you got 1 point

2. what does GPU stand for? graphics processing unit
correct! you got 1 point

3. what does RAM stand for? RAM
Incorrect!
current answer is --> random access memory

4. what does PSU stand for? power supplyy unit
Incorrect!
current answer is --> power supply unit

5. what does ROM stand for? read only memory
correct! you got 1 point

number of question is 5
your score is 3
60.0% questions are correct.
02, điều này sẽ tự nhiên hơn.

Trong bản cập nhật tiếp theo của bạn lên

[
    ["When was the first known use of the word 'quiz'", "1781"],
    ["Which built-in function can get information from the user", "input"],
]
8, bạn sẽ tự đánh số các câu hỏi và trình bày văn bản câu hỏi trên các giải pháp thay thế câu trả lời. Ngoài ra, bạn sẽ sử dụng các chữ cái viết thường thay vì các số để xác định câu trả lời:

Wellcome to quiz game !!
NOTE: if your spelling is incorrect then it is considered as wrong answer
Do you want to play ? yes

1. what does CPU stand for? central processing unit
correct! you got 1 point

2. what does GPU stand for? graphics processing unit
correct! you got 1 point

3. what does RAM stand for? RAM
Incorrect!
current answer is --> random access memory

4. what does PSU stand for? power supplyy unit
Incorrect!
current answer is --> power supply unit

5. what does ROM stand for? read only memory
correct! you got 1 point

number of question is 5
your score is 3
60.0% questions are correct.
6

Bạn sử dụng

Wellcome to quiz game !!
NOTE: if your spelling is incorrect then it is considered as wrong answer
Do you want to play ? yes

1. what does CPU stand for? central processing unit
correct! you got 1 point

2. what does GPU stand for? graphics processing unit
correct! you got 1 point

3. what does RAM stand for? RAM
Incorrect!
current answer is --> random access memory

4. what does PSU stand for? power supplyy unit
Incorrect!
current answer is --> power supply unit

5. what does ROM stand for? read only memory
correct! you got 1 point

number of question is 5
your score is 3
60.0% questions are correct.
04 để nhận các chữ cái dán nhãn câu trả lời thay thế của bạn. Bạn kết hợp các chữ cái và các lựa chọn thay thế với
Wellcome to quiz game !!
NOTE: if your spelling is incorrect then it is considered as wrong answer
Do you want to play ? yes

1. what does CPU stand for? central processing unit
correct! you got 1 point

2. what does GPU stand for? graphics processing unit
correct! you got 1 point

3. what does RAM stand for? RAM
Incorrect!
current answer is --> random access memory

4. what does PSU stand for? power supplyy unit
Incorrect!
current answer is --> power supply unit

5. what does ROM stand for? read only memory
correct! you got 1 point

number of question is 5
your score is 3
60.0% questions are correct.
05 và lưu trữ chúng trong một từ điển như sau:

>>>

Wellcome to quiz game !!
NOTE: if your spelling is incorrect then it is considered as wrong answer
Do you want to play ? yes

1. what does CPU stand for? central processing unit
correct! you got 1 point

2. what does GPU stand for? graphics processing unit
correct! you got 1 point

3. what does RAM stand for? RAM
Incorrect!
current answer is --> random access memory

4. what does PSU stand for? power supplyy unit
Incorrect!
current answer is --> power supply unit

5. what does ROM stand for? read only memory
correct! you got 1 point

number of question is 5
your score is 3
60.0% questions are correct.
7

Bạn sử dụng các lựa chọn thay thế được dán nhãn này khi bạn hiển thị các tùy chọn cho người dùng và khi bạn tìm kiếm câu trả lời của người dùng dựa trên nhãn mà họ đã nhập. Lưu ý việc sử dụng chuỗi thoát đặc biệt

Wellcome to quiz game !!
NOTE: if your spelling is incorrect then it is considered as wrong answer
Do you want to play ? yes

1. what does CPU stand for? central processing unit
correct! you got 1 point

2. what does GPU stand for? graphics processing unit
correct! you got 1 point

3. what does RAM stand for? RAM
Incorrect!
current answer is --> random access memory

4. what does PSU stand for? power supplyy unit
Incorrect!
current answer is --> power supply unit

5. what does ROM stand for? read only memory
correct! you got 1 point

number of question is 5
your score is 3
60.0% questions are correct.
06. Điều này được hiểu là một dòng mới và thêm một dòng trống trên màn hình. Đây là một cách đơn giản để thêm một số tổ chức vào đầu ra của bạn:

Wellcome to quiz game !!
NOTE: if your spelling is incorrect then it is considered as wrong answer
Do you want to play ? yes

1. what does CPU stand for? central processing unit
correct! you got 1 point

2. what does GPU stand for? graphics processing unit
correct! you got 1 point

3. what does RAM stand for? RAM
Incorrect!
current answer is --> random access memory

4. what does PSU stand for? power supplyy unit
Incorrect!
current answer is --> power supply unit

5. what does ROM stand for? read only memory
correct! you got 1 point

number of question is 5
your score is 3
60.0% questions are correct.
8

Đầu ra của bạn vẫn chủ yếu là đơn sắc trong thiết bị đầu cuối, nhưng nó rất dễ chịu hơn, và nó dễ đọc hơn.

Giữ điểm số

Bây giờ bạn đã đánh số các câu hỏi, thật tuyệt khi theo dõi chính xác có bao nhiêu câu hỏi mà người dùng trả lời. Bạn có thể thêm một biến,

Wellcome to quiz game !!
NOTE: if your spelling is incorrect then it is considered as wrong answer
Do you want to play ? yes

1. what does CPU stand for? central processing unit
correct! you got 1 point

2. what does GPU stand for? graphics processing unit
correct! you got 1 point

3. what does RAM stand for? RAM
Incorrect!
current answer is --> random access memory

4. what does PSU stand for? power supplyy unit
Incorrect!
current answer is --> power supply unit

5. what does ROM stand for? read only memory
correct! you got 1 point

number of question is 5
your score is 3
60.0% questions are correct.
07, để chăm sóc điều này:

Wellcome to quiz game !!
NOTE: if your spelling is incorrect then it is considered as wrong answer
Do you want to play ? yes

1. what does CPU stand for? central processing unit
correct! you got 1 point

2. what does GPU stand for? graphics processing unit
correct! you got 1 point

3. what does RAM stand for? RAM
Incorrect!
current answer is --> random access memory

4. what does PSU stand for? power supplyy unit
Incorrect!
current answer is --> power supply unit

5. what does ROM stand for? read only memory
correct! you got 1 point

number of question is 5
your score is 3
60.0% questions are correct.
9

Bạn tăng

Wellcome to quiz game !!
NOTE: if your spelling is incorrect then it is considered as wrong answer
Do you want to play ? yes

1. what does CPU stand for? central processing unit
correct! you got 1 point

2. what does GPU stand for? graphics processing unit
correct! you got 1 point

3. what does RAM stand for? RAM
Incorrect!
current answer is --> random access memory

4. what does PSU stand for? power supplyy unit
Incorrect!
current answer is --> power supply unit

5. what does ROM stand for? read only memory
correct! you got 1 point

number of question is 5
your score is 3
60.0% questions are correct.
07 cho mỗi câu trả lời đúng. Biến vòng
Wellcome to quiz game !!
NOTE: if your spelling is incorrect then it is considered as wrong answer
Do you want to play ? yes

1. what does CPU stand for? central processing unit
correct! you got 1 point

2. what does GPU stand for? graphics processing unit
correct! you got 1 point

3. what does RAM stand for? RAM
Incorrect!
current answer is --> random access memory

4. what does PSU stand for? power supplyy unit
Incorrect!
current answer is --> power supply unit

5. what does ROM stand for? read only memory
correct! you got 1 point

number of question is 5
your score is 3
60.0% questions are correct.
09 đã tính tổng số câu hỏi, vì vậy bạn có thể sử dụng điều đó để báo cáo kết quả của người dùng.

Xử lý lỗi người dùng

Cho đến nay, bạn đã lo lắng quá nhiều về những gì xảy ra nếu người dùng nhập câu trả lời mà không hợp lệ. Trong các phiên bản khác nhau của ứng dụng của bạn, sự giám sát này có thể dẫn đến việc chương trình gây ra lỗi hoặc không có gì đáng kể khi đăng ký một câu trả lời không hợp lệ của người dùng là sai.

Bạn có thể xử lý các lỗi người dùng theo cách tốt hơn bằng cách cho phép người dùng nhập lại câu trả lời của họ khi họ nhập một cái gì đó không hợp lệ. Một cách để làm điều này là quấn

# quiz.py

answer = input["When was the first known use of the word 'quiz'? "]
if answer == "1781":
    print["Correct!"]
else:
    print[f"The answer is '1781', not {answer!r}"]

answer = input["Which built-in function can get information from the user? "]
if answer == "input":
    print["Correct!"]
else:
    print[f"The answer is 'input', not {answer!r}"]
9 trong vòng lặp
$ python quiz.py
When was the first known use of the word 'quiz'? 1781
Correct!
Which built-in function can get information from the user? get
The answer is 'input', not 'get'
6:

>>>

>>> name = input["What's your name? "]
What's your name? Geir Arne

>>> name
'Geir Arne'
0

Điều kiện

Wellcome to quiz game !!
NOTE: if your spelling is incorrect then it is considered as wrong answer
Do you want to play ? yes

1. what does CPU stand for? central processing unit
correct! you got 1 point

2. what does GPU stand for? graphics processing unit
correct! you got 1 point

3. what does RAM stand for? RAM
Incorrect!
current answer is --> random access memory

4. what does PSU stand for? power supplyy unit
Incorrect!
current answer is --> power supply unit

5. what does ROM stand for? read only memory
correct! you got 1 point

number of question is 5
your score is 3
60.0% questions are correct.
12 thực hiện một vài điều cùng một lúc. Nó sử dụng biểu thức ASSIGMENT [
Wellcome to quiz game !!
NOTE: if your spelling is incorrect then it is considered as wrong answer
Do you want to play ? yes

1. what does CPU stand for? central processing unit
correct! you got 1 point

2. what does GPU stand for? graphics processing unit
correct! you got 1 point

3. what does RAM stand for? RAM
Incorrect!
current answer is --> random access memory

4. what does PSU stand for? power supplyy unit
Incorrect!
current answer is --> power supply unit

5. what does ROM stand for? read only memory
correct! you got 1 point

number of question is 5
your score is 3
60.0% questions are correct.
13], thường được gọi là toán tử Walrus, để lưu trữ đầu vào của người dùng là
Wellcome to quiz game !!
NOTE: if your spelling is incorrect then it is considered as wrong answer
Do you want to play ? yes

1. what does CPU stand for? central processing unit
correct! you got 1 point

2. what does GPU stand for? graphics processing unit
correct! you got 1 point

3. what does RAM stand for? RAM
Incorrect!
current answer is --> random access memory

4. what does PSU stand for? power supplyy unit
Incorrect!
current answer is --> power supply unit

5. what does ROM stand for? read only memory
correct! you got 1 point

number of question is 5
your score is 3
60.0% questions are correct.
14 và so sánh nó với chuỗi
Wellcome to quiz game !!
NOTE: if your spelling is incorrect then it is considered as wrong answer
Do you want to play ? yes

1. what does CPU stand for? central processing unit
correct! you got 1 point

2. what does GPU stand for? graphics processing unit
correct! you got 1 point

3. what does RAM stand for? RAM
Incorrect!
current answer is --> random access memory

4. what does PSU stand for? power supplyy unit
Incorrect!
current answer is --> power supply unit

5. what does ROM stand for? read only memory
correct! you got 1 point

number of question is 5
your score is 3
60.0% questions are correct.
15. Vòng lặp trong khi sẽ chạy cho đến khi bạn nhập
Wellcome to quiz game !!
NOTE: if your spelling is incorrect then it is considered as wrong answer
Do you want to play ? yes

1. what does CPU stand for? central processing unit
correct! you got 1 point

2. what does GPU stand for? graphics processing unit
correct! you got 1 point

3. what does RAM stand for? RAM
Incorrect!
current answer is --> random access memory

4. what does PSU stand for? power supplyy unit
Incorrect!
current answer is --> power supply unit

5. what does ROM stand for? read only memory
correct! you got 1 point

number of question is 5
your score is 3
60.0% questions are correct.
16 tại dấu nhắc. Xem toán tử Walrus: Biểu thức gán Python 3.8 để biết thêm các ví dụ.

Trong ứng dụng đố của bạn, bạn sử dụng cấu trúc tương tự để lặp cho đến khi người dùng đưa ra câu trả lời hợp lệ:

>>> name = input["What's your name? "]
What's your name? Geir Arne

>>> name
'Geir Arne'
1

Nếu bạn nhập một lựa chọn không hợp lệ tại dấu nhắc, thì bạn sẽ được nhắc nhở về các lựa chọn hợp lệ của bạn:

>>> name = input["What's your name? "]
What's your name? Geir Arne

>>> name
'Geir Arne'
2

Lưu ý rằng một khi các vòng lặp

$ python quiz.py
When was the first known use of the word 'quiz'? 1781
Correct!
Which built-in function can get information from the user? get
The answer is 'input', not 'get'
6 thoát ra, bạn đã đảm bảo rằng
Wellcome to quiz game !!
NOTE: if your spelling is incorrect then it is considered as wrong answer
Do you want to play ? yes

1. what does CPU stand for? central processing unit
correct! you got 1 point

2. what does GPU stand for? graphics processing unit
correct! you got 1 point

3. what does RAM stand for? RAM
Incorrect!
current answer is --> random access memory

4. what does PSU stand for? power supplyy unit
Incorrect!
current answer is --> power supply unit

5. what does ROM stand for? read only memory
correct! you got 1 point

number of question is 5
your score is 3
60.0% questions are correct.
18 là một trong những chìa khóa trong
Wellcome to quiz game !!
NOTE: if your spelling is incorrect then it is considered as wrong answer
Do you want to play ? yes

1. what does CPU stand for? central processing unit
correct! you got 1 point

2. what does GPU stand for? graphics processing unit
correct! you got 1 point

3. what does RAM stand for? RAM
Incorrect!
current answer is --> random access memory

4. what does PSU stand for? power supplyy unit
Incorrect!
current answer is --> power supply unit

5. what does ROM stand for? read only memory
correct! you got 1 point

number of question is 5
your score is 3
60.0% questions are correct.
19, do đó, nó an toàn để tìm kiếm trực tiếp
Wellcome to quiz game !!
NOTE: if your spelling is incorrect then it is considered as wrong answer
Do you want to play ? yes

1. what does CPU stand for? central processing unit
correct! you got 1 point

2. what does GPU stand for? graphics processing unit
correct! you got 1 point

3. what does RAM stand for? RAM
Incorrect!
current answer is --> random access memory

4. what does PSU stand for? power supplyy unit
Incorrect!
current answer is --> power supply unit

5. what does ROM stand for? read only memory
correct! you got 1 point

number of question is 5
your score is 3
60.0% questions are correct.
20. Tiếp theo, bạn sẽ thêm một cải tiến nữa bằng cách đưa một số ngẫu nhiên vào bài kiểm tra của bạn.

Thêm sự đa dạng vào bài kiểm tra của bạn

Hiện tại, khi bạn chạy ứng dụng đố vui của mình, bạn đã luôn hỏi các câu hỏi theo thứ tự giống như chúng được liệt kê trong mã nguồn của bạn. Ngoài ra, câu trả lời thay thế cho một câu hỏi nhất định cũng có một thứ tự cố định không bao giờ thay đổi.

Bạn có thể thêm một số loại vào bài kiểm tra của bạn bằng cách thay đổi mọi thứ một chút. Bạn có thể chọn ngẫu nhiên cả thứ tự của các câu hỏi và thứ tự của câu trả lời thay thế cho mỗi câu hỏi:

>>> name = input["What's your name? "]
What's your name? Geir Arne

>>> name
'Geir Arne'
3

Bạn sử dụng

Wellcome to quiz game !!
NOTE: if your spelling is incorrect then it is considered as wrong answer
Do you want to play ? yes

1. what does CPU stand for? central processing unit
correct! you got 1 point

2. what does GPU stand for? graphics processing unit
correct! you got 1 point

3. what does RAM stand for? RAM
Incorrect!
current answer is --> random access memory

4. what does PSU stand for? power supplyy unit
Incorrect!
current answer is --> power supply unit

5. what does ROM stand for? read only memory
correct! you got 1 point

number of question is 5
your score is 3
60.0% questions are correct.
21 để chọn ngẫu nhiên thứ tự câu hỏi của bạn và thứ tự của các giải pháp thay thế câu trả lời. Thông thường,
Wellcome to quiz game !!
NOTE: if your spelling is incorrect then it is considered as wrong answer
Do you want to play ? yes

1. what does CPU stand for? central processing unit
correct! you got 1 point

2. what does GPU stand for? graphics processing unit
correct! you got 1 point

3. what does RAM stand for? RAM
Incorrect!
current answer is --> random access memory

4. what does PSU stand for? power supplyy unit
Incorrect!
current answer is --> power supply unit

5. what does ROM stand for? read only memory
correct! you got 1 point

number of question is 5
your score is 3
60.0% questions are correct.
21 chọn ra một vài mẫu ngẫu nhiên từ một bộ sưu tập. Tuy nhiên, nếu bạn yêu cầu nhiều mẫu như có các mục trong chuỗi, thì bạn có thể sắp xếp lại một cách ngẫu nhiên toàn bộ chuỗi:

>>>

>>> name = input["What's your name? "]
What's your name? Geir Arne

>>> name
'Geir Arne'
4

Ngoài ra, bạn giới hạn số lượng câu hỏi trong bài kiểm tra đến

Wellcome to quiz game !!
NOTE: if your spelling is incorrect then it is considered as wrong answer
Do you want to play ? yes

1. what does CPU stand for? central processing unit
correct! you got 1 point

2. what does GPU stand for? graphics processing unit
correct! you got 1 point

3. what does RAM stand for? RAM
Incorrect!
current answer is --> random access memory

4. what does PSU stand for? power supplyy unit
Incorrect!
current answer is --> power supply unit

5. what does ROM stand for? read only memory
correct! you got 1 point

number of question is 5
your score is 3
60.0% questions are correct.
23 ban đầu được đặt thành năm. Nếu bạn bao gồm nhiều hơn năm câu hỏi trong ứng dụng của bạn, thì điều này cũng bổ sung một số câu hỏi nào được hỏi ngoài đơn đặt hàng mà họ đã hỏi.

Trong suốt bước này, bạn đã cải thiện ứng dụng đố vui của mình. Bây giờ, thời gian để lùi lại một bước và xem xét chính mã. Trong phần tiếp theo, bạn sẽ tổ chức lại mã để bạn giữ nó mô -đun và sẵn sàng để phát triển hơn nữa.

Bước 3: Tổ chức mã của bạn với các chức năng

Trong bước này, bạn sẽ tái cấu trúc mã của mình. Tái cấu trúc có nghĩa là bạn sẽ thay đổi mã của mình, nhưng hành vi ứng dụng của bạn và trải nghiệm người dùng của bạn sẽ ở lại như hiện tại. Điều này nghe có vẻ không thú vị lắm, nhưng nó sẽ rất hữu ích trong dòng, vì các chất tái cấu trúc tốt giúp việc duy trì và mở rộng mã của bạn thuận tiện hơn.refactor your code. Refactoring means that you’ll change your code, but your application’s behavior and your user’s experience will stay as they are. This may not sound very exciting, but it’ll be tremendously useful down the line, as good refactorings make it more convenient to maintain and expand your code.

Hiện tại, mã của bạn không được tổ chức đặc biệt. Tất cả các tuyên bố của bạn là cấp độ khá thấp. Bạn sẽ xác định các chức năng để cải thiện mã của bạn. Một vài lợi thế của họ là như sau:

  • Các chức năng Tên các hoạt động cấp cao hơn có thể giúp bạn có được cái nhìn tổng quan về mã của bạn.higher-level operations that can help you get an overview of your code.
  • Các chức năng có thể được tái sử dụng.reused.

Để xem mã sẽ chăm sóc bạn đã tái cấu trúc nó như thế nào, nhấp vào bên dưới và xem thư mục

Wellcome to quiz game !!
NOTE: if your spelling is incorrect then it is considered as wrong answer
Do you want to play ? yes

1. what does CPU stand for? central processing unit
correct! you got 1 point

2. what does GPU stand for? graphics processing unit
correct! you got 1 point

3. what does RAM stand for? RAM
Incorrect!
current answer is --> random access memory

4. what does PSU stand for? power supplyy unit
Incorrect!
current answer is --> power supply unit

5. what does ROM stand for? read only memory
correct! you got 1 point

number of question is 5
your score is 3
60.0% questions are correct.
24:

Chuẩn bị dữ liệu

Nhiều trò chơi và ứng dụng theo một vòng đời chung:

  1. Tiền xử lý: Chuẩn bị dữ liệu ban đầu. Prepare initial data.
  2. Quá trình: Chạy vòng chính. Run main loop.
  3. Post -Process: Dọn dẹp và đóng ứng dụng. Clean up and close application.

Trong ứng dụng đố vui của bạn, trước tiên bạn đọc các câu hỏi có sẵn, sau đó bạn hỏi từng câu hỏi, trước khi cuối cùng báo cáo điểm cuối cùng. Nếu bạn nhìn lại mã hiện tại của mình, thì bạn sẽ thấy ba bước này trong mã. Nhưng tổ chức vẫn còn một chút ẩn trong tất cả các chi tiết.

Bạn có thể làm cho chức năng chính rõ ràng hơn bằng cách gói gọn nó trong một chức năng. Bạn không cần phải cập nhật tệp

[
    ["When was the first known use of the word 'quiz'", "1781"],
    ["Which built-in function can get information from the user", "input"],
]
8 của mình, nhưng lưu ý rằng bạn có thể dịch đoạn trước thành mã trông như thế này:

>>> name = input["What's your name? "]
What's your name? Geir Arne

>>> name
'Geir Arne'
5

Mã này đã giành được chạy như nó là. Các chức năng

Wellcome to quiz game !!
NOTE: if your spelling is incorrect then it is considered as wrong answer
Do you want to play ? yes

1. what does CPU stand for? central processing unit
correct! you got 1 point

2. what does GPU stand for? graphics processing unit
correct! you got 1 point

3. what does RAM stand for? RAM
Incorrect!
current answer is --> random access memory

4. what does PSU stand for? power supplyy unit
Incorrect!
current answer is --> power supply unit

5. what does ROM stand for? read only memory
correct! you got 1 point

number of question is 5
your score is 3
60.0% questions are correct.
26 và
Wellcome to quiz game !!
NOTE: if your spelling is incorrect then it is considered as wrong answer
Do you want to play ? yes

1. what does CPU stand for? central processing unit
correct! you got 1 point

2. what does GPU stand for? graphics processing unit
correct! you got 1 point

3. what does RAM stand for? RAM
Incorrect!
current answer is --> random access memory

4. what does PSU stand for? power supplyy unit
Incorrect!
current answer is --> power supply unit

5. what does ROM stand for? read only memory
correct! you got 1 point

number of question is 5
your score is 3
60.0% questions are correct.
27 thiên đường đã được xác định, và có một số chi tiết khác bị thiếu. Tuy nhiên,
Wellcome to quiz game !!
NOTE: if your spelling is incorrect then it is considered as wrong answer
Do you want to play ? yes

1. what does CPU stand for? central processing unit
correct! you got 1 point

2. what does GPU stand for? graphics processing unit
correct! you got 1 point

3. what does RAM stand for? RAM
Incorrect!
current answer is --> random access memory

4. what does PSU stand for? power supplyy unit
Incorrect!
current answer is --> power supply unit

5. what does ROM stand for? read only memory
correct! you got 1 point

number of question is 5
your score is 3
60.0% questions are correct.
28 gói gọn chức năng của ứng dụng của bạn ở mức cao.

Viết ra luồng ứng dụng của bạn ở mức cao như thế này có thể là một khởi đầu tuyệt vời để khám phá các chức năng nào là các khối xây dựng tự nhiên trong mã của bạn. Trong phần còn lại của phần này, bạn sẽ điền vào các chi tiết bị thiếu:

  • Thực hiện
    Wellcome to quiz game !!
    NOTE: if your spelling is incorrect then it is considered as wrong answer
    Do you want to play ? yes
    
    1. what does CPU stand for? central processing unit
    correct! you got 1 point
    
    2. what does GPU stand for? graphics processing unit
    correct! you got 1 point
    
    3. what does RAM stand for? RAM
    Incorrect!
    current answer is --> random access memory
    
    4. what does PSU stand for? power supplyy unit
    Incorrect!
    current answer is --> power supply unit
    
    5. what does ROM stand for? read only memory
    correct! you got 1 point
    
    number of question is 5
    your score is 3
    60.0% questions are correct.
    26.
  • Thực hiện
    Wellcome to quiz game !!
    NOTE: if your spelling is incorrect then it is considered as wrong answer
    Do you want to play ? yes
    
    1. what does CPU stand for? central processing unit
    correct! you got 1 point
    
    2. what does GPU stand for? graphics processing unit
    correct! you got 1 point
    
    3. what does RAM stand for? RAM
    Incorrect!
    current answer is --> random access memory
    
    4. what does PSU stand for? power supplyy unit
    Incorrect!
    current answer is --> power supply unit
    
    5. what does ROM stand for? read only memory
    correct! you got 1 point
    
    number of question is 5
    your score is 3
    60.0% questions are correct.
    27.
  • Xem lại
    Wellcome to quiz game !!
    NOTE: if your spelling is incorrect then it is considered as wrong answer
    Do you want to play ? yes
    
    1. what does CPU stand for? central processing unit
    correct! you got 1 point
    
    2. what does GPU stand for? graphics processing unit
    correct! you got 1 point
    
    3. what does RAM stand for? RAM
    Incorrect!
    current answer is --> random access memory
    
    4. what does PSU stand for? power supplyy unit
    Incorrect!
    current answer is --> power supply unit
    
    5. what does ROM stand for? read only memory
    correct! you got 1 point
    
    number of question is 5
    your score is 3
    60.0% questions are correct.
    28.

Bây giờ bạn sẽ thực hiện các thay đổi khá đáng kể đối với mã của ứng dụng đố của bạn khi bạn tái cấu trúc nó để sử dụng các chức năng. Trước khi làm như vậy, đó là một ý tưởng tốt để đảm bảo bạn có thể trở lại trạng thái hiện tại mà bạn biết hoạt động. Bạn có thể làm điều này bằng cách lưu một bản sao mã của bạn với một tên tệp khác hoặc bằng cách thực hiện một cam kết nếu bạn sử dụng hệ thống điều khiển phiên bản.

Khi bạn đã lưu trữ mã hiện tại một cách an toàn, hãy bắt đầu với một

[
    ["When was the first known use of the word 'quiz'", "1781"],
    ["Which built-in function can get information from the user", "input"],
]
8 mới chỉ chứa các biến nhập và biến toàn cầu của bạn. Bạn có thể sao chép những thứ này từ phiên bản trước của bạn:

>>> name = input["What's your name? "]
What's your name? Geir Arne

>>> name
'Geir Arne'
6

Hãy nhớ rằng bạn chỉ tổ chức lại mã của bạn. Bạn không thêm chức năng mới, vì vậy bạn đã thắng cần phải nhập bất kỳ thư viện mới nào.

Tiếp theo, bạn sẽ thực hiện tiền xử lý cần thiết. Trong trường hợp này, điều này có nghĩa là bạn sẽ chuẩn bị cấu trúc dữ liệu

# quiz.py

QUESTIONS = [
    ["When was the first known use of the word 'quiz'", "1781"],
    ["Which built-in function can get information from the user", "input"],
    ["Which keyword do you use to loop over a given list of elements", "for"]
]

for question, correct_answer in QUESTIONS:
    answer = input[f"{question}? "]
    if answer == correct_answer:
        print["Correct!"]
    else:
        print[f"The answer is {correct_answer!r}, not {answer!r}"]
2 để nó sẵn sàng để sử dụng trong vòng lặp chính của bạn. Hiện tại, bạn có khả năng giới hạn số lượng câu hỏi và đảm bảo rằng họ được liệt kê theo thứ tự ngẫu nhiên:preprocessing. In this case, this means that you’ll prepare the
# quiz.py

QUESTIONS = [
    ["When was the first known use of the word 'quiz'", "1781"],
    ["Which built-in function can get information from the user", "input"],
    ["Which keyword do you use to loop over a given list of elements", "for"]
]

for question, correct_answer in QUESTIONS:
    answer = input[f"{question}? "]
    if answer == correct_answer:
        print["Correct!"]
    else:
        print[f"The answer is {correct_answer!r}, not {answer!r}"]
2 data structure so that it’s ready to be used in your main loop. For now, you’ll potentially limit the number of questions and make sure they’re listed in a random order:

>>> name = input["What's your name? "]
What's your name? Geir Arne

>>> name
'Geir Arne'
7

Lưu ý rằng

Wellcome to quiz game !!
NOTE: if your spelling is incorrect then it is considered as wrong answer
Do you want to play ? yes

1. what does CPU stand for? central processing unit
correct! you got 1 point

2. what does GPU stand for? graphics processing unit
correct! you got 1 point

3. what does RAM stand for? RAM
Incorrect!
current answer is --> random access memory

4. what does PSU stand for? power supplyy unit
Incorrect!
current answer is --> power supply unit

5. what does ROM stand for? read only memory
correct! you got 1 point

number of question is 5
your score is 3
60.0% questions are correct.
26 liên quan đến các tham số chung
Wellcome to quiz game !!
NOTE: if your spelling is incorrect then it is considered as wrong answer
Do you want to play ? yes

1. what does CPU stand for? central processing unit
correct! you got 1 point

2. what does GPU stand for? graphics processing unit
correct! you got 1 point

3. what does RAM stand for? RAM
Incorrect!
current answer is --> random access memory

4. what does PSU stand for? power supplyy unit
Incorrect!
current answer is --> power supply unit

5. what does ROM stand for? read only memory
correct! you got 1 point

number of question is 5
your score is 3
60.0% questions are correct.
35 và
Wellcome to quiz game !!
NOTE: if your spelling is incorrect then it is considered as wrong answer
Do you want to play ? yes

1. what does CPU stand for? central processing unit
correct! you got 1 point

2. what does GPU stand for? graphics processing unit
correct! you got 1 point

3. what does RAM stand for? RAM
Incorrect!
current answer is --> random access memory

4. what does PSU stand for? power supplyy unit
Incorrect!
current answer is --> power supply unit

5. what does ROM stand for? read only memory
correct! you got 1 point

number of question is 5
your score is 3
60.0% questions are correct.
36. Sau đó, bạn sẽ vượt qua trong
# quiz.py

QUESTIONS = [
    ["When was the first known use of the word 'quiz'", "1781"],
    ["Which built-in function can get information from the user", "input"],
    ["Which keyword do you use to loop over a given list of elements", "for"]
]

for question, correct_answer in QUESTIONS:
    answer = input[f"{question}? "]
    if answer == correct_answer:
        print["Correct!"]
    else:
        print[f"The answer is {correct_answer!r}, not {answer!r}"]
2 và
Wellcome to quiz game !!
NOTE: if your spelling is incorrect then it is considered as wrong answer
Do you want to play ? yes

1. what does CPU stand for? central processing unit
correct! you got 1 point

2. what does GPU stand for? graphics processing unit
correct! you got 1 point

3. what does RAM stand for? RAM
Incorrect!
current answer is --> random access memory

4. what does PSU stand for? power supplyy unit
Incorrect!
current answer is --> power supply unit

5. what does ROM stand for? read only memory
correct! you got 1 point

number of question is 5
your score is 3
60.0% questions are correct.
23 cụ thể của mình làm đối số. Điều này có nghĩa là
Wellcome to quiz game !!
NOTE: if your spelling is incorrect then it is considered as wrong answer
Do you want to play ? yes

1. what does CPU stand for? central processing unit
correct! you got 1 point

2. what does GPU stand for? graphics processing unit
correct! you got 1 point

3. what does RAM stand for? RAM
Incorrect!
current answer is --> random access memory

4. what does PSU stand for? power supplyy unit
Incorrect!
current answer is --> power supply unit

5. what does ROM stand for? read only memory
correct! you got 1 point

number of question is 5
your score is 3
60.0% questions are correct.
26 không phụ thuộc vào các biến toàn cầu của bạn. Với sự tách rời này, chức năng của bạn là chung chung hơn và sau này bạn có thể dễ dàng thay thế nguồn câu hỏi của mình.

Hỏi câu hỏi

Nhìn lại bản phác thảo của bạn cho chức năng

Wellcome to quiz game !!
NOTE: if your spelling is incorrect then it is considered as wrong answer
Do you want to play ? yes

1. what does CPU stand for? central processing unit
correct! you got 1 point

2. what does GPU stand for? graphics processing unit
correct! you got 1 point

3. what does RAM stand for? RAM
Incorrect!
current answer is --> random access memory

4. what does PSU stand for? power supplyy unit
Incorrect!
current answer is --> power supply unit

5. what does ROM stand for? read only memory
correct! you got 1 point

number of question is 5
your score is 3
60.0% questions are correct.
28 và nhớ rằng nó chứa vòng chính của bạn. Đối với mỗi câu hỏi, bạn sẽ gọi
Wellcome to quiz game !!
NOTE: if your spelling is incorrect then it is considered as wrong answer
Do you want to play ? yes

1. what does CPU stand for? central processing unit
correct! you got 1 point

2. what does GPU stand for? graphics processing unit
correct! you got 1 point

3. what does RAM stand for? RAM
Incorrect!
current answer is --> random access memory

4. what does PSU stand for? power supplyy unit
Incorrect!
current answer is --> power supply unit

5. what does ROM stand for? read only memory
correct! you got 1 point

number of question is 5
your score is 3
60.0% questions are correct.
27. Nhiệm vụ tiếp theo của bạn là thực hiện chức năng trợ giúp đó.

Hãy suy nghĩ về những gì

Wellcome to quiz game !!
NOTE: if your spelling is incorrect then it is considered as wrong answer
Do you want to play ? yes

1. what does CPU stand for? central processing unit
correct! you got 1 point

2. what does GPU stand for? graphics processing unit
correct! you got 1 point

3. what does RAM stand for? RAM
Incorrect!
current answer is --> random access memory

4. what does PSU stand for? power supplyy unit
Incorrect!
current answer is --> power supply unit

5. what does ROM stand for? read only memory
correct! you got 1 point

number of question is 5
your score is 3
60.0% questions are correct.
27 cần làm:

  1. Chọn ra câu trả lời đúng từ danh sách các lựa chọn thay thế
  2. Xáo trộn các lựa chọn thay thế
  3. In câu hỏi lên màn hình
  4. In tất cả các lựa chọn thay thế cho màn hình
  5. Nhận câu trả lời từ người dùng
  6. Kiểm tra xem câu trả lời của người dùng có hợp lệ không
  7. Kiểm tra xem người dùng có trả lời đúng hay không
  8. Thêm
    Wellcome to quiz game !!
    NOTE: if your spelling is incorrect then it is considered as wrong answer
    Do you want to play ? yes
    
    1. what does CPU stand for? central processing unit
    correct! you got 1 point
    
    2. what does GPU stand for? graphics processing unit
    correct! you got 1 point
    
    3. what does RAM stand for? RAM
    Incorrect!
    current answer is --> random access memory
    
    4. what does PSU stand for? power supplyy unit
    Incorrect!
    current answer is --> power supply unit
    
    5. what does ROM stand for? read only memory
    correct! you got 1 point
    
    number of question is 5
    your score is 3
    60.0% questions are correct.
    02 vào số lượng câu trả lời đúng nếu câu trả lời là chính xác

Đây là rất nhiều điều nhỏ để làm trong một chức năng và bạn có thể xem xét liệu có tiềm năng nào để mô đun hóa hơn nữa hay không. Ví dụ: các mục 3 đến 6 trong danh sách ở trên đều là về việc tương tác với người dùng và bạn có thể kéo chúng vào một chức năng trợ giúp khác.

Để đạt được mô đun hóa này, hãy thêm hàm trợ giúp

Wellcome to quiz game !!
NOTE: if your spelling is incorrect then it is considered as wrong answer
Do you want to play ? yes

1. what does CPU stand for? central processing unit
correct! you got 1 point

2. what does GPU stand for? graphics processing unit
correct! you got 1 point

3. what does RAM stand for? RAM
Incorrect!
current answer is --> random access memory

4. what does PSU stand for? power supplyy unit
Incorrect!
current answer is --> power supply unit

5. what does ROM stand for? read only memory
correct! you got 1 point

number of question is 5
your score is 3
60.0% questions are correct.
44 sau vào mã nguồn của bạn:

>>> name = input["What's your name? "]
What's your name? Geir Arne

>>> name
'Geir Arne'
8

Hàm này chấp nhận một văn bản câu hỏi và một danh sách các lựa chọn thay thế. Sau đó, bạn sử dụng các kỹ thuật tương tự như trước đó để dán nhãn các lựa chọn thay thế và yêu cầu người dùng nhập nhãn hợp lệ. Cuối cùng, bạn trả về câu trả lời của người dùng.

Sử dụng

Wellcome to quiz game !!
NOTE: if your spelling is incorrect then it is considered as wrong answer
Do you want to play ? yes

1. what does CPU stand for? central processing unit
correct! you got 1 point

2. what does GPU stand for? graphics processing unit
correct! you got 1 point

3. what does RAM stand for? RAM
Incorrect!
current answer is --> random access memory

4. what does PSU stand for? power supplyy unit
Incorrect!
current answer is --> power supply unit

5. what does ROM stand for? read only memory
correct! you got 1 point

number of question is 5
your score is 3
60.0% questions are correct.
44 đơn giản hóa việc triển khai
Wellcome to quiz game !!
NOTE: if your spelling is incorrect then it is considered as wrong answer
Do you want to play ? yes

1. what does CPU stand for? central processing unit
correct! you got 1 point

2. what does GPU stand for? graphics processing unit
correct! you got 1 point

3. what does RAM stand for? RAM
Incorrect!
current answer is --> random access memory

4. what does PSU stand for? power supplyy unit
Incorrect!
current answer is --> power supply unit

5. what does ROM stand for? read only memory
correct! you got 1 point

number of question is 5
your score is 3
60.0% questions are correct.
27 của bạn, vì bạn không còn cần phải xử lý tương tác của người dùng. Bạn có thể làm một cái gì đó như sau:

>>> name = input["What's your name? "]
What's your name? Geir Arne

>>> name
'Geir Arne'
9

Đầu tiên bạn sắp xếp lại ngẫu nhiên các giải pháp thay thế câu trả lời bằng cách sử dụng

Wellcome to quiz game !!
NOTE: if your spelling is incorrect then it is considered as wrong answer
Do you want to play ? yes

1. what does CPU stand for? central processing unit
correct! you got 1 point

2. what does GPU stand for? graphics processing unit
correct! you got 1 point

3. what does RAM stand for? RAM
Incorrect!
current answer is --> random access memory

4. what does PSU stand for? power supplyy unit
Incorrect!
current answer is --> power supply unit

5. what does ROM stand for? read only memory
correct! you got 1 point

number of question is 5
your score is 3
60.0% questions are correct.
47, như bạn đã làm trước đó. Tiếp theo, bạn gọi
Wellcome to quiz game !!
NOTE: if your spelling is incorrect then it is considered as wrong answer
Do you want to play ? yes

1. what does CPU stand for? central processing unit
correct! you got 1 point

2. what does GPU stand for? graphics processing unit
correct! you got 1 point

3. what does RAM stand for? RAM
Incorrect!
current answer is --> random access memory

4. what does PSU stand for? power supplyy unit
Incorrect!
current answer is --> power supply unit

5. what does ROM stand for? read only memory
correct! you got 1 point

number of question is 5
your score is 3
60.0% questions are correct.
44, xử lý tất cả các chi tiết về việc nhận câu trả lời từ người dùng. Do đó, bạn có thể hoàn thành
Wellcome to quiz game !!
NOTE: if your spelling is incorrect then it is considered as wrong answer
Do you want to play ? yes

1. what does CPU stand for? central processing unit
correct! you got 1 point

2. what does GPU stand for? graphics processing unit
correct! you got 1 point

3. what does RAM stand for? RAM
Incorrect!
current answer is --> random access memory

4. what does PSU stand for? power supplyy unit
Incorrect!
current answer is --> power supply unit

5. what does ROM stand for? read only memory
correct! you got 1 point

number of question is 5
your score is 3
60.0% questions are correct.
27 bằng cách kiểm tra tính chính xác của câu trả lời. Quan sát rằng bạn trả về
Wellcome to quiz game !!
NOTE: if your spelling is incorrect then it is considered as wrong answer
Do you want to play ? yes

1. what does CPU stand for? central processing unit
correct! you got 1 point

2. what does GPU stand for? graphics processing unit
correct! you got 1 point

3. what does RAM stand for? RAM
Incorrect!
current answer is --> random access memory

4. what does PSU stand for? power supplyy unit
Incorrect!
current answer is --> power supply unit

5. what does ROM stand for? read only memory
correct! you got 1 point

number of question is 5
your score is 3
60.0% questions are correct.
02 hoặc
Wellcome to quiz game !!
NOTE: if your spelling is incorrect then it is considered as wrong answer
Do you want to play ? yes

1. what does CPU stand for? central processing unit
correct! you got 1 point

2. what does GPU stand for? graphics processing unit
correct! you got 1 point

3. what does RAM stand for? RAM
Incorrect!
current answer is --> random access memory

4. what does PSU stand for? power supplyy unit
Incorrect!
current answer is --> power supply unit

5. what does ROM stand for? read only memory
correct! you got 1 point

number of question is 5
your score is 3
60.0% questions are correct.
01, điều này cho thấy chức năng gọi cho dù câu trả lời có chính xác hay không.

Bây giờ bạn đã sẵn sàng để thực hiện

Wellcome to quiz game !!
NOTE: if your spelling is incorrect then it is considered as wrong answer
Do you want to play ? yes

1. what does CPU stand for? central processing unit
correct! you got 1 point

2. what does GPU stand for? graphics processing unit
correct! you got 1 point

3. what does RAM stand for? RAM
Incorrect!
current answer is --> random access memory

4. what does PSU stand for? power supplyy unit
Incorrect!
current answer is --> power supply unit

5. what does ROM stand for? read only memory
correct! you got 1 point

number of question is 5
your score is 3
60.0% questions are correct.
28 đúng cách. Một điều bạn đã học được khi thực hiện
Wellcome to quiz game !!
NOTE: if your spelling is incorrect then it is considered as wrong answer
Do you want to play ? yes

1. what does CPU stand for? central processing unit
correct! you got 1 point

2. what does GPU stand for? graphics processing unit
correct! you got 1 point

3. what does RAM stand for? RAM
Incorrect!
current answer is --> random access memory

4. what does PSU stand for? power supplyy unit
Incorrect!
current answer is --> power supply unit

5. what does ROM stand for? read only memory
correct! you got 1 point

number of question is 5
your score is 3
60.0% questions are correct.
26 và
Wellcome to quiz game !!
NOTE: if your spelling is incorrect then it is considered as wrong answer
Do you want to play ? yes

1. what does CPU stand for? central processing unit
correct! you got 1 point

2. what does GPU stand for? graphics processing unit
correct! you got 1 point

3. what does RAM stand for? RAM
Incorrect!
current answer is --> random access memory

4. what does PSU stand for? power supplyy unit
Incorrect!
current answer is --> power supply unit

5. what does ROM stand for? read only memory
correct! you got 1 point

number of question is 5
your score is 3
60.0% questions are correct.
27 là những đối số bạn cần truyền lại:

>>> answer = input["When was the first known use of the word 'quiz'? "]
When was the first known use of the word 'quiz'? 1781

>>> answer == 1781
False

>>> answer == "1781"
True
0

Như trước đó, bạn sử dụng

# quiz.py

QUESTIONS = [
    ["When was the first known use of the word 'quiz'", "1781"],
    ["Which built-in function can get information from the user", "input"],
    ["Which keyword do you use to loop over a given list of elements", "for"]
]

for question, correct_answer in QUESTIONS:
    answer = input[f"{question}? "]
    if answer == correct_answer:
        print["Correct!"]
    else:
        print[f"The answer is {correct_answer!r}, not {answer!r}"]
7 để giữ một bộ đếm số lượng câu hỏi bạn hỏi. Bạn có thể tăng
Wellcome to quiz game !!
NOTE: if your spelling is incorrect then it is considered as wrong answer
Do you want to play ? yes

1. what does CPU stand for? central processing unit
correct! you got 1 point

2. what does GPU stand for? graphics processing unit
correct! you got 1 point

3. what does RAM stand for? RAM
Incorrect!
current answer is --> random access memory

4. what does PSU stand for? power supplyy unit
Incorrect!
current answer is --> power supply unit

5. what does ROM stand for? read only memory
correct! you got 1 point

number of question is 5
your score is 3
60.0% questions are correct.
07 dựa trên giá trị trả về của
Wellcome to quiz game !!
NOTE: if your spelling is incorrect then it is considered as wrong answer
Do you want to play ? yes

1. what does CPU stand for? central processing unit
correct! you got 1 point

2. what does GPU stand for? graphics processing unit
correct! you got 1 point

3. what does RAM stand for? RAM
Incorrect!
current answer is --> random access memory

4. what does PSU stand for? power supplyy unit
Incorrect!
current answer is --> power supply unit

5. what does ROM stand for? read only memory
correct! you got 1 point

number of question is 5
your score is 3
60.0% questions are correct.
27. Quan sát rằng
Wellcome to quiz game !!
NOTE: if your spelling is incorrect then it is considered as wrong answer
Do you want to play ? yes

1. what does CPU stand for? central processing unit
correct! you got 1 point

2. what does GPU stand for? graphics processing unit
correct! you got 1 point

3. what does RAM stand for? RAM
Incorrect!
current answer is --> random access memory

4. what does PSU stand for? power supplyy unit
Incorrect!
current answer is --> power supply unit

5. what does ROM stand for? read only memory
correct! you got 1 point

number of question is 5
your score is 3
60.0% questions are correct.
28 là chức năng duy nhất của bạn tương tác trực tiếp với
# quiz.py

QUESTIONS = [
    ["When was the first known use of the word 'quiz'", "1781"],
    ["Which built-in function can get information from the user", "input"],
    ["Which keyword do you use to loop over a given list of elements", "for"]
]

for question, correct_answer in QUESTIONS:
    answer = input[f"{question}? "]
    if answer == correct_answer:
        print["Correct!"]
    else:
        print[f"The answer is {correct_answer!r}, not {answer!r}"]
2 và
Wellcome to quiz game !!
NOTE: if your spelling is incorrect then it is considered as wrong answer
Do you want to play ? yes

1. what does CPU stand for? central processing unit
correct! you got 1 point

2. what does GPU stand for? graphics processing unit
correct! you got 1 point

3. what does RAM stand for? RAM
Incorrect!
current answer is --> random access memory

4. what does PSU stand for? power supplyy unit
Incorrect!
current answer is --> power supply unit

5. what does ROM stand for? read only memory
correct! you got 1 point

number of question is 5
your score is 3
60.0% questions are correct.
23.

Việc tái cấu trúc của bạn bây giờ đã hoàn tất, ngoại trừ một điều. Nếu bạn chạy

[
    ["When was the first known use of the word 'quiz'", "1781"],
    ["Which built-in function can get information from the user", "input"],
]
8 bây giờ, thì nó có vẻ như không có gì xảy ra. Trên thực tế, Python sẽ đọc các biến toàn cầu của bạn và xác định các chức năng của bạn. Tuy nhiên, bạn không gọi bất kỳ chức năng nào trong số đó. Do đó, bạn cần thêm một cuộc gọi chức năng bắt đầu ứng dụng của bạn:

>>> answer = input["When was the first known use of the word 'quiz'? "]
When was the first known use of the word 'quiz'? 1781

>>> answer == 1781
False

>>> answer == "1781"
True
1

Bạn gọi

Wellcome to quiz game !!
NOTE: if your spelling is incorrect then it is considered as wrong answer
Do you want to play ? yes

1. what does CPU stand for? central processing unit
correct! you got 1 point

2. what does GPU stand for? graphics processing unit
correct! you got 1 point

3. what does RAM stand for? RAM
Incorrect!
current answer is --> random access memory

4. what does PSU stand for? power supplyy unit
Incorrect!
current answer is --> power supply unit

5. what does ROM stand for? read only memory
correct! you got 1 point

number of question is 5
your score is 3
60.0% questions are correct.
28 vào cuối
[
    ["When was the first known use of the word 'quiz'", "1781"],
    ["Which built-in function can get information from the user", "input"],
]
8, bên ngoài bất kỳ chức năng nào. Nó thực hành tốt để bảo vệ một cuộc gọi như vậy đến chức năng chính của bạn với bài kiểm tra
Wellcome to quiz game !!
NOTE: if your spelling is incorrect then it is considered as wrong answer
Do you want to play ? yes

1. what does CPU stand for? central processing unit
correct! you got 1 point

2. what does GPU stand for? graphics processing unit
correct! you got 1 point

3. what does RAM stand for? RAM
Incorrect!
current answer is --> random access memory

4. what does PSU stand for? power supplyy unit
Incorrect!
current answer is --> power supply unit

5. what does ROM stand for? read only memory
correct! you got 1 point

number of question is 5
your score is 3
60.0% questions are correct.
64. Câu thần chú đặc biệt này là một quy ước Python có nghĩa là
Wellcome to quiz game !!
NOTE: if your spelling is incorrect then it is considered as wrong answer
Do you want to play ? yes

1. what does CPU stand for? central processing unit
correct! you got 1 point

2. what does GPU stand for? graphics processing unit
correct! you got 1 point

3. what does RAM stand for? RAM
Incorrect!
current answer is --> random access memory

4. what does PSU stand for? power supplyy unit
Incorrect!
current answer is --> power supply unit

5. what does ROM stand for? read only memory
correct! you got 1 point

number of question is 5
your score is 3
60.0% questions are correct.
28 được gọi khi bạn chạy
[
    ["When was the first known use of the word 'quiz'", "1781"],
    ["Which built-in function can get information from the user", "input"],
]
8 dưới dạng tập lệnh, nhưng nó không được gọi khi bạn nhập
Wellcome to quiz game !!
NOTE: if your spelling is incorrect then it is considered as wrong answer
Do you want to play ? yes

1. what does CPU stand for? central processing unit
correct! you got 1 point

2. what does GPU stand for? graphics processing unit
correct! you got 1 point

3. what does RAM stand for? RAM
Incorrect!
current answer is --> random access memory

4. what does PSU stand for? power supplyy unit
Incorrect!
current answer is --> power supply unit

5. what does ROM stand for? read only memory
correct! you got 1 point

number of question is 5
your score is 3
60.0% questions are correct.
67 dưới dạng mô -đun.

Đó là nó! Bạn đã tái cấu trúc mã của bạn thành một số chức năng. Điều này sẽ giúp bạn theo dõi chức năng của ứng dụng của bạn. Nó cũng sẽ hữu ích trong hướng dẫn này, vì bạn có thể xem xét các thay đổi đối với các chức năng riêng lẻ thay vì thay đổi toàn bộ kịch bản.

Đối với phần còn lại của hướng dẫn, bạn sẽ thấy mã đầy đủ được liệt kê trong các hộp có thể thu gọn như bên dưới. Mở rộng những điều này để xem trạng thái hiện tại và có được cái nhìn tổng quan về ứng dụng đầy đủ của bạn:

Mã nguồn đầy đủ của ứng dụng Quiz của bạn được liệt kê dưới đây:

>>> answer = input["When was the first known use of the word 'quiz'? "]
When was the first known use of the word 'quiz'? 1781

>>> answer == 1781
False

>>> answer == "1781"
True
2

Chạy ứng dụng của bạn với

Wellcome to quiz game !!
NOTE: if your spelling is incorrect then it is considered as wrong answer
Do you want to play ? yes

1. what does CPU stand for? central processing unit
correct! you got 1 point

2. what does GPU stand for? graphics processing unit
correct! you got 1 point

3. what does RAM stand for? RAM
Incorrect!
current answer is --> random access memory

4. what does PSU stand for? power supplyy unit
Incorrect!
current answer is --> power supply unit

5. what does ROM stand for? read only memory
correct! you got 1 point

number of question is 5
your score is 3
60.0% questions are correct.
68.

Thông qua bước này, bạn đã tái cấu trúc mã của mình để làm cho nó thuận tiện hơn để làm việc. Bạn đã tách các lệnh của mình thành các chức năng được tổ chức tốt mà bạn có thể tiếp tục phát triển. Trong bước tiếp theo, bạn sẽ tận dụng điều này bằng cách cải thiện cách bạn đọc các câu hỏi vào ứng dụng của mình.

Bước 4: Tách dữ liệu vào tệp của riêng mình

Bạn sẽ tiếp tục hành trình tái cấu trúc của mình trong bước này. Trọng tâm của bạn bây giờ sẽ là cách bạn cung cấp câu hỏi cho ứng dụng của bạn.

Cho đến nay, bạn đã lưu trữ các câu hỏi trực tiếp trong mã nguồn của mình trong cấu trúc dữ liệu

# quiz.py

QUESTIONS = [
    ["When was the first known use of the word 'quiz'", "1781"],
    ["Which built-in function can get information from the user", "input"],
    ["Which keyword do you use to loop over a given list of elements", "for"]
]

for question, correct_answer in QUESTIONS:
    answer = input[f"{question}? "]
    if answer == correct_answer:
        print["Correct!"]
    else:
        print[f"The answer is {correct_answer!r}, not {answer!r}"]
2. Nó thường tốt hơn để tách dữ liệu của bạn khỏi mã của bạn. Sự phân tách này có thể làm cho mã của bạn dễ đọc hơn, nhưng quan trọng hơn, bạn có thể tận dụng các hệ thống được thiết kế để xử lý dữ liệu nếu nó không ẩn bên trong mã của bạn.

Trong phần này, bạn sẽ học cách lưu trữ các câu hỏi của mình trong một tệp dữ liệu riêng biệt được định dạng theo tiêu chuẩn Toml. Các tùy chọn khác mà bạn đã giành được bìa trong hướng dẫn này, đang lưu trữ các câu hỏi ở một định dạng tệp khác như JSON hoặc YAML hoặc lưu trữ chúng trong cơ sở dữ liệu, hoặc là cơ sở dữ liệu truyền thống hoặc cơ sở dữ liệu NoQuery.

Để xem qua cách bạn sẽ cải thiện mã của mình trong bước này, nhấp vào bên dưới và truy cập thư mục

Wellcome to quiz game !!
NOTE: if your spelling is incorrect then it is considered as wrong answer
Do you want to play ? yes

1. what does CPU stand for? central processing unit
correct! you got 1 point

2. what does GPU stand for? graphics processing unit
correct! you got 1 point

3. what does RAM stand for? RAM
Incorrect!
current answer is --> random access memory

4. what does PSU stand for? power supplyy unit
Incorrect!
current answer is --> power supply unit

5. what does ROM stand for? read only memory
correct! you got 1 point

number of question is 5
your score is 3
60.0% questions are correct.
70:

Di chuyển câu hỏi sang tệp toml

Toml được coi là định dạng tệp cấu hình cho con người [nguồn]. Nó được thiết kế để có thể đọc được bởi con người và không phức tạp đến phân tích bởi máy tính. Thông tin được thể hiện trong các cặp giá trị khóa có thể được ánh xạ tới cấu trúc dữ liệu bảng băm, giống như từ điển Python.

TomL hỗ trợ một số loại dữ liệu, bao gồm chuỗi, số nguyên, số điểm nổi, booleans và ngày. Ngoài ra, dữ liệu có thể được cấu trúc trong các mảng và bảng, tương tự như danh sách và từ điển của Python, tương ứng. Toml đã trở nên phổ biến trong những năm qua và định dạng ổn định sau phiên bản 1.0.0 của đặc tả định dạng được phát hành vào tháng 1 năm 2021.

Tạo một tệp văn bản mới mà bạn sẽ gọi

Wellcome to quiz game !!
NOTE: if your spelling is incorrect then it is considered as wrong answer
Do you want to play ? yes

1. what does CPU stand for? central processing unit
correct! you got 1 point

2. what does GPU stand for? graphics processing unit
correct! you got 1 point

3. what does RAM stand for? RAM
Incorrect!
current answer is --> random access memory

4. what does PSU stand for? power supplyy unit
Incorrect!
current answer is --> power supply unit

5. what does ROM stand for? read only memory
correct! you got 1 point

number of question is 5
your score is 3
60.0% questions are correct.
71 và thêm nội dung sau:

>>> answer = input["When was the first known use of the word 'quiz'? "]
When was the first known use of the word 'quiz'? 1781

>>> answer == 1781
False

>>> answer == "1781"
True
3

Mặc dù có sự khác biệt giữa cú pháp Toml và cú pháp Python, bạn sẽ nhận ra các yếu tố như sử dụng dấu ngoặc kép [

Wellcome to quiz game !!
NOTE: if your spelling is incorrect then it is considered as wrong answer
Do you want to play ? yes

1. what does CPU stand for? central processing unit
correct! you got 1 point

2. what does GPU stand for? graphics processing unit
correct! you got 1 point

3. what does RAM stand for? RAM
Incorrect!
current answer is --> random access memory

4. what does PSU stand for? power supplyy unit
Incorrect!
current answer is --> power supply unit

5. what does ROM stand for? read only memory
correct! you got 1 point

number of question is 5
your score is 3
60.0% questions are correct.
72] cho dấu ngoặc văn bản và vuông [
Wellcome to quiz game !!
NOTE: if your spelling is incorrect then it is considered as wrong answer
Do you want to play ? yes

1. what does CPU stand for? central processing unit
correct! you got 1 point

2. what does GPU stand for? graphics processing unit
correct! you got 1 point

3. what does RAM stand for? RAM
Incorrect!
current answer is --> random access memory

4. what does PSU stand for? power supplyy unit
Incorrect!
current answer is --> power supply unit

5. what does ROM stand for? read only memory
correct! you got 1 point

number of question is 5
your score is 3
60.0% questions are correct.
73] cho danh sách các yếu tố.

Để làm việc với các tệp Toml trong Python, bạn cần một thư viện phân tích chúng. Trong hướng dẫn này, bạn sẽ sử dụng

Wellcome to quiz game !!
NOTE: if your spelling is incorrect then it is considered as wrong answer
Do you want to play ? yes

1. what does CPU stand for? central processing unit
correct! you got 1 point

2. what does GPU stand for? graphics processing unit
correct! you got 1 point

3. what does RAM stand for? RAM
Incorrect!
current answer is --> random access memory

4. what does PSU stand for? power supplyy unit
Incorrect!
current answer is --> power supply unit

5. what does ROM stand for? read only memory
correct! you got 1 point

number of question is 5
your score is 3
60.0% questions are correct.
74. Đây sẽ là gói duy nhất bạn sử dụng trong dự án này không phải là một phần của thư viện tiêu chuẩn Python.

Trước khi cài đặt

Wellcome to quiz game !!
NOTE: if your spelling is incorrect then it is considered as wrong answer
Do you want to play ? yes

1. what does CPU stand for? central processing unit
correct! you got 1 point

2. what does GPU stand for? graphics processing unit
correct! you got 1 point

3. what does RAM stand for? RAM
Incorrect!
current answer is --> random access memory

4. what does PSU stand for? power supplyy unit
Incorrect!
current answer is --> power supply unit

5. what does ROM stand for? read only memory
correct! you got 1 point

number of question is 5
your score is 3
60.0% questions are correct.
74, bạn nên tạo và kích hoạt môi trường ảo:

  • các cửa sổ
  • Linux + MacOS

>>> answer = input["When was the first known use of the word 'quiz'? "]
When was the first known use of the word 'quiz'? 1781

>>> answer == 1781
False

>>> answer == "1781"
True
4

>>> answer = input["When was the first known use of the word 'quiz'? "]
When was the first known use of the word 'quiz'? 1781

>>> answer == 1781
False

>>> answer == "1781"
True
5

Sau đó, bạn có thể cài đặt

Wellcome to quiz game !!
NOTE: if your spelling is incorrect then it is considered as wrong answer
Do you want to play ? yes

1. what does CPU stand for? central processing unit
correct! you got 1 point

2. what does GPU stand for? graphics processing unit
correct! you got 1 point

3. what does RAM stand for? RAM
Incorrect!
current answer is --> random access memory

4. what does PSU stand for? power supplyy unit
Incorrect!
current answer is --> power supply unit

5. what does ROM stand for? read only memory
correct! you got 1 point

number of question is 5
your score is 3
60.0% questions are correct.
74 với
Wellcome to quiz game !!
NOTE: if your spelling is incorrect then it is considered as wrong answer
Do you want to play ? yes

1. what does CPU stand for? central processing unit
correct! you got 1 point

2. what does GPU stand for? graphics processing unit
correct! you got 1 point

3. what does RAM stand for? RAM
Incorrect!
current answer is --> random access memory

4. what does PSU stand for? power supplyy unit
Incorrect!
current answer is --> power supply unit

5. what does ROM stand for? read only memory
correct! you got 1 point

number of question is 5
your score is 3
60.0% questions are correct.
77:

  • các cửa sổ
  • Linux + MacOS

>>> answer = input["When was the first known use of the word 'quiz'? "]
When was the first known use of the word 'quiz'? 1781

>>> answer == 1781
False

>>> answer == "1781"
True
6

>>> answer = input["When was the first known use of the word 'quiz'? "]
When was the first known use of the word 'quiz'? 1781

>>> answer == 1781
False

>>> answer == "1781"
True
7

Sau đó, bạn có thể cài đặt

Wellcome to quiz game !!
NOTE: if your spelling is incorrect then it is considered as wrong answer
Do you want to play ? yes

1. what does CPU stand for? central processing unit
correct! you got 1 point

2. what does GPU stand for? graphics processing unit
correct! you got 1 point

3. what does RAM stand for? RAM
Incorrect!
current answer is --> random access memory

4. what does PSU stand for? power supplyy unit
Incorrect!
current answer is --> power supply unit

5. what does ROM stand for? read only memory
correct! you got 1 point

number of question is 5
your score is 3
60.0% questions are correct.
74 với
Wellcome to quiz game !!
NOTE: if your spelling is incorrect then it is considered as wrong answer
Do you want to play ? yes

1. what does CPU stand for? central processing unit
correct! you got 1 point

2. what does GPU stand for? graphics processing unit
correct! you got 1 point

3. what does RAM stand for? RAM
Incorrect!
current answer is --> random access memory

4. what does PSU stand for? power supplyy unit
Incorrect!
current answer is --> power supply unit

5. what does ROM stand for? read only memory
correct! you got 1 point

number of question is 5
your score is 3
60.0% questions are correct.
77:

Bạn có thể kiểm tra xem bạn có sẵn

Wellcome to quiz game !!
NOTE: if your spelling is incorrect then it is considered as wrong answer
Do you want to play ? yes

1. what does CPU stand for? central processing unit
correct! you got 1 point

2. what does GPU stand for? graphics processing unit
correct! you got 1 point

3. what does RAM stand for? RAM
Incorrect!
current answer is --> random access memory

4. what does PSU stand for? power supplyy unit
Incorrect!
current answer is --> power supply unit

5. what does ROM stand for? read only memory
correct! you got 1 point

number of question is 5
your score is 3
60.0% questions are correct.
74 bằng cách phân tích cú pháp
Wellcome to quiz game !!
NOTE: if your spelling is incorrect then it is considered as wrong answer
Do you want to play ? yes

1. what does CPU stand for? central processing unit
correct! you got 1 point

2. what does GPU stand for? graphics processing unit
correct! you got 1 point

3. what does RAM stand for? RAM
Incorrect!
current answer is --> random access memory

4. what does PSU stand for? power supplyy unit
Incorrect!
current answer is --> power supply unit

5. what does ROM stand for? read only memory
correct! you got 1 point

number of question is 5
your score is 3
60.0% questions are correct.
71 mà bạn đã tạo trước đó không. Mở lại Python của bạn và kiểm tra mã sau:

>>> answer = input["When was the first known use of the word 'quiz'? "]
When was the first known use of the word 'quiz'? 1781

>>> answer == 1781
False

>>> answer == "1781"
True
8

>>>

Đầu tiên, hãy quan sát rằng

Wellcome to quiz game !!
NOTE: if your spelling is incorrect then it is considered as wrong answer
Do you want to play ? yes

1. what does CPU stand for? central processing unit
correct! you got 1 point

2. what does GPU stand for? graphics processing unit
correct! you got 1 point

3. what does RAM stand for? RAM
Incorrect!
current answer is --> random access memory

4. what does PSU stand for? power supplyy unit
Incorrect!
current answer is --> power supply unit

5. what does ROM stand for? read only memory
correct! you got 1 point

number of question is 5
your score is 3
60.0% questions are correct.
35 là một từ điển Python thông thường có cùng dạng với cấu trúc dữ liệu
# quiz.py

QUESTIONS = [
    ["When was the first known use of the word 'quiz'", "1781"],
    ["Which built-in function can get information from the user", "input"],
    ["Which keyword do you use to loop over a given list of elements", "for"]
]

for question, correct_answer in QUESTIONS:
    answer = input[f"{question}? "]
    if answer == correct_answer:
        print["Correct!"]
    else:
        print[f"The answer is {correct_answer!r}, not {answer!r}"]
2 của bạn mà bạn đã sử dụng cho đến nay.

Bạn có thể sử dụng thông tin

Wellcome to quiz game !!
NOTE: if your spelling is incorrect then it is considered as wrong answer
Do you want to play ? yes

1. what does CPU stand for? central processing unit
correct! you got 1 point

2. what does GPU stand for? graphics processing unit
correct! you got 1 point

3. what does RAM stand for? RAM
Incorrect!
current answer is --> random access memory

4. what does PSU stand for? power supplyy unit
Incorrect!
current answer is --> power supply unit

5. what does ROM stand for? read only memory
correct! you got 1 point

number of question is 5
your score is 3
60.0% questions are correct.
74 để phân tích thông tin theo hai cách khác nhau. Trong ví dụ trên, bạn sử dụng
Wellcome to quiz game !!
NOTE: if your spelling is incorrect then it is considered as wrong answer
Do you want to play ? yes

1. what does CPU stand for? central processing unit
correct! you got 1 point

2. what does GPU stand for? graphics processing unit
correct! you got 1 point

3. what does RAM stand for? RAM
Incorrect!
current answer is --> random access memory

4. what does PSU stand for? power supplyy unit
Incorrect!
current answer is --> power supply unit

5. what does ROM stand for? read only memory
correct! you got 1 point

number of question is 5
your score is 3
60.0% questions are correct.
83 để đọc Toml từ một tay cầm tệp đang mở. Ngoài ra, bạn có thể sử dụng
Wellcome to quiz game !!
NOTE: if your spelling is incorrect then it is considered as wrong answer
Do you want to play ? yes

1. what does CPU stand for? central processing unit
correct! you got 1 point

2. what does GPU stand for? graphics processing unit
correct! you got 1 point

3. what does RAM stand for? RAM
Incorrect!
current answer is --> random access memory

4. what does PSU stand for? power supplyy unit
Incorrect!
current answer is --> power supply unit

5. what does ROM stand for? read only memory
correct! you got 1 point

number of question is 5
your score is 3
60.0% questions are correct.
84 để đọc Toml từ chuỗi văn bản.

>>> answer = input["When was the first known use of the word 'quiz'? "]
When was the first known use of the word 'quiz'? 1781

>>> answer == 1781
False

>>> answer == "1781"
True
9

Bạn có thể tích hợp tệp TomL vào ứng dụng đố vui bằng cách cập nhật phần mở đầu mã của mình, nơi bạn thực hiện nhập khẩu và xác định các biến toàn cầu:

Bạn sử dụng

Wellcome to quiz game !!
NOTE: if your spelling is incorrect then it is considered as wrong answer
Do you want to play ? yes

1. what does CPU stand for? central processing unit
correct! you got 1 point

2. what does GPU stand for? graphics processing unit
correct! you got 1 point

3. what does RAM stand for? RAM
Incorrect!
current answer is --> random access memory

4. what does PSU stand for? power supplyy unit
Incorrect!
current answer is --> power supply unit

5. what does ROM stand for? read only memory
correct! you got 1 point

number of question is 5
your score is 3
60.0% questions are correct.
93 để xử lý đường dẫn đến
Wellcome to quiz game !!
NOTE: if your spelling is incorrect then it is considered as wrong answer
Do you want to play ? yes

1. what does CPU stand for? central processing unit
correct! you got 1 point

2. what does GPU stand for? graphics processing unit
correct! you got 1 point

3. what does RAM stand for? RAM
Incorrect!
current answer is --> random access memory

4. what does PSU stand for? power supplyy unit
Incorrect!
current answer is --> power supply unit

5. what does ROM stand for? read only memory
correct! you got 1 point

number of question is 5
your score is 3
60.0% questions are correct.
71. Thay vì mã hóa cứng đường dẫn đến
Wellcome to quiz game !!
NOTE: if your spelling is incorrect then it is considered as wrong answer
Do you want to play ? yes

1. what does CPU stand for? central processing unit
correct! you got 1 point

2. what does GPU stand for? graphics processing unit
correct! you got 1 point

3. what does RAM stand for? RAM
Incorrect!
current answer is --> random access memory

4. what does PSU stand for? power supplyy unit
Incorrect!
current answer is --> power supply unit

5. what does ROM stand for? read only memory
correct! you got 1 point

number of question is 5
your score is 3
60.0% questions are correct.
71, bạn dựa vào biến
Wellcome to quiz game !!
NOTE: if your spelling is incorrect then it is considered as wrong answer
Do you want to play ? yes

1. what does CPU stand for? central processing unit
correct! you got 1 point

2. what does GPU stand for? graphics processing unit
correct! you got 1 point

3. what does RAM stand for? RAM
Incorrect!
current answer is --> random access memory

4. what does PSU stand for? power supplyy unit
Incorrect!
current answer is --> power supply unit

5. what does ROM stand for? read only memory
correct! you got 1 point

number of question is 5
your score is 3
60.0% questions are correct.
96 đặc biệt. Trong thực tế, bạn đã nói rằng nó nằm trong cùng thư mục với tệp
[
    ["When was the first known use of the word 'quiz'", "1781"],
    ["Which built-in function can get information from the user", "input"],
]
8 của bạn.

Cuối cùng, bạn sử dụng

Wellcome to quiz game !!
NOTE: if your spelling is incorrect then it is considered as wrong answer
Do you want to play ? yes

1. what does CPU stand for? central processing unit
correct! you got 1 point

2. what does GPU stand for? graphics processing unit
correct! you got 1 point

3. what does RAM stand for? RAM
Incorrect!
current answer is --> random access memory

4. what does PSU stand for? power supplyy unit
Incorrect!
current answer is --> power supply unit

5. what does ROM stand for? read only memory
correct! you got 1 point

number of question is 5
your score is 3
60.0% questions are correct.
98 để đọc tệp toml dưới dạng chuỗi văn bản và sau đó
Wellcome to quiz game !!
NOTE: if your spelling is incorrect then it is considered as wrong answer
Do you want to play ? yes

1. what does CPU stand for? central processing unit
correct! you got 1 point

2. what does GPU stand for? graphics processing unit
correct! you got 1 point

3. what does RAM stand for? RAM
Incorrect!
current answer is --> random access memory

4. what does PSU stand for? power supplyy unit
Incorrect!
current answer is --> power supply unit

5. what does ROM stand for? read only memory
correct! you got 1 point

number of question is 5
your score is 3
60.0% questions are correct.
99 để phân tích chuỗi đó thành một từ điển. Như bạn đã thấy trong ví dụ trước, tải tệp TOML dẫn đến cùng cấu trúc dữ liệu như bạn đã có trước đây cho các câu hỏi của bạn. Khi bạn đã thực hiện các thay đổi thành
[
    ["When was the first known use of the word 'quiz'", "1781"],
    ["Which built-in function can get information from the user", "input"],
]
8, ứng dụng đố của bạn vẫn sẽ hoạt động như nhau, mặc dù các câu hỏi được xác định trong tệp Toml thay vì trong mã nguồn của bạn.

Hãy tiếp tục và thêm một vài câu hỏi vào tệp Toml của bạn để xác nhận rằng nó đang được sử dụng.

Thêm tính linh hoạt vào định dạng dữ liệu của bạn

Bạn đã chuyển dữ liệu câu hỏi của mình ra khỏi mã nguồn của mình và vào định dạng tệp dữ liệu chuyên dụng. Một lợi thế của TOML so với từ điển Python thông thường là bạn có thể thêm một số cấu trúc khác vào dữ liệu của mình trong khi vẫn có thể đọc được và có thể duy trì.

Một tính năng đáng chú ý của Toml là bảng. Đây được đặt tên là các phần ánh xạ đến từ điển lồng nhau trong Python. Hơn nữa, bạn có thể sử dụng các mảng của các bảng, được thể hiện bằng danh sách các từ điển trong Python.tables. These are named sections that map to nested dictionaries in Python. Furthermore, you can use arrays of tables, which are represented by lists of dictionaries in Python.

Bạn có thể tận dụng những điều này để rõ ràng hơn khi xác định câu hỏi của bạn. Hãy xem xét đoạn trích TOML sau:

# quiz.py

answer = input["When was the first known use of the word 'quiz'? "]
if answer == "1781":
    print["Correct!"]
else:
    print[f"The answer is '1781', not {answer!r}"]
0

Các bảng thông thường bắt đầu với một dòng một dấu ngoặc như

>>> name = input["What's your name? "]
What's your name? Geir Arne

>>> name
'Geir Arne'
01. Bạn chỉ ra một mảng các bảng bằng cách sử dụng dấu ngoặc kép, như trên. Bạn có thể phân tích lại toml với
Wellcome to quiz game !!
NOTE: if your spelling is incorrect then it is considered as wrong answer
Do you want to play ? yes

1. what does CPU stand for? central processing unit
correct! you got 1 point

2. what does GPU stand for? graphics processing unit
correct! you got 1 point

3. what does RAM stand for? RAM
Incorrect!
current answer is --> random access memory

4. what does PSU stand for? power supplyy unit
Incorrect!
current answer is --> power supply unit

5. what does ROM stand for? read only memory
correct! you got 1 point

number of question is 5
your score is 3
60.0% questions are correct.
74:

>>>

# quiz.py

answer = input["When was the first known use of the word 'quiz'? "]
if answer == "1781":
    print["Correct!"]
else:
    print[f"The answer is '1781', not {answer!r}"]
1

Điều này dẫn đến cấu trúc dữ liệu lồng nhau, với từ điển bên ngoài trong đó các điểm chính của

Wellcome to quiz game !!
NOTE: if your spelling is incorrect then it is considered as wrong answer
Do you want to play ? yes

1. what does CPU stand for? central processing unit
correct! you got 1 point

2. what does GPU stand for? graphics processing unit
correct! you got 1 point

3. what does RAM stand for? RAM
Incorrect!
current answer is --> random access memory

4. what does PSU stand for? power supplyy unit
Incorrect!
current answer is --> power supply unit

5. what does ROM stand for? read only memory
correct! you got 1 point

number of question is 5
your score is 3
60.0% questions are correct.
35 vào danh sách các từ điển. Các từ điển bên trong có khóa
>>> name = input["What's your name? "]
What's your name? Geir Arne

>>> name
'Geir Arne'
04,
Wellcome to quiz game !!
NOTE: if your spelling is incorrect then it is considered as wrong answer
Do you want to play ? yes

1. what does CPU stand for? central processing unit
correct! you got 1 point

2. what does GPU stand for? graphics processing unit
correct! you got 1 point

3. what does RAM stand for? RAM
Incorrect!
current answer is --> random access memory

4. what does PSU stand for? power supplyy unit
Incorrect!
current answer is --> power supply unit

5. what does ROM stand for? read only memory
correct! you got 1 point

number of question is 5
your score is 3
60.0% questions are correct.
20 và
>>> name = input["What's your name? "]
What's your name? Geir Arne

>>> name
'Geir Arne'
06.

Cấu trúc này phức tạp hơn một chút so với những gì bạn đã sử dụng cho đến nay. Tuy nhiên, nó cũng rõ ràng hơn và bạn không cần phải dựa vào các quy ước như câu trả lời đầu tiên thay thế đại diện cho câu trả lời đúng.

Bây giờ bạn sẽ chuyển đổi ứng dụng đố của mình để tận dụng cấu trúc dữ liệu mới này cho các câu hỏi của bạn. Đầu tiên, định dạng lại câu hỏi của bạn trong

Wellcome to quiz game !!
NOTE: if your spelling is incorrect then it is considered as wrong answer
Do you want to play ? yes

1. what does CPU stand for? central processing unit
correct! you got 1 point

2. what does GPU stand for? graphics processing unit
correct! you got 1 point

3. what does RAM stand for? RAM
Incorrect!
current answer is --> random access memory

4. what does PSU stand for? power supplyy unit
Incorrect!
current answer is --> power supply unit

5. what does ROM stand for? read only memory
correct! you got 1 point

number of question is 5
your score is 3
60.0% questions are correct.
71. Bạn nên định dạng chúng như sau:

# quiz.py

answer = input["When was the first known use of the word 'quiz'? "]
if answer == "1781":
    print["Correct!"]
else:
    print[f"The answer is '1781', not {answer!r}"]
2

Mỗi câu hỏi được lưu trữ bên trong một bảng riêng lẻ

Wellcome to quiz game !!
NOTE: if your spelling is incorrect then it is considered as wrong answer
Do you want to play ? yes

1. what does CPU stand for? central processing unit
correct! you got 1 point

2. what does GPU stand for? graphics processing unit
correct! you got 1 point

3. what does RAM stand for? RAM
Incorrect!
current answer is --> random access memory

4. what does PSU stand for? power supplyy unit
Incorrect!
current answer is --> power supply unit

5. what does ROM stand for? read only memory
correct! you got 1 point

number of question is 5
your score is 3
60.0% questions are correct.
35 với các cặp giá trị khóa cho văn bản câu hỏi, câu trả lời đúng và các giải pháp thay thế câu trả lời.

Về cơ bản, bạn sẽ cần thực hiện hai thay đổi cho mã nguồn ứng dụng của mình để sử dụng định dạng mới:

  1. Đọc câu hỏi từ danh sách bên trong
    Wellcome to quiz game !!
    NOTE: if your spelling is incorrect then it is considered as wrong answer
    Do you want to play ? yes
    
    1. what does CPU stand for? central processing unit
    correct! you got 1 point
    
    2. what does GPU stand for? graphics processing unit
    correct! you got 1 point
    
    3. what does RAM stand for? RAM
    Incorrect!
    current answer is --> random access memory
    
    4. what does PSU stand for? power supplyy unit
    Incorrect!
    current answer is --> power supply unit
    
    5. what does ROM stand for? read only memory
    correct! you got 1 point
    
    number of question is 5
    your score is 3
    60.0% questions are correct.
    35.
  2. Sử dụng từ điển câu hỏi bên trong khi đặt câu hỏi.

Những thay đổi này chạm vào cấu trúc dữ liệu chính của bạn, vì vậy chúng yêu cầu một số thay đổi mã nhỏ trong suốt mã của bạn.

Đầu tiên, thay đổi cách bạn đọc các câu hỏi từ tệp toml:

# quiz.py

answer = input["When was the first known use of the word 'quiz'? "]
if answer == "1781":
    print["Correct!"]
else:
    print[f"The answer is '1781', not {answer!r}"]
3

Bạn thay đổi

Wellcome to quiz game !!
NOTE: if your spelling is incorrect then it is considered as wrong answer
Do you want to play ? yes

1. what does CPU stand for? central processing unit
correct! you got 1 point

2. what does GPU stand for? graphics processing unit
correct! you got 1 point

3. what does RAM stand for? RAM
Incorrect!
current answer is --> random access memory

4. what does PSU stand for? power supplyy unit
Incorrect!
current answer is --> power supply unit

5. what does ROM stand for? read only memory
correct! you got 1 point

number of question is 5
your score is 3
60.0% questions are correct.
26 để đọc tệp Toml và chọn danh sách
Wellcome to quiz game !!
NOTE: if your spelling is incorrect then it is considered as wrong answer
Do you want to play ? yes

1. what does CPU stand for? central processing unit
correct! you got 1 point

2. what does GPU stand for? graphics processing unit
correct! you got 1 point

3. what does RAM stand for? RAM
Incorrect!
current answer is --> random access memory

4. what does PSU stand for? power supplyy unit
Incorrect!
current answer is --> power supply unit

5. what does ROM stand for? read only memory
correct! you got 1 point

number of question is 5
your score is 3
60.0% questions are correct.
35. Ngoài ra, bạn có thể đơn giản hóa vòng lặp chính trong
Wellcome to quiz game !!
NOTE: if your spelling is incorrect then it is considered as wrong answer
Do you want to play ? yes

1. what does CPU stand for? central processing unit
correct! you got 1 point

2. what does GPU stand for? graphics processing unit
correct! you got 1 point

3. what does RAM stand for? RAM
Incorrect!
current answer is --> random access memory

4. what does PSU stand for? power supplyy unit
Incorrect!
current answer is --> power supply unit

5. what does ROM stand for? read only memory
correct! you got 1 point

number of question is 5
your score is 3
60.0% questions are correct.
28 vì tất cả thông tin về một câu hỏi được chứa trong một từ điển. Bạn không theo dõi văn bản câu hỏi và các giải pháp thay thế riêng biệt.

Điểm sau này cũng đòi hỏi một số thay đổi trong

Wellcome to quiz game !!
NOTE: if your spelling is incorrect then it is considered as wrong answer
Do you want to play ? yes

1. what does CPU stand for? central processing unit
correct! you got 1 point

2. what does GPU stand for? graphics processing unit
correct! you got 1 point

3. what does RAM stand for? RAM
Incorrect!
current answer is --> random access memory

4. what does PSU stand for? power supplyy unit
Incorrect!
current answer is --> power supply unit

5. what does ROM stand for? read only memory
correct! you got 1 point

number of question is 5
your score is 3
60.0% questions are correct.
27:

# quiz.py

answer = input["When was the first known use of the word 'quiz'? "]
if answer == "1781":
    print["Correct!"]
else:
    print[f"The answer is '1781', not {answer!r}"]
4

Bây giờ bạn chọn ra văn bản câu hỏi, câu trả lời đúng và câu trả lời thay thế rõ ràng từ từ điển

>>> name = input["What's your name? "]
What's your name? Geir Arne

>>> name
'Geir Arne'
04 mới. Một điều tốt đẹp với điều này là nó dễ đọc hơn so với quy ước trước đó về việc giả định câu trả lời đầu tiên là câu trả lời đúng.

Bạn không cần phải thực hiện bất kỳ thay đổi nào trong

Wellcome to quiz game !!
NOTE: if your spelling is incorrect then it is considered as wrong answer
Do you want to play ? yes

1. what does CPU stand for? central processing unit
correct! you got 1 point

2. what does GPU stand for? graphics processing unit
correct! you got 1 point

3. what does RAM stand for? RAM
Incorrect!
current answer is --> random access memory

4. what does PSU stand for? power supplyy unit
Incorrect!
current answer is --> power supply unit

5. what does ROM stand for? read only memory
correct! you got 1 point

number of question is 5
your score is 3
60.0% questions are correct.
44, bởi vì chức năng đó đã xử lý văn bản câu hỏi và danh sách các lựa chọn thay thế nói chung. Điều đó đã thay đổi.

Bạn có thể tìm thấy mã nguồn đầy đủ hiện tại của ứng dụng của bạn bên trong các phần bị sụp đổ bên dưới:

Tệp dữ liệu

Wellcome to quiz game !!
NOTE: if your spelling is incorrect then it is considered as wrong answer
Do you want to play ? yes

1. what does CPU stand for? central processing unit
correct! you got 1 point

2. what does GPU stand for? graphics processing unit
correct! you got 1 point

3. what does RAM stand for? RAM
Incorrect!
current answer is --> random access memory

4. what does PSU stand for? power supplyy unit
Incorrect!
current answer is --> power supply unit

5. what does ROM stand for? read only memory
correct! you got 1 point

number of question is 5
your score is 3
60.0% questions are correct.
71 đầy đủ được sao chép dưới đây:

# quiz.py

answer = input["When was the first known use of the word 'quiz'? "]
if answer == "1781":
    print["Correct!"]
else:
    print[f"The answer is '1781', not {answer!r}"]
5

Lưu tệp này trong cùng một thư mục với

[
    ["When was the first known use of the word 'quiz'", "1781"],
    ["Which built-in function can get information from the user", "input"],
]
8.

Mã nguồn đầy đủ của ứng dụng Quiz của bạn được liệt kê dưới đây:

# quiz.py

answer = input["When was the first known use of the word 'quiz'? "]
if answer == "1781":
    print["Correct!"]
else:
    print[f"The answer is '1781', not {answer!r}"]
6

Chạy ứng dụng của bạn với

Wellcome to quiz game !!
NOTE: if your spelling is incorrect then it is considered as wrong answer
Do you want to play ? yes

1. what does CPU stand for? central processing unit
correct! you got 1 point

2. what does GPU stand for? graphics processing unit
correct! you got 1 point

3. what does RAM stand for? RAM
Incorrect!
current answer is --> random access memory

4. what does PSU stand for? power supplyy unit
Incorrect!
current answer is --> power supply unit

5. what does ROM stand for? read only memory
correct! you got 1 point

number of question is 5
your score is 3
60.0% questions are correct.
68.

Định dạng linh hoạt mới của bạn để xác định câu hỏi cung cấp cho bạn một số tùy chọn trong việc thêm nhiều chức năng vào ứng dụng đố của bạn. Bạn sẽ đi sâu vào một số trong số này trong bước tiếp theo.

Bước 5: Mở rộng chức năng đố của bạn

Trong bước thứ năm này, bạn sẽ thêm nhiều chức năng vào ứng dụng đố của mình. Cuối cùng, việc tái cấu trúc mà bạn đã thực hiện trong các bước trước sẽ được đền đáp! Bạn sẽ thêm các tính năng sau:

  • Câu hỏi có nhiều câu trả lời đúng
  • Những gợi ý có thể hướng về câu trả lời đúng
  • Giải thích có thể đóng vai trò là những khoảnh khắc giảng dạy

Vào cuối bước này, ứng dụng của bạn sẽ hoạt động như sau:

Những tính năng mới này cung cấp trải nghiệm thú vị hơn cho bất kỳ ai thử thách bản thân thông qua ứng dụng đố của bạn. Bạn có thể thấy mã nguồn của ứng dụng sẽ trông như thế nào khi bạn đã hoàn thành bước này bằng cách nhấp vào bên dưới và chuyển vào thư mục

>>> name = input["What's your name? "]
What's your name? Geir Arne

>>> name
'Geir Arne'
19:

Cho phép nhiều câu trả lời đúng

Một số câu hỏi có thể có nhiều câu trả lời đúng, và nó sẽ rất tuyệt nếu bài kiểm tra của bạn cũng có thể xử lý những câu hỏi đó. Trong phần này, bạn sẽ thêm hỗ trợ cho nhiều câu trả lời đúng.

Đầu tiên, bạn cần xem xét làm thế nào bạn có thể đại diện cho một số câu trả lời chính xác trong tệp dữ liệu

Wellcome to quiz game !!
NOTE: if your spelling is incorrect then it is considered as wrong answer
Do you want to play ? yes

1. what does CPU stand for? central processing unit
correct! you got 1 point

2. what does GPU stand for? graphics processing unit
correct! you got 1 point

3. what does RAM stand for? RAM
Incorrect!
current answer is --> random access memory

4. what does PSU stand for? power supplyy unit
Incorrect!
current answer is --> power supply unit

5. what does ROM stand for? read only memory
correct! you got 1 point

number of question is 5
your score is 3
60.0% questions are correct.
71 của mình. Một lợi thế của cấu trúc dữ liệu rõ ràng hơn mà bạn đã giới thiệu trong bước trước là bạn cũng có thể sử dụng một mảng để chỉ định các câu trả lời chính xác. Thay thế từng khóa
Wellcome to quiz game !!
NOTE: if your spelling is incorrect then it is considered as wrong answer
Do you want to play ? yes

1. what does CPU stand for? central processing unit
correct! you got 1 point

2. what does GPU stand for? graphics processing unit
correct! you got 1 point

3. what does RAM stand for? RAM
Incorrect!
current answer is --> random access memory

4. what does PSU stand for? power supplyy unit
Incorrect!
current answer is --> power supply unit

5. what does ROM stand for? read only memory
correct! you got 1 point

number of question is 5
your score is 3
60.0% questions are correct.
20 trong tệp Toml của bạn bằng khóa
>>> name = input["What's your name? "]
What's your name? Geir Arne

>>> name
'Geir Arne'
22 kết thúc mỗi câu trả lời đúng trong dấu ngoặc vuông [
Wellcome to quiz game !!
NOTE: if your spelling is incorrect then it is considered as wrong answer
Do you want to play ? yes

1. what does CPU stand for? central processing unit
correct! you got 1 point

2. what does GPU stand for? graphics processing unit
correct! you got 1 point

3. what does RAM stand for? RAM
Incorrect!
current answer is --> random access memory

4. what does PSU stand for? power supplyy unit
Incorrect!
current answer is --> power supply unit

5. what does ROM stand for? read only memory
correct! you got 1 point

number of question is 5
your score is 3
60.0% questions are correct.
73].

Tệp câu hỏi của bạn sau đó sẽ trông giống như sau:

# quiz.py

answer = input["When was the first known use of the word 'quiz'? "]
if answer == "1781":
    print["Correct!"]
else:
    print[f"The answer is '1781', not {answer!r}"]
7

Đối với các câu hỏi cũ chỉ có một câu trả lời đúng, sẽ chỉ có một câu trả lời được liệt kê trong mảng

>>> name = input["What's your name? "]
What's your name? Geir Arne

>>> name
'Geir Arne'
22. Câu hỏi cuối cùng ở trên cho thấy một ví dụ về một câu hỏi với hai câu trả lời đúng.

Khi cấu trúc dữ liệu của bạn được cập nhật, bạn cũng sẽ cần triển khai tính năng trong mã của mình. Bạn không cần phải thực hiện bất kỳ thay đổi nào trong

Wellcome to quiz game !!
NOTE: if your spelling is incorrect then it is considered as wrong answer
Do you want to play ? yes

1. what does CPU stand for? central processing unit
correct! you got 1 point

2. what does GPU stand for? graphics processing unit
correct! you got 1 point

3. what does RAM stand for? RAM
Incorrect!
current answer is --> random access memory

4. what does PSU stand for? power supplyy unit
Incorrect!
current answer is --> power supply unit

5. what does ROM stand for? read only memory
correct! you got 1 point

number of question is 5
your score is 3
60.0% questions are correct.
28 hoặc
Wellcome to quiz game !!
NOTE: if your spelling is incorrect then it is considered as wrong answer
Do you want to play ? yes

1. what does CPU stand for? central processing unit
correct! you got 1 point

2. what does GPU stand for? graphics processing unit
correct! you got 1 point

3. what does RAM stand for? RAM
Incorrect!
current answer is --> random access memory

4. what does PSU stand for? power supplyy unit
Incorrect!
current answer is --> power supply unit

5. what does ROM stand for? read only memory
correct! you got 1 point

number of question is 5
your score is 3
60.0% questions are correct.
26. Trong
Wellcome to quiz game !!
NOTE: if your spelling is incorrect then it is considered as wrong answer
Do you want to play ? yes

1. what does CPU stand for? central processing unit
correct! you got 1 point

2. what does GPU stand for? graphics processing unit
correct! you got 1 point

3. what does RAM stand for? RAM
Incorrect!
current answer is --> random access memory

4. what does PSU stand for? power supplyy unit
Incorrect!
current answer is --> power supply unit

5. what does ROM stand for? read only memory
correct! you got 1 point

number of question is 5
your score is 3
60.0% questions are correct.
27 Bạn cần kiểm tra xem tất cả các câu trả lời đúng có được đưa ra không, trong khi trong
Wellcome to quiz game !!
NOTE: if your spelling is incorrect then it is considered as wrong answer
Do you want to play ? yes

1. what does CPU stand for? central processing unit
correct! you got 1 point

2. what does GPU stand for? graphics processing unit
correct! you got 1 point

3. what does RAM stand for? RAM
Incorrect!
current answer is --> random access memory

4. what does PSU stand for? power supplyy unit
Incorrect!
current answer is --> power supply unit

5. what does ROM stand for? read only memory
correct! you got 1 point

number of question is 5
your score is 3
60.0% questions are correct.
44, bạn cần có thể đọc nhiều câu trả lời từ người dùng.

Bắt đầu với thử thách sau. Làm thế nào người dùng có thể nhập nhiều câu trả lời và làm thế nào bạn có thể xác nhận rằng mỗi người có hợp lệ? Một khả năng là nhập nhiều câu trả lời dưới dạng chuỗi được phân tách bằng dấu phẩy. Sau đó, bạn có thể chuyển đổi chuỗi thành một danh sách như sau:

>>>

# quiz.py

answer = input["When was the first known use of the word 'quiz'? "]
if answer == "1781":
    print["Correct!"]
else:
    print[f"The answer is '1781', not {answer!r}"]
8

Bạn có thể sử dụng

>>> name = input["What's your name? "]
What's your name? Geir Arne

>>> name
'Geir Arne'
29 để chia trực tiếp trên dấu phẩy. Tuy nhiên, trước tiên, việc thay thế dấu phẩy bằng không gian và sau đó tách ra khoảng trắng mặc định thêm một số khoan hồng với không gian được phép xung quanh dấu phẩy. Đây sẽ là một trải nghiệm tốt hơn cho người dùng của bạn, vì họ có thể viết
>>> name = input["What's your name? "]
What's your name? Geir Arne

>>> name
'Geir Arne'
30,
>>> name = input["What's your name? "]
What's your name? Geir Arne

>>> name
'Geir Arne'
31 hoặc thậm chí
>>> name = input["What's your name? "]
What's your name? Geir Arne

>>> name
'Geir Arne'
32 mà không có dấu phẩy và chương trình của bạn nên giải thích nó như dự định.

Bài kiểm tra cho câu trả lời hợp lệ trở nên phức tạp hơn một chút. Do đó, bạn thay thế vòng

$ python quiz.py
When was the first known use of the word 'quiz'? 1781
Correct!
Which built-in function can get information from the user? get
The answer is 'input', not 'get'
6 chặt chẽ bằng một vòng linh hoạt hơn. Để lặp lại cho đến khi bạn nhận được câu trả lời hợp lệ, bạn bắt đầu một vòng lặp vô hạn mà bạn trở lại sau khi tất cả các bài kiểm tra được thỏa mãn. Đổi tên
Wellcome to quiz game !!
NOTE: if your spelling is incorrect then it is considered as wrong answer
Do you want to play ? yes

1. what does CPU stand for? central processing unit
correct! you got 1 point

2. what does GPU stand for? graphics processing unit
correct! you got 1 point

3. what does RAM stand for? RAM
Incorrect!
current answer is --> random access memory

4. what does PSU stand for? power supplyy unit
Incorrect!
current answer is --> power supply unit

5. what does ROM stand for? read only memory
correct! you got 1 point

number of question is 5
your score is 3
60.0% questions are correct.
44 thành
>>> name = input["What's your name? "]
What's your name? Geir Arne

>>> name
'Geir Arne'
35 và cập nhật nó như sau:

# quiz.py

answer = input["When was the first known use of the word 'quiz'? "]
if answer == "1781":
    print["Correct!"]
else:
    print[f"The answer is '1781', not {answer!r}"]
9

Trước khi xem xét quá kỹ vào các chi tiết trong mã, hãy lấy chức năng để chạy thử:

>>>

$ python quiz.py
When was the first known use of the word 'quiz'? 1871
The answer is '1781', not '1871'
0

Bạn có thể sử dụng

>>> name = input["What's your name? "]
What's your name? Geir Arne

>>> name
'Geir Arne'
29 để chia trực tiếp trên dấu phẩy. Tuy nhiên, trước tiên, việc thay thế dấu phẩy bằng không gian và sau đó tách ra khoảng trắng mặc định thêm một số khoan hồng với không gian được phép xung quanh dấu phẩy. Đây sẽ là một trải nghiệm tốt hơn cho người dùng của bạn, vì họ có thể viết
>>> name = input["What's your name? "]
What's your name? Geir Arne

>>> name
'Geir Arne'
30,
>>> name = input["What's your name? "]
What's your name? Geir Arne

>>> name
'Geir Arne'
31 hoặc thậm chí
>>> name = input["What's your name? "]
What's your name? Geir Arne

>>> name
'Geir Arne'
32 mà không có dấu phẩy và chương trình của bạn nên giải thích nó như dự định.

Bài kiểm tra cho câu trả lời hợp lệ trở nên phức tạp hơn một chút. Do đó, bạn thay thế vòng

$ python quiz.py
When was the first known use of the word 'quiz'? 1781
Correct!
Which built-in function can get information from the user? get
The answer is 'input', not 'get'
6 chặt chẽ bằng một vòng linh hoạt hơn. Để lặp lại cho đến khi bạn nhận được câu trả lời hợp lệ, bạn bắt đầu một vòng lặp vô hạn mà bạn trở lại sau khi tất cả các bài kiểm tra được thỏa mãn. Đổi tên
Wellcome to quiz game !!
NOTE: if your spelling is incorrect then it is considered as wrong answer
Do you want to play ? yes

1. what does CPU stand for? central processing unit
correct! you got 1 point

2. what does GPU stand for? graphics processing unit
correct! you got 1 point

3. what does RAM stand for? RAM
Incorrect!
current answer is --> random access memory

4. what does PSU stand for? power supplyy unit
Incorrect!
current answer is --> power supply unit

5. what does ROM stand for? read only memory
correct! you got 1 point

number of question is 5
your score is 3
60.0% questions are correct.
44 thành
>>> name = input["What's your name? "]
What's your name? Geir Arne

>>> name
'Geir Arne'
35 và cập nhật nó như sau:

Trước khi xem xét quá kỹ vào các chi tiết trong mã, hãy lấy chức năng để chạy thử:

Chức năng của bạn trước tiên kiểm tra xem câu trả lời bao gồm số lượng lựa chọn thích hợp. Sau đó, mỗi người được kiểm tra để đảm bảo rằng nó là một lựa chọn hợp lệ. Nếu bất kỳ kiểm tra nào trong số này không thành công, thì một tin nhắn hữu ích được in cho người dùng.

Trong mã, bạn cũng thực hiện một số nỗ lực để xử lý sự khác biệt giữa một và một số mục khi nói đến ngữ pháp. Bạn sử dụng

>>> name = input["What's your name? "]
What's your name? Geir Arne

>>> name
'Geir Arne'
36 để sửa đổi chuỗi văn bản để bao gồm số nhiều khi cần thiết.

Ngoài ra, bạn chuyển đổi câu trả lời thành một tập hợp để nhanh chóng bỏ qua các lựa chọn thay thế trùng lặp. Một chuỗi câu trả lời như

>>> name = input["What's your name? "]
What's your name? Geir Arne

>>> name
'Geir Arne'
37 được hiểu là
>>> name = input["What's your name? "]
What's your name? Geir Arne

>>> name
'Geir Arne'
38.

$ python quiz.py
When was the first known use of the word 'quiz'? 1871
The answer is '1781', not '1871'
1

Cuối cùng, lưu ý rằng

>>> name = input["What's your name? "]
What's your name? Geir Arne

>>> name
'Geir Arne'
35 trả về một danh sách các chuỗi thay vì chuỗi đơn giản được trả về bởi
Wellcome to quiz game !!
NOTE: if your spelling is incorrect then it is considered as wrong answer
Do you want to play ? yes

1. what does CPU stand for? central processing unit
correct! you got 1 point

2. what does GPU stand for? graphics processing unit
correct! you got 1 point

3. what does RAM stand for? RAM
Incorrect!
current answer is --> random access memory

4. what does PSU stand for? power supplyy unit
Incorrect!
current answer is --> power supply unit

5. what does ROM stand for? read only memory
correct! you got 1 point

number of question is 5
your score is 3
60.0% questions are correct.
44.

$ python quiz.py
When was the first known use of the word 'quiz'? 1871
The answer is '1781', not '1871'
2

Tiếp theo, bạn điều chỉnh

Wellcome to quiz game !!
NOTE: if your spelling is incorrect then it is considered as wrong answer
Do you want to play ? yes

1. what does CPU stand for? central processing unit
correct! you got 1 point

2. what does GPU stand for? graphics processing unit
correct! you got 1 point

3. what does RAM stand for? RAM
Incorrect!
current answer is --> random access memory

4. what does PSU stand for? power supplyy unit
Incorrect!
current answer is --> power supply unit

5. what does ROM stand for? read only memory
correct! you got 1 point

number of question is 5
your score is 3
60.0% questions are correct.
27 với khả năng nhiều câu trả lời đúng. Vì
>>> name = input["What's your name? "]
What's your name? Geir Arne

>>> name
'Geir Arne'
35 đã xử lý hầu hết các biến chứng, những gì còn lại là để kiểm tra tất cả các câu trả lời thay vì chỉ một. Hãy nhớ lại rằng
>>> name = input["What's your name? "]
What's your name? Geir Arne

>>> name
'Geir Arne'
04 là một từ điển với tất cả thông tin về một câu hỏi, vì vậy bạn không cần phải vượt qua
>>> name = input["What's your name? "]
What's your name? Geir Arne

>>> name
'Geir Arne'
06 nữa.

Bởi vì thứ tự của các câu trả lời là không liên quan, bạn sử dụng
>>> name = input["What's your name? "]
What's your name? Geir Arne

>>> name
'Geir Arne'
45 khi so sánh các câu trả lời đã cho với các câu trả lời chính xác:

Bạn chỉ ghi một điểm cho người dùng nếu họ tìm thấy tất cả các câu trả lời chính xác. Nếu không, bạn liệt kê tất cả các câu trả lời đúng. Bây giờ bạn có thể chạy ứng dụng Python Quiz của mình một lần nữa:hints.

Cho phép nhiều câu trả lời đúng cho bạn sự linh hoạt hơn trong đó các loại câu hỏi bạn có thể hỏi trong các câu đố của bạn.

$ python quiz.py
When was the first known use of the word 'quiz'? 1871
The answer is '1781', not '1871'
3

Thêm gợi ý để giúp người dùng

Thỉnh thoảng khi bạn hỏi một câu hỏi, bạn cần một chút trợ giúp để chạy bộ nhớ. Cung cấp cho người dùng tùy chọn nhìn thấy một gợi ý có thể làm cho các câu đố của bạn thú vị hơn. Trong phần này, bạn sẽ mở rộng ứng dụng của mình để bao gồm các gợi ý.

$ python quiz.py
When was the first known use of the word 'quiz'? 1871
The answer is '1781', not '1871'
4

Bạn sử dụng

>>> name = input["What's your name? "]
What's your name? Geir Arne

>>> name
'Geir Arne'
50 thay vì
>>> name = input["What's your name? "]
What's your name? Geir Arne

>>> name
'Geir Arne'
51 vì không phải tất cả các câu hỏi đều có gợi ý. Nếu một trong các từ điển
>>> name = input["What's your name? "]
What's your name? Geir Arne

>>> name
'Geir Arne'
04 không định nghĩa
>>> name = input["What's your name? "]
What's your name? Geir Arne

>>> name
'Geir Arne'
53 là chìa khóa, thì
>>> name = input["What's your name? "]
What's your name? Geir Arne

>>> name
'Geir Arne'
50 sẽ trả về
>>> name = input["What's your name? "]
What's your name? Geir Arne

>>> name
'Geir Arne'
55, sau đó được chuyển vào
>>> name = input["What's your name? "]
What's your name? Geir Arne

>>> name
'Geir Arne'
35.

Một lần nữa, bạn sẽ thực hiện các thay đổi lớn hơn đối với

>>> name = input["What's your name? "]
What's your name? Geir Arne

>>> name
'Geir Arne'
35. Bạn sẽ thêm gợi ý là một trong những lựa chọn thay thế câu trả lời, với nhãn câu hỏi đặc biệt [
>>> name = input["What's your name? "]
What's your name? Geir Arne

>>> name
'Geir Arne'
58]:

$ python quiz.py
When was the first known use of the word 'quiz'? 1871
The answer is '1781', not '1871'
5

Nếu một gợi ý được cung cấp, thì nó thêm vào cuối

Wellcome to quiz game !!
NOTE: if your spelling is incorrect then it is considered as wrong answer
Do you want to play ? yes

1. what does CPU stand for? central processing unit
correct! you got 1 point

2. what does GPU stand for? graphics processing unit
correct! you got 1 point

3. what does RAM stand for? RAM
Incorrect!
current answer is --> random access memory

4. what does PSU stand for? power supplyy unit
Incorrect!
current answer is --> power supply unit

5. what does ROM stand for? read only memory
correct! you got 1 point

number of question is 5
your score is 3
60.0% questions are correct.
19. Sau đó, người dùng có thể sử dụng
>>> name = input["What's your name? "]
What's your name? Geir Arne

>>> name
'Geir Arne'
58 để xem gợi ý được in lên màn hình. Nếu bạn kiểm tra ứng dụng đố của mình, thì bây giờ bạn sẽ nhận được một chút trợ giúp thân thiện:

$ python quiz.py
When was the first known use of the word 'quiz'? 1871
The answer is '1781', not '1871'
6

Trong phần tiếp theo, bạn sẽ thêm một tính năng tương tự. Ngoài việc hiển thị một gợi ý tùy chọn trước khi người dùng trả lời một câu hỏi, bạn sẽ hiển thị một lời giải thích sau khi họ đã trả lời nó.

Thêm các giải thích để củng cố việc học

Bạn có thể thực hiện các giải thích tương tự như cách bạn thực hiện các gợi ý trong phần trước. Đầu tiên, bạn sẽ thêm một trường

>>> name = input["What's your name? "]
What's your name? Geir Arne

>>> name
'Geir Arne'
61 tùy chọn trong tệp dữ liệu của mình. Sau đó, trong ứng dụng của bạn, bạn sẽ hiển thị lời giải thích sau khi người dùng đã trả lời một câu hỏi.explanations similarly to how you implemented hints in the previous section. First, you’ll add an optional
>>> name = input["What's your name? "]
What's your name? Geir Arne

>>> name
'Geir Arne'
61 field in your data file. Then, in your application, you’ll show the explanation after the user has answered a question.

Bắt đầu với việc thêm các khóa

>>> name = input["What's your name? "]
What's your name? Geir Arne

>>> name
'Geir Arne'
61 trong
Wellcome to quiz game !!
NOTE: if your spelling is incorrect then it is considered as wrong answer
Do you want to play ? yes

1. what does CPU stand for? central processing unit
correct! you got 1 point

2. what does GPU stand for? graphics processing unit
correct! you got 1 point

3. what does RAM stand for? RAM
Incorrect!
current answer is --> random access memory

4. what does PSU stand for? power supplyy unit
Incorrect!
current answer is --> power supply unit

5. what does ROM stand for? read only memory
correct! you got 1 point

number of question is 5
your score is 3
60.0% questions are correct.
71:

$ python quiz.py
When was the first known use of the word 'quiz'? 1871
The answer is '1781', not '1871'
7

TomL hỗ trợ các chuỗi đa dòng bằng cách sử dụng trích dẫn ba [

>>> name = input["What's your name? "]
What's your name? Geir Arne

>>> name
'Geir Arne'
64] theo cách tương tự như Python. Đây là những cách giải thích tuyệt vời có thể kéo dài một vài câu.

Các giải thích sẽ được in lên màn hình sau khi người dùng trả lời một câu hỏi. Nói cách khác, các giải thích không phải là một phần của tương tác người dùng được thực hiện trong

>>> name = input["What's your name? "]
What's your name? Geir Arne

>>> name
'Geir Arne'
35. Thay vào đó, bạn sẽ in chúng bên trong
Wellcome to quiz game !!
NOTE: if your spelling is incorrect then it is considered as wrong answer
Do you want to play ? yes

1. what does CPU stand for? central processing unit
correct! you got 1 point

2. what does GPU stand for? graphics processing unit
correct! you got 1 point

3. what does RAM stand for? RAM
Incorrect!
current answer is --> random access memory

4. what does PSU stand for? power supplyy unit
Incorrect!
current answer is --> power supply unit

5. what does ROM stand for? read only memory
correct! you got 1 point

number of question is 5
your score is 3
60.0% questions are correct.
27:

$ python quiz.py
When was the first known use of the word 'quiz'? 1871
The answer is '1781', not '1871'
8

Bởi vì bạn in lời giải thích sau khi đưa ra phản hồi của người dùng về việc câu trả lời của họ có đúng hay không, bạn có thể quay lại bên trong khối

$ python quiz.py
When was the first known use of the word 'quiz'? 1781
Correct!
Which built-in function can get information from the user? get
The answer is 'input', not 'get'
4 ____ ____268 nữa. Do đó, bạn di chuyển câu lệnh
>>> name = input["What's your name? "]
What's your name? Geir Arne

>>> name
'Geir Arne'
69 đến cuối hàm.

Giải thích của bạn trông giống như sau khi bạn chạy ứng dụng đố vui của mình:

$ python quiz.py
When was the first known use of the word 'quiz'? 1871
The answer is '1781', not '1871'
9

Các cải tiến cho ứng dụng đố Python của bạn cộng lại. Vui lòng mở rộng các phần bị thu gọn bên dưới để xem mã nguồn đầy đủ với tất cả các tính năng mới của bạn:

Tệp dữ liệu

Wellcome to quiz game !!
NOTE: if your spelling is incorrect then it is considered as wrong answer
Do you want to play ? yes

1. what does CPU stand for? central processing unit
correct! you got 1 point

2. what does GPU stand for? graphics processing unit
correct! you got 1 point

3. what does RAM stand for? RAM
Incorrect!
current answer is --> random access memory

4. what does PSU stand for? power supplyy unit
Incorrect!
current answer is --> power supply unit

5. what does ROM stand for? read only memory
correct! you got 1 point

number of question is 5
your score is 3
60.0% questions are correct.
71 đầy đủ được sao chép dưới đây:

# quiz.py

answer = input["When was the first known use of the word 'quiz'? "]
if answer == "1781":
    print["Correct!"]
else:
    print[f"The answer is '1781', not {answer!r}"]

answer = input["Which built-in function can get information from the user? "]
if answer == "input":
    print["Correct!"]
else:
    print[f"The answer is 'input', not {answer!r}"]
0

Lưu tệp này trong cùng một thư mục với

[
    ["When was the first known use of the word 'quiz'", "1781"],
    ["Which built-in function can get information from the user", "input"],
]
8.

Mã nguồn đầy đủ của ứng dụng Quiz của bạn được liệt kê dưới đây:

# quiz.py

answer = input["When was the first known use of the word 'quiz'? "]
if answer == "1781":
    print["Correct!"]
else:
    print[f"The answer is '1781', not {answer!r}"]

answer = input["Which built-in function can get information from the user? "]
if answer == "input":
    print["Correct!"]
else:
    print[f"The answer is 'input', not {answer!r}"]
1

Chạy ứng dụng của bạn với

Wellcome to quiz game !!
NOTE: if your spelling is incorrect then it is considered as wrong answer
Do you want to play ? yes

1. what does CPU stand for? central processing unit
correct! you got 1 point

2. what does GPU stand for? graphics processing unit
correct! you got 1 point

3. what does RAM stand for? RAM
Incorrect!
current answer is --> random access memory

4. what does PSU stand for? power supplyy unit
Incorrect!
current answer is --> power supply unit

5. what does ROM stand for? read only memory
correct! you got 1 point

number of question is 5
your score is 3
60.0% questions are correct.
68.

Trong bước cuối cùng, bạn sẽ thêm một tính năng nữa: hỗ trợ cho một số chủ đề đố trong ứng dụng của bạn.

Bước 6: Hỗ trợ một số chủ đề đố

Trong phần này, bạn sẽ thực hiện một cải tiến cuối cùng sẽ làm cho ứng dụng đố vui của bạn trở nên thú vị, đa dạng và thú vị hơn. Bạn sẽ thêm tùy chọn nhóm các câu hỏi vào các chủ đề khác nhau và cho phép người dùng của bạn chọn chủ đề mà họ sẽ được hỏi.

Phiên bản cuối cùng của ứng dụng Python Quiz của bạn sẽ trông như sau:

Nhiều chủ đề và câu hỏi mới sẽ giữ cho ứng dụng đố của bạn mới. Nhấp vào bên dưới và điều hướng đến thư mục

>>> name = input["What's your name? "]
What's your name? Geir Arne

>>> name
'Geir Arne'
73 để xem mã nguồn sẽ chăm sóc bạn như thế nào sau khi bạn đã thêm những thứ này:

Các phần trong các tập tin Toml có thể được lồng nhau. Bạn tạo các bảng lồng nhau bằng cách thêm các khoảng thời gian [

>>> name = input["What's your name? "]
What's your name? Geir Arne

>>> name
'Geir Arne'
74] trong các tiêu đề phần. Như một ví dụ minh họa, hãy xem xét tài liệu Toml sau:

>>>

# quiz.py

answer = input["When was the first known use of the word 'quiz'? "]
if answer == "1781":
    print["Correct!"]
else:
    print[f"The answer is '1781', not {answer!r}"]

answer = input["Which built-in function can get information from the user? "]
if answer == "input":
    print["Correct!"]
else:
    print[f"The answer is 'input', not {answer!r}"]
2

Ở đây, tiêu đề phần

>>> name = input["What's your name? "]
What's your name? Geir Arne

>>> name
'Geir Arne'
75 được biểu thị dưới dạng
>>> name = input["What's your name? "]
What's your name? Geir Arne

>>> name
'Geir Arne'
76 lồng trong vòng
>>> name = input["What's your name? "]
What's your name? Geir Arne

>>> name
'Geir Arne'
77. Tương tự, các khóa có thời gian cũng được hiểu là từ điển lồng nhau, bằng chứng là
>>> name = input["What's your name? "]
What's your name? Geir Arne

>>> name
'Geir Arne'
78 trong ví dụ này.

Bạn có thể tổ chức lại

Wellcome to quiz game !!
NOTE: if your spelling is incorrect then it is considered as wrong answer
Do you want to play ? yes

1. what does CPU stand for? central processing unit
correct! you got 1 point

2. what does GPU stand for? graphics processing unit
correct! you got 1 point

3. what does RAM stand for? RAM
Incorrect!
current answer is --> random access memory

4. what does PSU stand for? power supplyy unit
Incorrect!
current answer is --> power supply unit

5. what does ROM stand for? read only memory
correct! you got 1 point

number of question is 5
your score is 3
60.0% questions are correct.
71 để bao gồm một phần cho mỗi chủ đề. Ngoài các mảng
Wellcome to quiz game !!
NOTE: if your spelling is incorrect then it is considered as wrong answer
Do you want to play ? yes

1. what does CPU stand for? central processing unit
correct! you got 1 point

2. what does GPU stand for? graphics processing unit
correct! you got 1 point

3. what does RAM stand for? RAM
Incorrect!
current answer is --> random access memory

4. what does PSU stand for? power supplyy unit
Incorrect!
current answer is --> power supply unit

5. what does ROM stand for? read only memory
correct! you got 1 point

number of question is 5
your score is 3
60.0% questions are correct.
35 lồng nhau, bạn sẽ thêm khóa
>>> name = input["What's your name? "]
What's your name? Geir Arne

>>> name
'Geir Arne'
81 cung cấp tên cho mỗi chủ đề. Cập nhật tệp dữ liệu của bạn để sử dụng định dạng sau:

# quiz.py

answer = input["When was the first known use of the word 'quiz'? "]
if answer == "1781":
    print["Correct!"]
else:
    print[f"The answer is '1781', not {answer!r}"]

answer = input["Which built-in function can get information from the user? "]
if answer == "input":
    print["Correct!"]
else:
    print[f"The answer is 'input', not {answer!r}"]
3

Bây giờ, có hai chủ đề có trong tệp dữ liệu: Python và Thủ đô. Trong mỗi phần chủ đề, các bảng câu hỏi vẫn được cấu trúc giống như trước đây. Điều này có nghĩa là thay đổi duy nhất bạn cần thực hiện là cách bạn chuẩn bị các câu hỏi.

Bạn bắt đầu bằng cách đọc và phân tích cú pháp

Wellcome to quiz game !!
NOTE: if your spelling is incorrect then it is considered as wrong answer
Do you want to play ? yes

1. what does CPU stand for? central processing unit
correct! you got 1 point

2. what does GPU stand for? graphics processing unit
correct! you got 1 point

3. what does RAM stand for? RAM
Incorrect!
current answer is --> random access memory

4. what does PSU stand for? power supplyy unit
Incorrect!
current answer is --> power supply unit

5. what does ROM stand for? read only memory
correct! you got 1 point

number of question is 5
your score is 3
60.0% questions are correct.
71. Tiếp theo, bạn chọn từng chủ đề và lưu trữ nó trong một từ điển mới, tạm thời. Bạn cần hỏi người dùng về chủ đề mà họ muốn thử. May mắn thay, bạn có thể sử dụng lại
>>> name = input["What's your name? "]
What's your name? Geir Arne

>>> name
'Geir Arne'
35 để có được đầu vào về điều này. Cuối cùng, bạn chọn ra các câu hỏi thuộc về chủ đề đã chọn và xáo trộn chúng:

# quiz.py

answer = input["When was the first known use of the word 'quiz'? "]
if answer == "1781":
    print["Correct!"]
else:
    print[f"The answer is '1781', not {answer!r}"]

answer = input["Which built-in function can get information from the user? "]
if answer == "input":
    print["Correct!"]
else:
    print[f"The answer is 'input', not {answer!r}"]
4

Cấu trúc dữ liệu được trả về bởi

Wellcome to quiz game !!
NOTE: if your spelling is incorrect then it is considered as wrong answer
Do you want to play ? yes

1. what does CPU stand for? central processing unit
correct! you got 1 point

2. what does GPU stand for? graphics processing unit
correct! you got 1 point

3. what does RAM stand for? RAM
Incorrect!
current answer is --> random access memory

4. what does PSU stand for? power supplyy unit
Incorrect!
current answer is --> power supply unit

5. what does ROM stand for? read only memory
correct! you got 1 point

number of question is 5
your score is 3
60.0% questions are correct.
26 vẫn giống như trước đây, vì vậy bạn không cần thực hiện bất kỳ thay đổi nào đối với
Wellcome to quiz game !!
NOTE: if your spelling is incorrect then it is considered as wrong answer
Do you want to play ? yes

1. what does CPU stand for? central processing unit
correct! you got 1 point

2. what does GPU stand for? graphics processing unit
correct! you got 1 point

3. what does RAM stand for? RAM
Incorrect!
current answer is --> random access memory

4. what does PSU stand for? power supplyy unit
Incorrect!
current answer is --> power supply unit

5. what does ROM stand for? read only memory
correct! you got 1 point

number of question is 5
your score is 3
60.0% questions are correct.
28,
Wellcome to quiz game !!
NOTE: if your spelling is incorrect then it is considered as wrong answer
Do you want to play ? yes

1. what does CPU stand for? central processing unit
correct! you got 1 point

2. what does GPU stand for? graphics processing unit
correct! you got 1 point

3. what does RAM stand for? RAM
Incorrect!
current answer is --> random access memory

4. what does PSU stand for? power supplyy unit
Incorrect!
current answer is --> power supply unit

5. what does ROM stand for? read only memory
correct! you got 1 point

number of question is 5
your score is 3
60.0% questions are correct.
27 hoặc
>>> name = input["What's your name? "]
What's your name? Geir Arne

>>> name
'Geir Arne'
35. Khi các loại cập nhật này chỉ yêu cầu bạn chỉnh sửa một hoặc một vài chức năng, thì đó là một dấu hiệu tốt cho thấy mã của bạn có cấu trúc tốt, với sự trừu tượng tốt.

Chạy ứng dụng đố Python của bạn. Bạn sẽ được chào đón bởi lời nhắc chủ đề mới:

# quiz.py

answer = input["When was the first known use of the word 'quiz'? "]
if answer == "1781":
    print["Correct!"]
else:
    print[f"The answer is '1781', not {answer!r}"]

answer = input["Which built-in function can get information from the user? "]
if answer == "input":
    print["Correct!"]
else:
    print[f"The answer is 'input', not {answer!r}"]
5

Điều này kết thúc phần hướng dẫn của hành trình này. Bạn đã tạo ra một ứng dụng đố Python mạnh mẽ trong thiết bị đầu cuối. Bạn có thể thấy mã nguồn đầy đủ cũng như danh sách các câu hỏi bằng cách mở rộng các hộp bên dưới:

Tệp dữ liệu

Wellcome to quiz game !!
NOTE: if your spelling is incorrect then it is considered as wrong answer
Do you want to play ? yes

1. what does CPU stand for? central processing unit
correct! you got 1 point

2. what does GPU stand for? graphics processing unit
correct! you got 1 point

3. what does RAM stand for? RAM
Incorrect!
current answer is --> random access memory

4. what does PSU stand for? power supplyy unit
Incorrect!
current answer is --> power supply unit

5. what does ROM stand for? read only memory
correct! you got 1 point

number of question is 5
your score is 3
60.0% questions are correct.
71 đầy đủ được sao chép dưới đây:

# quiz.py

answer = input["When was the first known use of the word 'quiz'? "]
if answer == "1781":
    print["Correct!"]
else:
    print[f"The answer is '1781', not {answer!r}"]

answer = input["Which built-in function can get information from the user? "]
if answer == "input":
    print["Correct!"]
else:
    print[f"The answer is 'input', not {answer!r}"]
6

Lưu tệp này trong cùng một thư mục với

[
    ["When was the first known use of the word 'quiz'", "1781"],
    ["Which built-in function can get information from the user", "input"],
]
8.

Mã nguồn đầy đủ của ứng dụng Quiz của bạn được liệt kê ở đây:

# quiz.py

answer = input["When was the first known use of the word 'quiz'? "]
if answer == "1781":
    print["Correct!"]
else:
    print[f"The answer is '1781', not {answer!r}"]

answer = input["Which built-in function can get information from the user? "]
if answer == "input":
    print["Correct!"]
else:
    print[f"The answer is 'input', not {answer!r}"]
7

Chạy ứng dụng của bạn với

Wellcome to quiz game !!
NOTE: if your spelling is incorrect then it is considered as wrong answer
Do you want to play ? yes

1. what does CPU stand for? central processing unit
correct! you got 1 point

2. what does GPU stand for? graphics processing unit
correct! you got 1 point

3. what does RAM stand for? RAM
Incorrect!
current answer is --> random access memory

4. what does PSU stand for? power supplyy unit
Incorrect!
current answer is --> power supply unit

5. what does ROM stand for? read only memory
correct! you got 1 point

number of question is 5
your score is 3
60.0% questions are correct.
68.

Bạn cũng có thể truy cập mã nguồn và tệp câu hỏi bằng cách nhấp vào bên dưới:

Bạn sẽ tìm thấy phiên bản cuối cùng của ứng dụng trong thư mục

>>> name = input["What's your name? "]
What's your name? Geir Arne

>>> name
'Geir Arne'
73.

Sự kết luận

Làm tốt lắm! Bạn đã tạo ra một ứng dụng đố vui và hữu ích với Python. Trên đường đi, bạn đã học được cách bạn có thể bắt đầu với một tập lệnh cơ bản và xây dựng nó thành một chương trình phức tạp hơn.

Trong hướng dẫn này, bạn đã học được cách:

  • Tương tác với người dùng trong thiết bị đầu cuối with the user in the terminal
  • Cải thiện khả năng sử dụng ứng dụng của bạn the usability of your application
  • Tái cấu trúc ứng dụng của bạn để liên tục cải thiện nó your application to continuously improve it
  • Lưu trữ dữ liệu trong các tệp dữ liệu chuyên dụng data in dedicated data files

Bây giờ, hãy vui vẻ với ứng dụng đố của bạn. Thêm một số câu hỏi của riêng bạn, và thách thức bạn bè của bạn. Chia sẻ câu hỏi tốt nhất và chủ đề đố của bạn trong các ý kiến ​​dưới đây!

Bước tiếp theo

Như bạn đã theo dõi trong hướng dẫn này, bạn đã tạo ra một ứng dụng đố vui có tính năng tốt. Tuy nhiên, vẫn còn nhiều cơ hội để cải thiện dự án.

Dưới đây là một số ý tưởng cho các tính năng bổ sung:

  • Người tạo Quiz: Thêm một ứng dụng riêng để tương tác hỏi các câu hỏi và câu trả lời và lưu trữ chúng ở định dạng Toml thích hợp. Add a separate application that interactively asks for questions and answers and stores them in the proper TOML format.
  • Lưu trữ dữ liệu trong cơ sở dữ liệu: Thay thế tệp dữ liệu Toml bằng cơ sở dữ liệu thích hợp. Replace the TOML data file with a proper database.
  • Câu hỏi Hub: Tạo cơ sở dữ liệu câu hỏi trung tâm trực tuyến mà ứng dụng của bạn có thể kết nối. Create a central questions database online that your application can connect to.
  • Thử thách đa nhân: Cho phép người dùng khác nhau thách thức nhau trong một cuộc thi đố đàng. Allow different users to challenge each other in a trivia competition.

Bạn cũng có thể sử dụng lại logic trong ứng dụng bài kiểm tra này nhưng thay đổi lớp trình bày phía trước. Có lẽ bạn có thể chuyển đổi dự án thành ứng dụng web hoặc tạo ứng dụng Flashcards để giúp bạn chuẩn bị cho các câu đố của mình. Hãy chia sẻ những cải tiến của bạn trong các ý kiến ​​dưới đây.

Làm thế nào để bạn thực hiện một trò chơi đố đơn giản trong Python?

Xây dựng một ứng dụng đố vui với Python..
Bản demo: Ứng dụng đố vui của bạn ..
Tổng quan dự án..
Prerequisites..
Bước 1: Đặt câu hỏi. ....
Bước 2: Làm cho ứng dụng của bạn thân thiện với người dùng. ....
Bước 3: Tổ chức mã của bạn với các chức năng. ....
Bước 4: Tách dữ liệu vào tệp của riêng mình. ....
Bước 5: Mở rộng chức năng đố của bạn ..

Làm thế nào để bạn ghi điểm trong Python?

Bạn cần thêm các điểm như thế này:..
điểm = 0 ..
câu hỏi = đầu vào ['ksljafoiwahgsda?'].
câu hỏi = int [câu hỏi].
Nếu câu hỏi = true:.
Điểm += 1 ..
Elif câu hỏi = Sai:.
Điểm -= 1 ..
print[score].

Làm thế nào để bạn tạo một câu hỏi và câu trả lời trong Python?

Làm thế nào để bạn hỏi một câu hỏi trong Python ?..
Câu hỏi = đầu vào ["câu hỏi của bạn"].
Nếu câu hỏi == ["Có"].
In ["Làm tốt"].
Elif câu hỏi == ["Không"].
in ["thử lại"].

Làm cách nào để tạo một trò chơi trong Python?

Dưới đây là ví dụ sau đây về việc tạo một cửa sổ pygame đơn giản ...
Nhập pygame ..
pygame.init[].
màn hình = pygame.display.set_mode [[400.500]].
Xong = Sai ..
trong khi không được thực hiện:.
cho sự kiện trong pygame.event.get []:.
Nếu event.type == pygame.quit:.
Xong = Đúng ..

Chủ Đề