Hướng dẫn python dice module - nói mô-đun python

Xây dựng các dự án nhỏ, như ứng dụng Xúc xắc dựa trên giao diện người dùng dựa trên văn bản (TUI), sẽ giúp bạn tăng cấp các kỹ năng lập trình Python của mình. Bạn có thể học cách thu thập và xác thực đầu vào của người dùng, nhập mã từ các mô -đun và gói, ghi các chức năng, sử dụng các vòng và điều kiện

 1# dice.py
 2
 3def parse_input(input_string):
 4    """Return `input_string` as an integer between 1 and 6.
 5
 6    Check if `input_string` is an integer number between 1 and 6.
 7    If so, return an integer with the same value. Otherwise, tell
 8    the user to enter a valid number and quit the program.
 9    """
10    if input_string.strip() in {"1", "2", "3", "4", "5", "6"}:
11        return int(input_string)
12    else:
13        print("Please enter a number from 1 to 6.")
14        raise SystemExit(1)
15
16# ~~~ App's main code block ~~~
17# ...
4 và hiển thị gọn gàng đầu ra bằng cách sử dụng Chuỗi và hàm
 1# dice.py
 2
 3def parse_input(input_string):
 4    """Return `input_string` as an integer between 1 and 6.
 5
 6    Check if `input_string` is an integer number between 1 and 6.
 7    If so, return an integer with the same value. Otherwise, tell
 8    the user to enter a valid number and quit the program.
 9    """
10    if input_string.strip() in {"1", "2", "3", "4", "5", "6"}:
11        return int(input_string)
12    else:
13        print("Please enter a number from 1 to 6.")
14        raise SystemExit(1)
15
16# ~~~ App's main code block ~~~
17# ...
5.

Nội dung chính ShowShow

  • Thử nghiệm
  • Tổng quan dự án
  • Điều kiện tiên quyết
  • Bước 1: Mã TUI của ứng dụng Rolling Dice Python của bạn
  • Lấy đầu vào của người dùng tại dòng lệnh
  • Phân tích cú pháp và xác nhận đầu vào của người dùng
  • Hãy thử ứng dụng Rolling Dice
  • Bước 2: Mô phỏng việc lăn xúc xắc sáu mặt trong Python
  • Bước 3: Tạo và hiển thị sơ đồ ASCII của các mặt xúc xắc
  • Thiết lập sơ đồ mặt xúc xắc
  • Tạo sơ đồ mặt xúc xắc
  • Kết thúc mã chính của ứng dụng và cuộn xúc xắc
  • Bước 4: Tái cấu trúc mã tạo ra sơ đồ mặt xúc xắc
  • Sự kết luận
  • Bước tiếp theo
  • Trình mô phỏng xúc xắc là gì?
  • Làm thế nào để bạn mô phỏng một con xúc xắc trong Python?
  • Làm thế nào để bạn làm một con xúc xắc trong trình mô phỏng lăn?

Trong dự án này, bạn sẽ mã hóa một ứng dụng mô phỏng các sự kiện lăn xúc xắc. Để làm như vậy, bạn sẽ sử dụng mô -đun Python từ

 1# dice.py
 2
 3def parse_input(input_string):
 4    """Return `input_string` as an integer between 1 and 6.
 5
 6    Check if `input_string` is an integer number between 1 and 6.
 7    If so, return an integer with the same value. Otherwise, tell
 8    the user to enter a valid number and quit the program.
 9    """
10    if input_string.strip() in {"1", "2", "3", "4", "5", "6"}:
11        return int(input_string)
12    else:
13        print("Please enter a number from 1 to 6.")
14        raise SystemExit(1)
15
16# ~~~ App's main code block ~~~
17# ...
6.

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

  • Sử dụng
     1# dice.py
     2
     3def parse_input(input_string):
     4    """Return `input_string` as an integer between 1 and 6.
     5
     6    Check if `input_string` is an integer number between 1 and 6.
     7    If so, return an integer with the same value. Otherwise, tell
     8    the user to enter a valid number and quit the program.
     9    """
    10    if input_string.strip() in {"1", "2", "3", "4", "5", "6"}:
    11        return int(input_string)
    12    else:
    13        print("Please enter a number from 1 to 6.")
    14        raise SystemExit(1)
    15
    16# ~~~ App's main code block ~~~
    17# ...
    
    7 để mô phỏng các sự kiện lăn xúc xắc
     1# dice.py
     2
     3def parse_input(input_string):
     4    """Return `input_string` as an integer between 1 and 6.
     5
     6    Check if `input_string` is an integer number between 1 and 6.
     7    If so, return an integer with the same value. Otherwise, tell
     8    the user to enter a valid number and quit the program.
     9    """
    10    if input_string.strip() in {"1", "2", "3", "4", "5", "6"}:
    11        return int(input_string)
    12    else:
    13        print("Please enter a number from 1 to 6.")
    14        raise SystemExit(1)
    15
    16# ~~~ App's main code block ~~~
    17# ...
    
    7 to simulate dice-rolling events
     1# dice.py
     2
     3def parse_input(input_string):
     4    """Return `input_string` as an integer between 1 and 6.
     5
     6    Check if `input_string` is an integer number between 1 and 6.
     7    If so, return an integer with the same value. Otherwise, tell
     8    the user to enter a valid number and quit the program.
     9    """
    10    if input_string.strip() in {"1", "2", "3", "4", "5", "6"}:
    11        return int(input_string)
    12    else:
    13        print("Please enter a number from 1 to 6.")
    14        raise SystemExit(1)
    15
    16# ~~~ App's main code block ~~~
    17# ...
    
    7
    to simulate dice-rolling events
  • Yêu cầu đầu vào của người dùng bằng cách sử dụng chức năng
     1# dice.py
     2
     3def parse_input(input_string):
     4    """Return `input_string` as an integer between 1 and 6.
     5
     6    Check if `input_string` is an integer number between 1 and 6.
     7    If so, return an integer with the same value. Otherwise, tell
     8    the user to enter a valid number and quit the program.
     9    """
    10    if input_string.strip() in {"1", "2", "3", "4", "5", "6"}:
    11        return int(input_string)
    12    else:
    13        print("Please enter a number from 1 to 6.")
    14        raise SystemExit(1)
    15
    16# ~~~ App's main code block ~~~
    17# ...
    
    8 tích hợpuser’s input using the built-in
     1# dice.py
     2
     3def parse_input(input_string):
     4    """Return `input_string` as an integer between 1 and 6.
     5
     6    Check if `input_string` is an integer number between 1 and 6.
     7    If so, return an integer with the same value. Otherwise, tell
     8    the user to enter a valid number and quit the program.
     9    """
    10    if input_string.strip() in {"1", "2", "3", "4", "5", "6"}:
    11        return int(input_string)
    12    else:
    13        print("Please enter a number from 1 to 6.")
    14        raise SystemExit(1)
    15
    16# ~~~ App's main code block ~~~
    17# ...
    
    8 functionuser’s input using the built-in
     1# dice.py
     2
     3def parse_input(input_string):
     4    """Return `input_string` as an integer between 1 and 6.
     5
     6    Check if `input_string` is an integer number between 1 and 6.
     7    If so, return an integer with the same value. Otherwise, tell
     8    the user to enter a valid number and quit the program.
     9    """
    10    if input_string.strip() in {"1", "2", "3", "4", "5", "6"}:
    11        return int(input_string)
    12    else:
    13        print("Please enter a number from 1 to 6.")
    14        raise SystemExit(1)
    15
    16# ~~~ App's main code block ~~~
    17# ...
    
    8
    function
  • Phân tích cú pháp và xác nhận đầu vào của người dùng and validate the user’s input and validate the user’s input
  • Thao tác chuỗi bằng các phương pháp, chẳng hạn như
     1# dice.py
     2
     3def parse_input(input_string):
     4    """Return `input_string` as an integer between 1 and 6.
     5
     6    Check if `input_string` is an integer number between 1 and 6.
     7    If so, return an integer with the same value. Otherwise, tell
     8    the user to enter a valid number and quit the program.
     9    """
    10    if input_string.strip() in {"1", "2", "3", "4", "5", "6"}:
    11        return int(input_string)
    12    else:
    13        print("Please enter a number from 1 to 6.")
    14        raise SystemExit(1)
    15
    16# ~~~ App's main code block ~~~
    17# ...
    
    9 và
    $ python dice.py
    How many dice do you want to roll? [1-6] 3
    
    $ python dice.py
    How many dice do you want to roll? [1-6] 7
    Please enter a number from 1 to 6.
    
    0strings using methods, such as
     1# dice.py
     2
     3def parse_input(input_string):
     4    """Return `input_string` as an integer between 1 and 6.
     5
     6    Check if `input_string` is an integer number between 1 and 6.
     7    If so, return an integer with the same value. Otherwise, tell
     8    the user to enter a valid number and quit the program.
     9    """
    10    if input_string.strip() in {"1", "2", "3", "4", "5", "6"}:
    11        return int(input_string)
    12    else:
    13        print("Please enter a number from 1 to 6.")
    14        raise SystemExit(1)
    15
    16# ~~~ App's main code block ~~~
    17# ...
    
    9 and
    $ python dice.py
    How many dice do you want to roll? [1-6] 3
    
    $ python dice.py
    How many dice do you want to roll? [1-6] 7
    Please enter a number from 1 to 6.
    
    0strings using methods, such as
     1# dice.py
     2
     3def parse_input(input_string):
     4    """Return `input_string` as an integer between 1 and 6.
     5
     6    Check if `input_string` is an integer number between 1 and 6.
     7    If so, return an integer with the same value. Otherwise, tell
     8    the user to enter a valid number and quit the program.
     9    """
    10    if input_string.strip() in {"1", "2", "3", "4", "5", "6"}:
    11        return int(input_string)
    12    else:
    13        print("Please enter a number from 1 to 6.")
    14        raise SystemExit(1)
    15
    16# ~~~ App's main code block ~~~
    17# ...
    
    9
    and
    $ python dice.py
    How many dice do you want to roll? [1-6] 3
    
    $ python dice.py
    How many dice do you want to roll? [1-6] 7
    Please enter a number from 1 to 6.
    
    0

Bạn cũng sẽ học những điều cơ bản về cách cấu trúc, tổ chức, tài liệu và chạy các chương trình và kịch bản Python của bạn.

Nhấp vào liên kết bên dưới để tải xuống toàn bộ mã cho ứng dụng Rolling Dice này và theo dõi trong khi bạn tự xây dựng dự án:

Thử nghiệm

Tổng quan dự án

Hướng dẫn python dice module - nói mô-đun python

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

Tổng quan dự án

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

Bước 1: Mã TUI của ứng dụng Rolling Dice Python của bạn

Lấy đầu vào của người dùng tại dòng lệnhPhân tích cú pháp và xác nhận đầu vào của người dùngHãy thử ứng dụng Rolling Dice
Bước 2: Mô phỏng việc lăn xúc xắc sáu mặt trong PythonBước 3: Tạo và hiển thị sơ đồ ASCII của các mặt xúc xắcThiết lập sơ đồ mặt xúc xắc
Tạo sơ đồ mặt xúc xắcKết thúc mã chính của ứng dụng và cuộn xúc xắcBước 4: Tái cấu trúc mã tạo ra sơ đồ mặt xúc xắc
Sự kết luậnBước tiếp theoTrình mô phỏng xúc xắc là gì?
Làm thế nào để bạn mô phỏng một con xúc xắc trong Python?Làm thế nào để bạn làm một con xúc xắc trong trình mô phỏng lăn?Trong dự án này, bạn sẽ mã hóa một ứng dụng mô phỏng các sự kiện lăn xúc xắc. Để làm như vậy, bạn sẽ sử dụng mô -đun Python từ
Trong hướng dẫn này, bạn sẽ học cách:Sử dụng
 1# dice.py
 2
 3def parse_input(input_string):
 4    """Return `input_string` as an integer between 1 and 6.
 5
 6    Check if `input_string` is an integer number between 1 and 6.
 7    If so, return an integer with the same value. Otherwise, tell
 8    the user to enter a valid number and quit the program.
 9    """
10    if input_string.strip() in {"1", "2", "3", "4", "5", "6"}:
11        return int(input_string)
12    else:
13        print("Please enter a number from 1 to 6.")
14        raise SystemExit(1)
15
16# ~~~ App's main code block ~~~
17# ...
7 để mô phỏng các sự kiện lăn xúc xắc
 1# dice.py
 2
 3def parse_input(input_string):
 4    """Return `input_string` as an integer between 1 and 6.
 5
 6    Check if `input_string` is an integer number between 1 and 6.
 7    If so, return an integer with the same value. Otherwise, tell
 8    the user to enter a valid number and quit the program.
 9    """
10    if input_string.strip() in {"1", "2", "3", "4", "5", "6"}:
11        return int(input_string)
12    else:
13        print("Please enter a number from 1 to 6.")
14        raise SystemExit(1)
15
16# ~~~ App's main code block ~~~
17# ...
7 to simulate dice-rolling events
Yêu cầu đầu vào của người dùng bằng cách sử dụng chức năng
 1# dice.py
 2
 3def parse_input(input_string):
 4    """Return `input_string` as an integer between 1 and 6.
 5
 6    Check if `input_string` is an integer number between 1 and 6.
 7    If so, return an integer with the same value. Otherwise, tell
 8    the user to enter a valid number and quit the program.
 9    """
10    if input_string.strip() in {"1", "2", "3", "4", "5", "6"}:
11        return int(input_string)
12    else:
13        print("Please enter a number from 1 to 6.")
14        raise SystemExit(1)
15
16# ~~~ App's main code block ~~~
17# ...
8 tích hợpuser’s input using the built-in
 1# dice.py
 2
 3def parse_input(input_string):
 4    """Return `input_string` as an integer between 1 and 6.
 5
 6    Check if `input_string` is an integer number between 1 and 6.
 7    If so, return an integer with the same value. Otherwise, tell
 8    the user to enter a valid number and quit the program.
 9    """
10    if input_string.strip() in {"1", "2", "3", "4", "5", "6"}:
11        return int(input_string)
12    else:
13        print("Please enter a number from 1 to 6.")
14        raise SystemExit(1)
15
16# ~~~ App's main code block ~~~
17# ...
8 function

Phân tích cú pháp và xác nhận đầu vào của người dùng and validate the user’s input

Thao tác chuỗi bằng các phương pháp, chẳng hạn như

 1# dice.py
 2
 3def parse_input(input_string):
 4    """Return `input_string` as an integer between 1 and 6.
 5
 6    Check if `input_string` is an integer number between 1 and 6.
 7    If so, return an integer with the same value. Otherwise, tell
 8    the user to enter a valid number and quit the program.
 9    """
10    if input_string.strip() in {"1", "2", "3", "4", "5", "6"}:
11        return int(input_string)
12    else:
13        print("Please enter a number from 1 to 6.")
14        raise SystemExit(1)
15
16# ~~~ App's main code block ~~~
17# ...
9 và
$ python dice.py
How many dice do you want to roll? [1-6] 3

$ python dice.py
How many dice do you want to roll? [1-6] 7
Please enter a number from 1 to 6.
0strings using methods, such as
 1# dice.py
 2
 3def parse_input(input_string):
 4    """Return `input_string` as an integer between 1 and 6.
 5
 6    Check if `input_string` is an integer number between 1 and 6.
 7    If so, return an integer with the same value. Otherwise, tell
 8    the user to enter a valid number and quit the program.
 9    """
10    if input_string.strip() in {"1", "2", "3", "4", "5", "6"}:
11        return int(input_string)
12    else:
13        print("Please enter a number from 1 to 6.")
14        raise SystemExit(1)
15
16# ~~~ App's main code block ~~~
17# ...
9 and
$ python dice.py
How many dice do you want to roll? [1-6] 3

$ python dice.py
How many dice do you want to roll? [1-6] 7
Please enter a number from 1 to 6.
0

Bạn cũng sẽ học những điều cơ bản về cách cấu trúc, tổ chức, tài liệu và chạy các chương trình và kịch bản Python của bạn.

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

Nhấp vào liên kết bên dưới để tải xuống toàn bộ mã cho ứng dụng Rolling Dice này và theo dõi trong khi bạn tự xây dựng dự án:

  • Trong dự án từng bước này, bạn sẽ xây dựng một ứng dụng chạy các mô phỏng xúc xắc. Ứng dụng sẽ có thể cuộn lên đến sáu con xúc xắc, mỗi lần chết có sáu mặt. Sau mỗi lần cuộn, ứng dụng sẽ tạo sơ đồ ASCII của các mặt xúc xắc và hiển thị nó trên màn hình. Video sau đây cho thấy cách thức hoạt động của ứng dụng:
  • Khi bạn chạy ứng dụng giả lập xúc xắc của mình, bạn sẽ nhận được lời nhanh chóng yêu cầu số xúc xắc bạn muốn lăn. Khi bạn cung cấp một số nguyên hợp lệ từ 1 đến 6, bao gồm, thì ứng dụng mô phỏng sự kiện lăn và hiển thị sơ đồ các mặt xúc xắc trên màn hình.
  • Ứng dụng giả lập xúc xắc của bạn sẽ có giao diện người dùng (TUI) dựa trên văn bản tối thiểu nhưng thân thiện với người dùng, sẽ cho phép bạn chỉ định số lượng xúc xắc sáu mặt mà bạn muốn cuộn. Bạn sẽ sử dụng TUI này để lăn xúc xắc ở nhà mà không cần phải bay đến Las Vegas.
  • Ở đây, một mô tả về cách ứng dụng sẽ hoạt động trong nội bộ:
  • Nhiệm vụ để chạy
  • Công cụ để sử dụng
  • Mã để viết
  • Nhắc người dùng chọn số lượng xúc xắc sáu mặt, sau đó đọc đầu vào của người dùng
  • Chức năng
     1# dice.py
     2
     3def parse_input(input_string):
     4    """Return `input_string` as an integer between 1 and 6.
     5
     6    Check if `input_string` is an integer number between 1 and 6.
     7    If so, return an integer with the same value. Otherwise, tell
     8    the user to enter a valid number and quit the program.
     9    """
    10    if input_string.strip() in {"1", "2", "3", "4", "5", "6"}:
    11        return int(input_string)
    12    else:
    13        print("Please enter a number from 1 to 6.")
    14        raise SystemExit(1)
    15
    16# ~~~ App's main code block ~~~
    17# ...
    
    8 tích hợp của Python
  • Một cuộc gọi đến
     1# dice.py
     2
     3def parse_input(input_string):
     4    """Return `input_string` as an integer between 1 and 6.
     5
     6    Check if `input_string` is an integer number between 1 and 6.
     7    If so, return an integer with the same value. Otherwise, tell
     8    the user to enter a valid number and quit the program.
     9    """
    10    if input_string.strip() in {"1", "2", "3", "4", "5", "6"}:
    11        return int(input_string)
    12    else:
    13        print("Please enter a number from 1 to 6.")
    14        raise SystemExit(1)
    15
    16# ~~~ App's main code block ~~~
    17# ...
    
    8 với các đối số thích hợp

Nếu bạn không có tất cả các kiến ​​thức điều kiện tiên quyết trước khi bắt đầu cuộc phiêu lưu mã hóa này, thì đó là điều không ổn! Bạn có thể tìm hiểu thêm bằng cách tiếp tục và bắt đầu! 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 ở đây nếu bạn bị mắc kẹt.

Bước 1: Mã TUI của ứng dụng Rolling Dice Python của bạn

Trong bước này, bạn sẽ viết mã cần thiết để yêu cầu đầu vào của người dùng về số lượng xúc xắc họ muốn cuộn trong mô phỏng. Bạn cũng sẽ mã hóa một hàm Python lấy đầu vào của người dùng, xác nhận nó và trả về nó dưới dạng số nguyên nếu xác thực thành công. Nếu không, chức năng sẽ yêu cầu lại đầu vào của người dùng.

Để tải xuống mã cho bước này, nhấp vào liên kết sau và điều hướng đến thư mục

 1# dice.py
 2
 3def parse_input(input_string):
 4    """Return `input_string` as an integer between 1 and 6.
 5
 6    Check if `input_string` is an integer number between 1 and 6.
 7    If so, return an integer with the same value. Otherwise, tell
 8    the user to enter a valid number and quit the program.
 9    """
10    if input_string.strip() in {"1", "2", "3", "4", "5", "6"}:
11        return int(input_string)
12    else:
13        print("Please enter a number from 1 to 6.")
14        raise SystemExit(1)
15
16# ~~~ App's main code block ~~~
17# ...
25:

Lấy đầu vào của người dùng tại dòng lệnh

Để làm cho tay của bạn bị bẩn, bạn có thể bắt đầu viết mã tương tác với người dùng. Mã này sẽ cung cấp giao diện dựa trên văn bản ứng dụng và sẽ dựa vào

 1# dice.py
 2
 3def parse_input(input_string):
 4    """Return `input_string` as an integer between 1 and 6.
 5
 6    Check if `input_string` is an integer number between 1 and 6.
 7    If so, return an integer with the same value. Otherwise, tell
 8    the user to enter a valid number and quit the program.
 9    """
10    if input_string.strip() in {"1", "2", "3", "4", "5", "6"}:
11        return int(input_string)
12    else:
13        print("Please enter a number from 1 to 6.")
14        raise SystemExit(1)
15
16# ~~~ App's main code block ~~~
17# ...
8. Hàm tích hợp này đọc đầu vào người dùng từ dòng lệnh. Đối số
 1# dice.py
 2
 3def parse_input(input_string):
 4    """Return `input_string` as an integer between 1 and 6.
 5
 6    Check if `input_string` is an integer number between 1 and 6.
 7    If so, return an integer with the same value. Otherwise, tell
 8    the user to enter a valid number and quit the program.
 9    """
10    if input_string.strip() in {"1", "2", "3", "4", "5", "6"}:
11        return int(input_string)
12    else:
13        print("Please enter a number from 1 to 6.")
14        raise SystemExit(1)
15
16# ~~~ App's main code block ~~~
17# ...
27 của nó cho phép bạn vượt qua mô tả về loại đầu vào bạn cần.text-based interface and will rely on
 1# dice.py
 2
 3def parse_input(input_string):
 4    """Return `input_string` as an integer between 1 and 6.
 5
 6    Check if `input_string` is an integer number between 1 and 6.
 7    If so, return an integer with the same value. Otherwise, tell
 8    the user to enter a valid number and quit the program.
 9    """
10    if input_string.strip() in {"1", "2", "3", "4", "5", "6"}:
11        return int(input_string)
12    else:
13        print("Please enter a number from 1 to 6.")
14        raise SystemExit(1)
15
16# ~~~ App's main code block ~~~
17# ...
8. This built-in function reads the user input from the command line. Its
 1# dice.py
 2
 3def parse_input(input_string):
 4    """Return `input_string` as an integer between 1 and 6.
 5
 6    Check if `input_string` is an integer number between 1 and 6.
 7    If so, return an integer with the same value. Otherwise, tell
 8    the user to enter a valid number and quit the program.
 9    """
10    if input_string.strip() in {"1", "2", "3", "4", "5", "6"}:
11        return int(input_string)
12    else:
13        print("Please enter a number from 1 to 6.")
14        raise SystemExit(1)
15
16# ~~~ App's main code block ~~~
17# ...
27 argument allows you to pass a description of what type of input you need.text-based interface and will rely on
 1# dice.py
 2
 3def parse_input(input_string):
 4    """Return `input_string` as an integer between 1 and 6.
 5
 6    Check if `input_string` is an integer number between 1 and 6.
 7    If so, return an integer with the same value. Otherwise, tell
 8    the user to enter a valid number and quit the program.
 9    """
10    if input_string.strip() in {"1", "2", "3", "4", "5", "6"}:
11        return int(input_string)
12    else:
13        print("Please enter a number from 1 to 6.")
14        raise SystemExit(1)
15
16# ~~~ App's main code block ~~~
17# ...
8. This built-in function reads the user input from the command line. Its
 1# dice.py
 2
 3def parse_input(input_string):
 4    """Return `input_string` as an integer between 1 and 6.
 5
 6    Check if `input_string` is an integer number between 1 and 6.
 7    If so, return an integer with the same value. Otherwise, tell
 8    the user to enter a valid number and quit the program.
 9    """
10    if input_string.strip() in {"1", "2", "3", "4", "5", "6"}:
11        return int(input_string)
12    else:
13        print("Please enter a number from 1 to 6.")
14        raise SystemExit(1)
15
16# ~~~ App's main code block ~~~
17# ...
27 argument allows you to pass a description of what type of input you need.

Bắn trình soạn thảo hoặc IDE yêu thích của bạn và nhập mã sau vào tệp

 1# dice.py
 2
 3def parse_input(input_string):
 4    """Return `input_string` as an integer between 1 and 6.
 5
 6    Check if `input_string` is an integer number between 1 and 6.
 7    If so, return an integer with the same value. Otherwise, tell
 8    the user to enter a valid number and quit the program.
 9    """
10    if input_string.strip() in {"1", "2", "3", "4", "5", "6"}:
11        return int(input_string)
12    else:
13        print("Please enter a number from 1 to 6.")
14        raise SystemExit(1)
15
16# ~~~ App's main code block ~~~
17# ...
22 của bạn:
 1# dice.py
 2
 3def parse_input(input_string):
 4    """Return `input_string` as an integer between 1 and 6.
 5
 6    Check if `input_string` is an integer number between 1 and 6.
 7    If so, return an integer with the same value. Otherwise, tell
 8    the user to enter a valid number and quit the program.
 9    """
10    if input_string.strip() in {"1", "2", "3", "4", "5", "6"}:
11        return int(input_string)
12    else:
13        print("Please enter a number from 1 to 6.")
14        raise SystemExit(1)
15
16# ~~~ App's main code block ~~~
17# ...
1

Cuộc gọi của bạn đến

 1# dice.py
 2
 3def parse_input(input_string):
 4    """Return `input_string` as an integer between 1 and 6.
 5
 6    Check if `input_string` is an integer number between 1 and 6.
 7    If so, return an integer with the same value. Otherwise, tell
 8    the user to enter a valid number and quit the program.
 9    """
10    if input_string.strip() in {"1", "2", "3", "4", "5", "6"}:
11        return int(input_string)
12    else:
13        print("Please enter a number from 1 to 6.")
14        raise SystemExit(1)
15
16# ~~~ App's main code block ~~~
17# ...
8 trên dòng 5 hiển thị lời nhắc hỏi người dùng muốn lăn bao nhiêu con xúc xắc. Số lượng phải rơi trong khoảng từ 1 đến 6, bao gồm, như lời nhắc gợi ý.

Dòng 6 gọi

$ python dice.py
How many dice do you want to roll? [1-6] 3

$ python dice.py
How many dice do you want to roll? [1-6] 7
Please enter a number from 1 to 6.
3 và lưu trữ giá trị trả về trong
 1# dice.py
 2
 3def parse_input(input_string):
 4    """Return `input_string` as an integer between 1 and 6.
 5
 6    Check if `input_string` is an integer number between 1 and 6.
 7    If so, return an integer with the same value. Otherwise, tell
 8    the user to enter a valid number and quit the program.
 9    """
10    if input_string.strip() in {"1", "2", "3", "4", "5", "6"}:
11        return int(input_string)
12    else:
13        print("Please enter a number from 1 to 6.")
14        raise SystemExit(1)
15
16# ~~~ App's main code block ~~~
17# ...
41. Trong phần sau, bạn sẽ thực hiện chức năng này.

Phân tích cú pháp và xác nhận đầu vào của người dùng

Công việc của

$ python dice.py
How many dice do you want to roll? [1-6] 3

$ python dice.py
How many dice do you want to roll? [1-6] 7
Please enter a number from 1 to 6.
3 là lấy đầu vào của người dùng làm chuỗi, hãy kiểm tra xem nó có số nguyên hợp lệ và trả về nó dưới dạng đối tượng Python
 1# dice.py
 2
 3def parse_input(input_string):
 4    """Return `input_string` as an integer between 1 and 6.
 5
 6    Check if `input_string` is an integer number between 1 and 6.
 7    If so, return an integer with the same value. Otherwise, tell
 8    the user to enter a valid number and quit the program.
 9    """
10    if input_string.strip() in {"1", "2", "3", "4", "5", "6"}:
11        return int(input_string)
12    else:
13        print("Please enter a number from 1 to 6.")
14        raise SystemExit(1)
15
16# ~~~ App's main code block ~~~
17# ...
43 không. Đi trước và thêm phần sau vào tệp
 1# dice.py
 2
 3def parse_input(input_string):
 4    """Return `input_string` as an integer between 1 and 6.
 5
 6    Check if `input_string` is an integer number between 1 and 6.
 7    If so, return an integer with the same value. Otherwise, tell
 8    the user to enter a valid number and quit the program.
 9    """
10    if input_string.strip() in {"1", "2", "3", "4", "5", "6"}:
11        return int(input_string)
12    else:
13        print("Please enter a number from 1 to 6.")
14        raise SystemExit(1)
15
16# ~~~ App's main code block ~~~
17# ...
22 của bạn, ngay trước mã chính của ứng dụng:
 1# dice.py
 2
 3def parse_input(input_string):
 4    """Return `input_string` as an integer between 1 and 6.
 5
 6    Check if `input_string` is an integer number between 1 and 6.
 7    If so, return an integer with the same value. Otherwise, tell
 8    the user to enter a valid number and quit the program.
 9    """
10    if input_string.strip() in {"1", "2", "3", "4", "5", "6"}:
11        return int(input_string)
12    else:
13        print("Please enter a number from 1 to 6.")
14        raise SystemExit(1)
15
16# ~~~ App's main code block ~~~
17# ...

Ở đây, cách thức mã này hoạt động từng dòng:

  • Dòng 3 xác định

     1# dice.py
     2
     3def parse_input(input_string):
     4    """Return `input_string` as an integer between 1 and 6.
     5
     6    Check if `input_string` is an integer number between 1 and 6.
     7    If so, return an integer with the same value. Otherwise, tell
     8    the user to enter a valid number and quit the program.
     9    """
    10    if input_string.strip() in {"1", "2", "3", "4", "5", "6"}:
    11        return int(input_string)
    12    else:
    13        print("Please enter a number from 1 to 6.")
    14        raise SystemExit(1)
    15
    16# ~~~ App's main code block ~~~
    17# ...
    
    93, lấy chuỗi đầu vào làm đối số. defines 3, lấy chuỗi đầu vào làm đối số. defines

    $ python dice.py
    How many dice do you want to roll? [1-6] 3
    
    $ python dice.py
    How many dice do you want to roll? [1-6] 7
    Please enter a number from 1 to 6.
    
    3, which takes the input string as an argument.
  • Các dòng 4 đến 9 cung cấp chức năng DocString. Bao gồm một tài liệu thông tin và được định dạng tốt trong các chức năng của bạn là một thực tiễn tốt nhất trong lập trình Python vì các tài liệu cho phép bạn ghi lại mã của mình. provide the function’s docstring. Including an informative and well-formatted docstring in your functions is a best practice in Python programming because docstrings allow you to document your code. provide the function’s docstring. Including an informative and well-formatted docstring in your functions is a best practice in Python programming because docstrings allow you to document your code.

  • Dòng 10 kiểm tra xem đầu vào của người dùng có phải là giá trị hợp lệ cho số lượng xúc xắc để cuộn không. Cuộc gọi đến

     1# dice.py
     2
     3def parse_input(input_string):
     4    """Return `input_string` as an integer between 1 and 6.
     5
     6    Check if `input_string` is an integer number between 1 and 6.
     7    If so, return an integer with the same value. Otherwise, tell
     8    the user to enter a valid number and quit the program.
     9    """
    10    if input_string.strip() in {"1", "2", "3", "4", "5", "6"}:
    11        return int(input_string)
    12    else:
    13        print("Please enter a number from 1 to 6.")
    14        raise SystemExit(1)
    15
    16# ~~~ App's main code block ~~~
    17# ...
    
    16 sẽ loại bỏ bất kỳ khoảng trống không mong muốn xung quanh chuỗi đầu vào. Toán tử
     1# dice.py
     2
     3def parse_input(input_string):
     4    """Return `input_string` as an integer between 1 and 6.
     5
     6    Check if `input_string` is an integer number between 1 and 6.
     7    If so, return an integer with the same value. Otherwise, tell
     8    the user to enter a valid number and quit the program.
     9    """
    10    if input_string.strip() in {"1", "2", "3", "4", "5", "6"}:
    11        return int(input_string)
    12    else:
    13        print("Please enter a number from 1 to 6.")
    14        raise SystemExit(1)
    15
    16# ~~~ App's main code block ~~~
    17# ...
    
    47 kiểm tra xem đầu vào có nằm trong tập hợp các số lượng xúc xắc được phép cuộn không. Trong trường hợp này, bạn sử dụng một tập hợp vì các thử nghiệm thành viên trong cấu trúc dữ liệu Python này khá hiệu quả. checks if the user input is a valid value for the number of dice to roll. The call to 6 sẽ loại bỏ bất kỳ khoảng trống không mong muốn xung quanh chuỗi đầu vào. Toán tử
     1# dice.py
     2
     3def parse_input(input_string):
     4    """Return `input_string` as an integer between 1 and 6.
     5
     6    Check if `input_string` is an integer number between 1 and 6.
     7    If so, return an integer with the same value. Otherwise, tell
     8    the user to enter a valid number and quit the program.
     9    """
    10    if input_string.strip() in {"1", "2", "3", "4", "5", "6"}:
    11        return int(input_string)
    12    else:
    13        print("Please enter a number from 1 to 6.")
    14        raise SystemExit(1)
    15
    16# ~~~ App's main code block ~~~
    17# ...
    
    47 kiểm tra xem đầu vào có nằm trong tập hợp các số lượng xúc xắc được phép cuộn không. Trong trường hợp này, bạn sử dụng một tập hợp vì các thử nghiệm thành viên trong cấu trúc dữ liệu Python này khá hiệu quả.
    checks if the user input is a valid value for the number of dice to roll. The call to

     1# dice.py
     2
     3def parse_input(input_string):
     4    """Return `input_string` as an integer between 1 and 6.
     5
     6    Check if `input_string` is an integer number between 1 and 6.
     7    If so, return an integer with the same value. Otherwise, tell
     8    the user to enter a valid number and quit the program.
     9    """
    10    if input_string.strip() in {"1", "2", "3", "4", "5", "6"}:
    11        return int(input_string)
    12    else:
    13        print("Please enter a number from 1 to 6.")
    14        raise SystemExit(1)
    15
    16# ~~~ App's main code block ~~~
    17# ...
    
    46 removes any unwanted spaces around the input string. The
     1# dice.py
     2
     3def parse_input(input_string):
     4    """Return `input_string` as an integer between 1 and 6.
     5
     6    Check if `input_string` is an integer number between 1 and 6.
     7    If so, return an integer with the same value. Otherwise, tell
     8    the user to enter a valid number and quit the program.
     9    """
    10    if input_string.strip() in {"1", "2", "3", "4", "5", "6"}:
    11        return int(input_string)
    12    else:
    13        print("Please enter a number from 1 to 6.")
    14        raise SystemExit(1)
    15
    16# ~~~ App's main code block ~~~
    17# ...
    
    47 operator checks if the input falls within the set of allowed numbers of dice to roll. In this case, you use a set because membership tests in this Python data structure are quite efficient.membership tests in this Python data structure are quite efficient.
  • Dòng 11 chuyển đổi đầu vào thành số nguyên và trả lại cho người gọi. converts the input into an integer number and returns it to the caller. converts the input into an integer number and returns it to the caller.

  • Dòng 13 In một thông báo lên màn hình để cảnh báo người dùng đầu vào không hợp lệ, nếu có. prints a message to the screen to alert the user of invalid input, if applicable. prints a message to the screen to alert the user of invalid input, if applicable.

  • Dòng 14 thoát khỏi ứng dụng với ngoại lệ

     1# dice.py
     2
     3def parse_input(input_string):
     4    """Return `input_string` as an integer between 1 and 6.
     5
     6    Check if `input_string` is an integer number between 1 and 6.
     7    If so, return an integer with the same value. Otherwise, tell
     8    the user to enter a valid number and quit the program.
     9    """
    10    if input_string.strip() in {"1", "2", "3", "4", "5", "6"}:
    11        return int(input_string)
    12    else:
    13        print("Please enter a number from 1 to 6.")
    14        raise SystemExit(1)
    15
    16# ~~~ App's main code block ~~~
    17# ...
    
    18 và mã trạng thái là
     1# dice.py
     2
     3def parse_input(input_string):
     4    """Return `input_string` as an integer between 1 and 6.
     5
     6    Check if `input_string` is an integer number between 1 and 6.
     7    If so, return an integer with the same value. Otherwise, tell
     8    the user to enter a valid number and quit the program.
     9    """
    10    if input_string.strip() in {"1", "2", "3", "4", "5", "6"}:
    11        return int(input_string)
    12    else:
    13        print("Please enter a number from 1 to 6.")
    14        raise SystemExit(1)
    15
    16# ~~~ App's main code block ~~~
    17# ...
    
    49 để báo hiệu rằng có điều gì đó không ổn. exits the app with a 8 và mã trạng thái là
     1# dice.py
     2
     3def parse_input(input_string):
     4    """Return `input_string` as an integer between 1 and 6.
     5
     6    Check if `input_string` is an integer number between 1 and 6.
     7    If so, return an integer with the same value. Otherwise, tell
     8    the user to enter a valid number and quit the program.
     9    """
    10    if input_string.strip() in {"1", "2", "3", "4", "5", "6"}:
    11        return int(input_string)
    12    else:
    13        print("Please enter a number from 1 to 6.")
    14        raise SystemExit(1)
    15
    16# ~~~ App's main code block ~~~
    17# ...
    
    49 để báo hiệu rằng có điều gì đó không ổn.
    exits the app with a

     1# dice.py
     2
     3def parse_input(input_string):
     4    """Return `input_string` as an integer between 1 and 6.
     5
     6    Check if `input_string` is an integer number between 1 and 6.
     7    If so, return an integer with the same value. Otherwise, tell
     8    the user to enter a valid number and quit the program.
     9    """
    10    if input_string.strip() in {"1", "2", "3", "4", "5", "6"}:
    11        return int(input_string)
    12    else:
    13        print("Please enter a number from 1 to 6.")
    14        raise SystemExit(1)
    15
    16# ~~~ App's main code block ~~~
    17# ...
    
    48 exception and a status code of
     1# dice.py
     2
     3def parse_input(input_string):
     4    """Return `input_string` as an integer between 1 and 6.
     5
     6    Check if `input_string` is an integer number between 1 and 6.
     7    If so, return an integer with the same value. Otherwise, tell
     8    the user to enter a valid number and quit the program.
     9    """
    10    if input_string.strip() in {"1", "2", "3", "4", "5", "6"}:
    11        return int(input_string)
    12    else:
    13        print("Please enter a number from 1 to 6.")
    14        raise SystemExit(1)
    15
    16# ~~~ App's main code block ~~~
    17# ...
    
    49 to signal that something went wrong.

Với

$ python dice.py
How many dice do you want to roll? [1-6] 3

$ python dice.py
How many dice do you want to roll? [1-6] 7
Please enter a number from 1 to 6.
3, bạn xử lý và xác nhận đầu vào của người dùng tại dòng lệnh. Xác thực bất kỳ đầu vào nào đến trực tiếp từ người dùng hoặc bất kỳ nguồn không tin cậy nào là chìa khóa để ứng dụng của bạn hoạt động một cách đáng tin cậy và an toàn.

Bây giờ bạn có TUI thân thiện với người dùng và cơ chế xác thực đầu vào thích hợp, bạn cần đảm bảo rằng các chức năng này hoạt động chính xác. Đó là những gì bạn sẽ làm trong phần sau.

Hãy thử ứng dụng Rolling Dice

Để thử mã mà bạn đã viết cho đến nay, hãy mở một cửa sổ dòng lệnh và chạy tập lệnh

 1# dice.py
 2
 3def parse_input(input_string):
 4    """Return `input_string` as an integer between 1 and 6.
 5
 6    Check if `input_string` is an integer number between 1 and 6.
 7    If so, return an integer with the same value. Otherwise, tell
 8    the user to enter a valid number and quit the program.
 9    """
10    if input_string.strip() in {"1", "2", "3", "4", "5", "6"}:
11        return int(input_string)
12    else:
13        print("Please enter a number from 1 to 6.")
14        raise SystemExit(1)
15
16# ~~~ App's main code block ~~~
17# ...
22 của bạn:
$ python dice.py
How many dice do you want to roll? [1-6] 3

$ python dice.py
How many dice do you want to roll? [1-6] 7
Please enter a number from 1 to 6.

Nếu bạn nhập một số nguyên từ 1 đến 6, thì mã không hiển thị thông báo. Mặt khác, nếu đầu vào không phải là số nguyên hợp lệ hoặc nằm ngoài khoảng mục tiêu, thì bạn sẽ nhận được một thông báo cho bạn biết rằng một số nguyên từ 1 đến 6 là bắt buộc.

Cho đến thời điểm này, bạn đã viết thành công mã để yêu cầu và phân tích đầu vào của người dùng tại dòng lệnh. Mã này cung cấp ứng dụng TUI TUI, dựa trên chức năng

 1# dice.py
 2
 3def parse_input(input_string):
 4    """Return `input_string` as an integer between 1 and 6.
 5
 6    Check if `input_string` is an integer number between 1 and 6.
 7    If so, return an integer with the same value. Otherwise, tell
 8    the user to enter a valid number and quit the program.
 9    """
10    if input_string.strip() in {"1", "2", "3", "4", "5", "6"}:
11        return int(input_string)
12    else:
13        print("Please enter a number from 1 to 6.")
14        raise SystemExit(1)
15
16# ~~~ App's main code block ~~~
17# ...
8 tích hợp. Bạn cũng đã mã hóa một chức năng để xác nhận đầu vào của người dùng và trả về nó dưới dạng số nguyên. Bây giờ, nó thời gian để lăn xúc xắc!

Bước 2: Mô phỏng việc lăn xúc xắc sáu mặt trong Python

Ứng dụng lăn xúc xắc của bạn hiện cung cấp TUI để lấy đầu vào của người dùng và xử lý nó. Tuyệt quá! Để tiếp tục xây dựng chức năng chính của ứng dụng, bạn sẽ viết chức năng

$ python dice.py
How many dice do you want to roll? [1-6] 3

$ python dice.py
How many dice do you want to roll? [1-6] 7
Please enter a number from 1 to 6.
6, điều này sẽ cho phép bạn mô phỏng một sự kiện lăn xúc xắc. Chức năng này sẽ lấy số xúc xắc mà người dùng muốn lăn.

Mô-đun Python từ

 1# dice.py
 2
 3def parse_input(input_string):
 4    """Return `input_string` as an integer between 1 and 6.
 5
 6    Check if `input_string` is an integer number between 1 and 6.
 7    If so, return an integer with the same value. Otherwise, tell
 8    the user to enter a valid number and quit the program.
 9    """
10    if input_string.strip() in {"1", "2", "3", "4", "5", "6"}:
11        return int(input_string)
12    else:
13        print("Please enter a number from 1 to 6.")
14        raise SystemExit(1)
15
16# ~~~ App's main code block ~~~
17# ...
6 từ thư viện tiêu chuẩn cung cấp chức năng
$ python dice.py
How many dice do you want to roll? [1-6] 3

$ python dice.py
How many dice do you want to roll? [1-6] 7
Please enter a number from 1 to 6.
5, tạo ra các số nguyên giả ngẫu nhiên trong một khoảng thời gian nhất định. Bạn sẽ tận dụng chức năng này để mô phỏng xúc xắc lăn.

Để tải xuống mã cho bước này, nhấp vào liên kết sau và xem bên trong thư mục

 1# dice.py
 2
 3def parse_input(input_string):
 4    """Return `input_string` as an integer between 1 and 6.
 5
 6    Check if `input_string` is an integer number between 1 and 6.
 7    If so, return an integer with the same value. Otherwise, tell
 8    the user to enter a valid number and quit the program.
 9    """
10    if input_string.strip() in {"1", "2", "3", "4", "5", "6"}:
11        return int(input_string)
12    else:
13        print("Please enter a number from 1 to 6.")
14        raise SystemExit(1)
15
16# ~~~ App's main code block ~~~
17# ...
66:

Tại đây, mã thực hiện

$ python dice.py
How many dice do you want to roll? [1-6] 3

$ python dice.py
How many dice do you want to roll? [1-6] 7
Please enter a number from 1 to 6.
6:
 1# dice.py
 2
 3def parse_input(input_string):
 4    """Return `input_string` as an integer between 1 and 6.
 5
 6    Check if `input_string` is an integer number between 1 and 6.
 7    If so, return an integer with the same value. Otherwise, tell
 8    the user to enter a valid number and quit the program.
 9    """
10    if input_string.strip() in {"1", "2", "3", "4", "5", "6"}:
11        return int(input_string)
12    else:
13        print("Please enter a number from 1 to 6.")
14        raise SystemExit(1)
15
16# ~~~ App's main code block ~~~
17# ...
2

Trong đoạn mã này, dòng 2 nhập

 1# dice.py
 2
 3def parse_input(input_string):
 4    """Return `input_string` as an integer between 1 and 6.
 5
 6    Check if `input_string` is an integer number between 1 and 6.
 7    If so, return an integer with the same value. Otherwise, tell
 8    the user to enter a valid number and quit the program.
 9    """
10    if input_string.strip() in {"1", "2", "3", "4", "5", "6"}:
11        return int(input_string)
12    else:
13        print("Please enter a number from 1 to 6.")
14        raise SystemExit(1)
15
16# ~~~ App's main code block ~~~
17# ...
6 vào không gian tên hiện tại của bạn. Nhập này cho phép bạn truy cập chức năng
$ python dice.py
How many dice do you want to roll? [1-6] 3

$ python dice.py
How many dice do you want to roll? [1-6] 7
Please enter a number from 1 to 6.
5 sau. Ở đây, một sự cố của phần còn lại của mã:
  • Dòng 6 xác định

     1# dice.py
     2
     3def parse_input(input_string):
     4    """Return `input_string` as an integer between 1 and 6.
     5
     6    Check if `input_string` is an integer number between 1 and 6.
     7    If so, return an integer with the same value. Otherwise, tell
     8    the user to enter a valid number and quit the program.
     9    """
    10    if input_string.strip() in {"1", "2", "3", "4", "5", "6"}:
    11        return int(input_string)
    12    else:
    13        print("Please enter a number from 1 to 6.")
    14        raise SystemExit(1)
    15
    16# ~~~ App's main code block ~~~
    17# ...
    
    96, có một đối số đại diện cho số lượng xúc xắc để lăn trong một cuộc gọi nhất định. defines 6, có một đối số đại diện cho số lượng xúc xắc để lăn trong một cuộc gọi nhất định. defines

    $ python dice.py
    How many dice do you want to roll? [1-6] 3
    
    $ python dice.py
    How many dice do you want to roll? [1-6] 7
    Please enter a number from 1 to 6.
    
    6, which takes an argument representing the number of dice to roll in a given call.
  • Các dòng 7 đến 11 cung cấp chức năng DocString. provide the function’s docstring. provide the function’s docstring.

  • Dòng 12 tạo ra một danh sách trống,

     1# dice.py
     2
     3def parse_input(input_string):
     4    """Return `input_string` as an integer between 1 and 6.
     5
     6    Check if `input_string` is an integer number between 1 and 6.
     7    If so, return an integer with the same value. Otherwise, tell
     8    the user to enter a valid number and quit the program.
     9    """
    10    if input_string.strip() in {"1", "2", "3", "4", "5", "6"}:
    11        return int(input_string)
    12    else:
    13        print("Please enter a number from 1 to 6.")
    14        raise SystemExit(1)
    15
    16# ~~~ App's main code block ~~~
    17# ...
    
    31, để lưu trữ kết quả của mô phỏng xúc xắc. creates an empty list, 1, để lưu trữ kết quả của mô phỏng xúc xắc. creates an empty list,

     1# dice.py
     2
     3def parse_input(input_string):
     4    """Return `input_string` as an integer between 1 and 6.
     5
     6    Check if `input_string` is an integer number between 1 and 6.
     7    If so, return an integer with the same value. Otherwise, tell
     8    the user to enter a valid number and quit the program.
     9    """
    10    if input_string.strip() in {"1", "2", "3", "4", "5", "6"}:
    11        return int(input_string)
    12    else:
    13        print("Please enter a number from 1 to 6.")
    14        raise SystemExit(1)
    15
    16# ~~~ App's main code block ~~~
    17# ...
    
    41, to store the results of the dice-rolling simulation.
  • Dòng 13 xác định vòng lặp

     1# dice.py
     2
     3def parse_input(input_string):
     4    """Return `input_string` as an integer between 1 and 6.
     5
     6    Check if `input_string` is an integer number between 1 and 6.
     7    If so, return an integer with the same value. Otherwise, tell
     8    the user to enter a valid number and quit the program.
     9    """
    10    if input_string.strip() in {"1", "2", "3", "4", "5", "6"}:
    11        return int(input_string)
    12    else:
    13        print("Please enter a number from 1 to 6.")
    14        raise SystemExit(1)
    15
    16# ~~~ App's main code block ~~~
    17# ...
    
    54 mà lặp lại một lần cho mỗi lần chết mà người dùng muốn cuộn. defines a 4 mà lặp lại một lần cho mỗi lần chết mà người dùng muốn cuộn. defines a

     1# dice.py
     2
     3def parse_input(input_string):
     4    """Return `input_string` as an integer between 1 and 6.
     5
     6    Check if `input_string` is an integer number between 1 and 6.
     7    If so, return an integer with the same value. Otherwise, tell
     8    the user to enter a valid number and quit the program.
     9    """
    10    if input_string.strip() in {"1", "2", "3", "4", "5", "6"}:
    11        return int(input_string)
    12    else:
    13        print("Please enter a number from 1 to 6.")
    14        raise SystemExit(1)
    15
    16# ~~~ App's main code block ~~~
    17# ...
    
    4 loop that iterates once for each die that the user wants to roll.
  • Dòng 14 gọi

     1# dice.py
     2
     3def parse_input(input_string):
     4    """Return `input_string` as an integer between 1 and 6.
     5
     6    Check if `input_string` is an integer number between 1 and 6.
     7    If so, return an integer with the same value. Otherwise, tell
     8    the user to enter a valid number and quit the program.
     9    """
    10    if input_string.strip() in {"1", "2", "3", "4", "5", "6"}:
    11        return int(input_string)
    12    else:
    13        print("Please enter a number from 1 to 6.")
    14        raise SystemExit(1)
    15
    16# ~~~ App's main code block ~~~
    17# ...
    
    95 để tạo số số nguyên giả ngẫu nhiên từ 1 đến 6, bao gồm. Cuộc gọi này tạo ra một số duy nhất trên mỗi lần lặp. Con số này đại diện cho kết quả của việc lăn một cái chết sáu mặt. calls 5 để tạo số số nguyên giả ngẫu nhiên từ 1 đến 6, bao gồm. Cuộc gọi này tạo ra một số duy nhất trên mỗi lần lặp. Con số này đại diện cho kết quả của việc lăn một cái chết sáu mặt. calls

    $ python dice.py
    How many dice do you want to roll? [1-6] 3
    
    $ python dice.py
    How many dice do you want to roll? [1-6] 7
    Please enter a number from 1 to 6.
    
    5 to generate a pseudo-random integer number from 1 to 6, inclusive. This call generates a single number on each iteration. This number represents the result of rolling a six-sided die.
  • Dòng 15 cộng đồng kết quả lăn chết hiện tại trong

     1# dice.py
     2
     3def parse_input(input_string):
     4    """Return `input_string` as an integer between 1 and 6.
     5
     6    Check if `input_string` is an integer number between 1 and 6.
     7    If so, return an integer with the same value. Otherwise, tell
     8    the user to enter a valid number and quit the program.
     9    """
    10    if input_string.strip() in {"1", "2", "3", "4", "5", "6"}:
    11        return int(input_string)
    12    else:
    13        print("Please enter a number from 1 to 6.")
    14        raise SystemExit(1)
    15
    16# ~~~ App's main code block ~~~
    17# ...
    
    31. appends the current die-rolling result in 1. appends the current die-rolling result in

     1# dice.py
     2
     3def parse_input(input_string):
     4    """Return `input_string` as an integer between 1 and 6.
     5
     6    Check if `input_string` is an integer number between 1 and 6.
     7    If so, return an integer with the same value. Otherwise, tell
     8    the user to enter a valid number and quit the program.
     9    """
    10    if input_string.strip() in {"1", "2", "3", "4", "5", "6"}:
    11        return int(input_string)
    12    else:
    13        print("Please enter a number from 1 to 6.")
    14        raise SystemExit(1)
    15
    16# ~~~ App's main code block ~~~
    17# ...
    
    41.
  • Dòng 16 Trả về danh sách các kết quả mô phỏng xói con xúc xắc. returns the list of dice-rolling simulation results. returns the list of dice-rolling simulation results.

Để thử chức năng mới được tạo của bạn, hãy thêm các dòng mã sau vào cuối tệp

 1# dice.py
 2
 3def parse_input(input_string):
 4    """Return `input_string` as an integer between 1 and 6.
 5
 6    Check if `input_string` is an integer number between 1 and 6.
 7    If so, return an integer with the same value. Otherwise, tell
 8    the user to enter a valid number and quit the program.
 9    """
10    if input_string.strip() in {"1", "2", "3", "4", "5", "6"}:
11        return int(input_string)
12    else:
13        print("Please enter a number from 1 to 6.")
14        raise SystemExit(1)
15
16# ~~~ App's main code block ~~~
17# ...
22 của bạn:
 1# dice.py
 2
 3def parse_input(input_string):
 4    """Return `input_string` as an integer between 1 and 6.
 5
 6    Check if `input_string` is an integer number between 1 and 6.
 7    If so, return an integer with the same value. Otherwise, tell
 8    the user to enter a valid number and quit the program.
 9    """
10    if input_string.strip() in {"1", "2", "3", "4", "5", "6"}:
11        return int(input_string)
12    else:
13        print("Please enter a number from 1 to 6.")
14        raise SystemExit(1)
15
16# ~~~ App's main code block ~~~
17# ...
4

Trong mã này, dòng 9 gọi

$ python dice.py
How many dice do you want to roll? [1-6] 3

$ python dice.py
How many dice do you want to roll? [1-6] 7
Please enter a number from 1 to 6.
6 với
 1# dice.py
 2
 3def parse_input(input_string):
 4    """Return `input_string` as an integer between 1 and 6.
 5
 6    Check if `input_string` is an integer number between 1 and 6.
 7    If so, return an integer with the same value. Otherwise, tell
 8    the user to enter a valid number and quit the program.
 9    """
10    if input_string.strip() in {"1", "2", "3", "4", "5", "6"}:
11        return int(input_string)
12    else:
13        print("Please enter a number from 1 to 6.")
14        raise SystemExit(1)
15
16# ~~~ App's main code block ~~~
17# ...
41 như một đối số. Dòng 11 gọi
 1# dice.py
 2
 3def parse_input(input_string):
 4    """Return `input_string` as an integer between 1 and 6.
 5
 6    Check if `input_string` is an integer number between 1 and 6.
 7    If so, return an integer with the same value. Otherwise, tell
 8    the user to enter a valid number and quit the program.
 9    """
10    if input_string.strip() in {"1", "2", "3", "4", "5", "6"}:
11        return int(input_string)
12    else:
13        print("Please enter a number from 1 to 6.")
14        raise SystemExit(1)
15
16# ~~~ App's main code block ~~~
17# ...
5 để hiển thị kết quả dưới dạng danh sách các số trên màn hình của bạn. Mỗi số trong danh sách thể hiện kết quả cho một lần chết. Bạn có thể xóa dòng 11 sau khi kiểm tra mã của bạn.

Đi trước và chạy ứng dụng của bạn từ dòng lệnh:

 1# dice.py
 2
 3def parse_input(input_string):
 4    """Return `input_string` as an integer between 1 and 6.
 5
 6    Check if `input_string` is an integer number between 1 and 6.
 7    If so, return an integer with the same value. Otherwise, tell
 8    the user to enter a valid number and quit the program.
 9    """
10    if input_string.strip() in {"1", "2", "3", "4", "5", "6"}:
11        return int(input_string)
12    else:
13        print("Please enter a number from 1 to 6.")
14        raise SystemExit(1)
15
16# ~~~ App's main code block ~~~
17# ...
6

Danh sách kết quả trên màn hình của bạn sẽ khác vì bạn tạo ra các số giả ngẫu nhiên của riêng bạn. Trong ví dụ này, bạn mô phỏng lần lượt của năm và hai con xúc xắc. Giá trị của mỗi lần chết là từ 1 đến 6 vì bạn làm việc với xúc xắc sáu mặt.

Bây giờ, bạn đã viết và thử mã mô phỏng một sự kiện xua đuổi xúc xắc, đã đến lúc tiếp tục và cung cấp cho ứng dụng của bạn một cách hào nhoáng để hiển thị các kết quả này. Đó là những gì bạn sẽ làm trong phần tiếp theo.

Bước 3: Tạo và hiển thị sơ đồ ASCII của các mặt xúc xắc

Tại thời điểm này, ứng dụng của bạn đã mô phỏng việc lăn một số xúc xắc và lưu trữ kết quả dưới dạng danh sách các số. Tuy nhiên, một danh sách các con số không hấp dẫn từ quan điểm của người dùng. Bạn cần một đầu ra fancier để ứng dụng của bạn trông chuyên nghiệp.

Trong phần này, bạn sẽ viết mã để tạo sơ đồ hiển thị các mặt lên đến sáu con xúc xắc. Để làm điều này, bạn sẽ tạo ra một chút nghệ thuật ASCII.

Nhấp vào liên kết bên dưới để tải xuống mã cho bước này để bạn có thể theo dõi cùng với dự án. Bạn sẽ tìm thấy những gì bạn cần trong thư mục

 1# dice.py
 2
 3def parse_input(input_string):
 4    """Return `input_string` as an integer between 1 and 6.
 5
 6    Check if `input_string` is an integer number between 1 and 6.
 7    If so, return an integer with the same value. Otherwise, tell
 8    the user to enter a valid number and quit the program.
 9    """
10    if input_string.strip() in {"1", "2", "3", "4", "5", "6"}:
11        return int(input_string)
12    else:
13        print("Please enter a number from 1 to 6.")
14        raise SystemExit(1)
15
16# ~~~ App's main code block ~~~
17# ...
49:

Thiết lập sơ đồ mặt xúc xắc

Ứng dụng giả lập xúc xắc của bạn cần một cách để hiển thị kết quả của việc lăn xúc xắc. Để kết thúc này, bạn sẽ sử dụng sơ đồ ASCII của các mặt xúc xắc mà màllll cho thấy kết quả của việc lăn số con xúc xắc sáu mặt mong muốn. Ví dụ, sau khi lăn bốn con xúc xắc, sơ đồ sẽ trông giống như thế này:

 1# dice.py
 2
 3def parse_input(input_string):
 4    """Return `input_string` as an integer between 1 and 6.
 5
 6    Check if `input_string` is an integer number between 1 and 6.
 7    If so, return an integer with the same value. Otherwise, tell
 8    the user to enter a valid number and quit the program.
 9    """
10    if input_string.strip() in {"1", "2", "3", "4", "5", "6"}:
11        return int(input_string)
12    else:
13        print("Please enter a number from 1 to 6.")
14        raise SystemExit(1)
15
16# ~~~ App's main code block ~~~
17# ...
4

Mỗi mặt chết trong sơ đồ này phản ánh giá trị kết quả từ một lần lặp của mô phỏng. Để bắt đầu mã hóa chức năng để xây dựng sơ đồ này, bạn cần kết hợp một số nghệ thuật ASCII. Quay lại Trình chỉnh sửa mã của bạn và thêm thông tin sau:

 1# dice.py
 2
 3def parse_input(input_string):
 4    """Return `input_string` as an integer between 1 and 6.
 5
 6    Check if `input_string` is an integer number between 1 and 6.
 7    If so, return an integer with the same value. Otherwise, tell
 8    the user to enter a valid number and quit the program.
 9    """
10    if input_string.strip() in {"1", "2", "3", "4", "5", "6"}:
11        return int(input_string)
12    else:
13        print("Please enter a number from 1 to 6.")
14        raise SystemExit(1)
15
16# ~~~ App's main code block ~~~
17# ...
9

Trong dòng 4 đến 47, bạn vẽ sáu mặt xúc xắc bằng các ký tự ASCII. Bạn lưu trữ các khuôn mặt trong

 1# dice.py
 2
 3def parse_input(input_string):
 4    """Return `input_string` as an integer between 1 and 6.
 5
 6    Check if `input_string` is an integer number between 1 and 6.
 7    If so, return an integer with the same value. Otherwise, tell
 8    the user to enter a valid number and quit the program.
 9    """
10    if input_string.strip() in {"1", "2", "3", "4", "5", "6"}:
11        return int(input_string)
12    else:
13        print("Please enter a number from 1 to 6.")
14        raise SystemExit(1)
15
16# ~~~ App's main code block ~~~
17# ...
90, một từ điển ánh xạ từng mặt theo giá trị số nguyên tương ứng của nó.

Dòng 48 xác định

 1# dice.py
 2
 3def parse_input(input_string):
 4    """Return `input_string` as an integer between 1 and 6.
 5
 6    Check if `input_string` is an integer number between 1 and 6.
 7    If so, return an integer with the same value. Otherwise, tell
 8    the user to enter a valid number and quit the program.
 9    """
10    if input_string.strip() in {"1", "2", "3", "4", "5", "6"}:
11        return int(input_string)
12    else:
13        print("Please enter a number from 1 to 6.")
14        raise SystemExit(1)
15
16# ~~~ App's main code block ~~~
17# ...
91, giữ số lượng hàng mà một khuôn mặt nhất định sẽ chiếm. Trong ví dụ này, mỗi khuôn mặt chiếm năm hàng. Tương tự, dòng 49 xác định
 1# dice.py
 2
 3def parse_input(input_string):
 4    """Return `input_string` as an integer between 1 and 6.
 5
 6    Check if `input_string` is an integer number between 1 and 6.
 7    If so, return an integer with the same value. Otherwise, tell
 8    the user to enter a valid number and quit the program.
 9    """
10    if input_string.strip() in {"1", "2", "3", "4", "5", "6"}:
11        return int(input_string)
12    else:
13        print("Please enter a number from 1 to 6.")
14        raise SystemExit(1)
15
16# ~~~ App's main code block ~~~
17# ...
92 để giữ số lượng cột cần thiết để vẽ mặt chết. Trong ví dụ này, chiều rộng là 11 ký tự.

Cuối cùng, dòng 50 xác định

 1# dice.py
 2
 3def parse_input(input_string):
 4    """Return `input_string` as an integer between 1 and 6.
 5
 6    Check if `input_string` is an integer number between 1 and 6.
 7    If so, return an integer with the same value. Otherwise, tell
 8    the user to enter a valid number and quit the program.
 9    """
10    if input_string.strip() in {"1", "2", "3", "4", "5", "6"}:
11        return int(input_string)
12    else:
13        print("Please enter a number from 1 to 6.")
14        raise SystemExit(1)
15
16# ~~~ App's main code block ~~~
17# ...
93, giữ một ký tự khoảng trắng. Bạn sẽ sử dụng tất cả các hằng số này để tạo và hiển thị sơ đồ ASCII của các mặt xúc xắc cho ứng dụng của bạn.

Tạo sơ đồ mặt xúc xắc

Tại thời điểm này, bạn đã xây dựng nghệ thuật ASCII cho mỗi khuôn mặt chết. Để đặt các mảnh này lại với nhau thành một sơ đồ cuối cùng thể hiện kết quả hoàn chỉnh của mô phỏng xỏ xúc xắc, bạn sẽ viết một chức năng tùy chỉnh khác:

$ python dice.py
How many dice do you want to roll? [1-6] 3

$ python dice.py
How many dice do you want to roll? [1-6] 7
Please enter a number from 1 to 6.
4

Chức năng này làm như sau:

  • Dòng 5 xác định

     1# dice.py
     2
     3def parse_input(input_string):
     4    """Return `input_string` as an integer between 1 and 6.
     5
     6    Check if `input_string` is an integer number between 1 and 6.
     7    If so, return an integer with the same value. Otherwise, tell
     8    the user to enter a valid number and quit the program.
     9    """
    10    if input_string.strip() in {"1", "2", "3", "4", "5", "6"}:
    11        return int(input_string)
    12    else:
    13        print("Please enter a number from 1 to 6.")
    14        raise SystemExit(1)
    15
    16# ~~~ App's main code block ~~~
    17# ...
    
    99 với một đối số duy nhất gọi là
     1# dice.py
     2
     3def parse_input(input_string):
     4    """Return `input_string` as an integer between 1 and 6.
     5
     6    Check if `input_string` is an integer number between 1 and 6.
     7    If so, return an integer with the same value. Otherwise, tell
     8    the user to enter a valid number and quit the program.
     9    """
    10    if input_string.strip() in {"1", "2", "3", "4", "5", "6"}:
    11        return int(input_string)
    12    else:
    13        print("Please enter a number from 1 to 6.")
    14        raise SystemExit(1)
    15
    16# ~~~ App's main code block ~~~
    17# ...
    
    95. Đối số này sẽ giữ danh sách các giá trị số nguyên cuộn xúc xắc xuất phát từ việc gọi
    $ python dice.py
    How many dice do you want to roll? [1-6] 3
    
    $ python dice.py
    How many dice do you want to roll? [1-6] 7
    Please enter a number from 1 to 6.
    
    6. defines 9 với một đối số duy nhất gọi là
     1# dice.py
     2
     3def parse_input(input_string):
     4    """Return `input_string` as an integer between 1 and 6.
     5
     6    Check if `input_string` is an integer number between 1 and 6.
     7    If so, return an integer with the same value. Otherwise, tell
     8    the user to enter a valid number and quit the program.
     9    """
    10    if input_string.strip() in {"1", "2", "3", "4", "5", "6"}:
    11        return int(input_string)
    12    else:
    13        print("Please enter a number from 1 to 6.")
    14        raise SystemExit(1)
    15
    16# ~~~ App's main code block ~~~
    17# ...
    
    95. Đối số này sẽ giữ danh sách các giá trị số nguyên cuộn xúc xắc xuất phát từ việc gọi
    $ python dice.py
    How many dice do you want to roll? [1-6] 3
    
    $ python dice.py
    How many dice do you want to roll? [1-6] 7
    Please enter a number from 1 to 6.
    
    6.
    defines

    $ python dice.py
    How many dice do you want to roll? [1-6] 3
    
    $ python dice.py
    How many dice do you want to roll? [1-6] 7
    Please enter a number from 1 to 6.
    
    9 with a single argument called
     1# dice.py
     2
     3def parse_input(input_string):
     4    """Return `input_string` as an integer between 1 and 6.
     5
     6    Check if `input_string` is an integer number between 1 and 6.
     7    If so, return an integer with the same value. Otherwise, tell
     8    the user to enter a valid number and quit the program.
     9    """
    10    if input_string.strip() in {"1", "2", "3", "4", "5", "6"}:
    11        return int(input_string)
    12    else:
    13        print("Please enter a number from 1 to 6.")
    14        raise SystemExit(1)
    15
    16# ~~~ App's main code block ~~~
    17# ...
    
    95. This argument will hold the list of dice-rolling integer values that results from calling
    $ python dice.py
    How many dice do you want to roll? [1-6] 3
    
    $ python dice.py
    How many dice do you want to roll? [1-6] 7
    Please enter a number from 1 to 6.
    
    6.
  • Các dòng 6 đến 18 cung cấp chức năng DocString. provide the function’s docstring. provide the function’s docstring.

  • Dòng 20 tạo một danh sách trống có tên

     1# dice.py
     2
     3def parse_input(input_string):
     4    """Return `input_string` as an integer between 1 and 6.
     5
     6    Check if `input_string` is an integer number between 1 and 6.
     7    If so, return an integer with the same value. Otherwise, tell
     8    the user to enter a valid number and quit the program.
     9    """
    10    if input_string.strip() in {"1", "2", "3", "4", "5", "6"}:
    11        return int(input_string)
    12    else:
    13        print("Please enter a number from 1 to 6.")
    14        raise SystemExit(1)
    15
    16# ~~~ App's main code block ~~~
    17# ...
    
    17 để lưu trữ các mặt xúc xắc tương ứng với danh sách đầu vào của các giá trị xúc xắc. Những khuôn mặt xúc xắc này sẽ hiển thị trong sơ đồ ASCII cuối cùng. creates an empty list called 7 để lưu trữ các mặt xúc xắc tương ứng với danh sách đầu vào của các giá trị xúc xắc. Những khuôn mặt xúc xắc này sẽ hiển thị trong sơ đồ ASCII cuối cùng. creates an empty list called

     1# dice.py
     2
     3def parse_input(input_string):
     4    """Return `input_string` as an integer between 1 and 6.
     5
     6    Check if `input_string` is an integer number between 1 and 6.
     7    If so, return an integer with the same value. Otherwise, tell
     8    the user to enter a valid number and quit the program.
     9    """
    10    if input_string.strip() in {"1", "2", "3", "4", "5", "6"}:
    11        return int(input_string)
    12    else:
    13        print("Please enter a number from 1 to 6.")
    14        raise SystemExit(1)
    15
    16# ~~~ App's main code block ~~~
    17# ...
    
    97 to store the dice faces corresponding to the input list of dice values. These dice faces will display in the final ASCII diagram.
  • Dòng 21 xác định vòng lặp

     1# dice.py
     2
     3def parse_input(input_string):
     4    """Return `input_string` as an integer between 1 and 6.
     5
     6    Check if `input_string` is an integer number between 1 and 6.
     7    If so, return an integer with the same value. Otherwise, tell
     8    the user to enter a valid number and quit the program.
     9    """
    10    if input_string.strip() in {"1", "2", "3", "4", "5", "6"}:
    11        return int(input_string)
    12    else:
    13        print("Please enter a number from 1 to 6.")
    14        raise SystemExit(1)
    15
    16# ~~~ App's main code block ~~~
    17# ...
    
    54 để lặp lại các giá trị xúc xắc. defines a 4 để lặp lại các giá trị xúc xắc. defines a

     1# dice.py
     2
     3def parse_input(input_string):
     4    """Return `input_string` as an integer between 1 and 6.
     5
     6    Check if `input_string` is an integer number between 1 and 6.
     7    If so, return an integer with the same value. Otherwise, tell
     8    the user to enter a valid number and quit the program.
     9    """
    10    if input_string.strip() in {"1", "2", "3", "4", "5", "6"}:
    11        return int(input_string)
    12    else:
    13        print("Please enter a number from 1 to 6.")
    14        raise SystemExit(1)
    15
    16# ~~~ App's main code block ~~~
    17# ...
    
    4 loop to iterate over the dice values.
  • Dòng 22 lấy mặt chết tương ứng với giá trị khuôn hiện tại từ

     1# dice.py
     2
     3def parse_input(input_string):
     4    """Return `input_string` as an integer between 1 and 6.
     5
     6    Check if `input_string` is an integer number between 1 and 6.
     7    If so, return an integer with the same value. Otherwise, tell
     8    the user to enter a valid number and quit the program.
     9    """
    10    if input_string.strip() in {"1", "2", "3", "4", "5", "6"}:
    11        return int(input_string)
    12    else:
    13        print("Please enter a number from 1 to 6.")
    14        raise SystemExit(1)
    15
    16# ~~~ App's main code block ~~~
    17# ...
    
    10 và nối nó vào
     1# dice.py
     2
     3def parse_input(input_string):
     4    """Return `input_string` as an integer between 1 and 6.
     5
     6    Check if `input_string` is an integer number between 1 and 6.
     7    If so, return an integer with the same value. Otherwise, tell
     8    the user to enter a valid number and quit the program.
     9    """
    10    if input_string.strip() in {"1", "2", "3", "4", "5", "6"}:
    11        return int(input_string)
    12    else:
    13        print("Please enter a number from 1 to 6.")
    14        raise SystemExit(1)
    15
    16# ~~~ App's main code block ~~~
    17# ...
    
    97. retrieves the die face corresponding to the current die value from 0 và nối nó vào
     1# dice.py
     2
     3def parse_input(input_string):
     4    """Return `input_string` as an integer between 1 and 6.
     5
     6    Check if `input_string` is an integer number between 1 and 6.
     7    If so, return an integer with the same value. Otherwise, tell
     8    the user to enter a valid number and quit the program.
     9    """
    10    if input_string.strip() in {"1", "2", "3", "4", "5", "6"}:
    11        return int(input_string)
    12    else:
    13        print("Please enter a number from 1 to 6.")
    14        raise SystemExit(1)
    15
    16# ~~~ App's main code block ~~~
    17# ...
    
    97.
    retrieves the die face corresponding to the current die value from

     1# dice.py
     2
     3def parse_input(input_string):
     4    """Return `input_string` as an integer between 1 and 6.
     5
     6    Check if `input_string` is an integer number between 1 and 6.
     7    If so, return an integer with the same value. Otherwise, tell
     8    the user to enter a valid number and quit the program.
     9    """
    10    if input_string.strip() in {"1", "2", "3", "4", "5", "6"}:
    11        return int(input_string)
    12    else:
    13        print("Please enter a number from 1 to 6.")
    14        raise SystemExit(1)
    15
    16# ~~~ App's main code block ~~~
    17# ...
    
    90 and appends it to
     1# dice.py
     2
     3def parse_input(input_string):
     4    """Return `input_string` as an integer between 1 and 6.
     5
     6    Check if `input_string` is an integer number between 1 and 6.
     7    If so, return an integer with the same value. Otherwise, tell
     8    the user to enter a valid number and quit the program.
     9    """
    10    if input_string.strip() in {"1", "2", "3", "4", "5", "6"}:
    11        return int(input_string)
    12    else:
    13        print("Please enter a number from 1 to 6.")
    14        raise SystemExit(1)
    15
    16# ~~~ App's main code block ~~~
    17# ...
    
    97.
  • Dòng 25 tạo ra một danh sách trống để giữ các hàng trong sơ đồ đối mặt với xúc xắc cuối cùng. creates an empty list to hold the rows in the final dice faces diagram. creates an empty list to hold the rows in the final dice faces diagram.

  • Dòng 26 xác định một vòng lặp lặp qua các chỉ số từ

     1# dice.py
     2
     3def parse_input(input_string):
     4    """Return `input_string` as an integer between 1 and 6.
     5
     6    Check if `input_string` is an integer number between 1 and 6.
     7    If so, return an integer with the same value. Otherwise, tell
     8    the user to enter a valid number and quit the program.
     9    """
    10    if input_string.strip() in {"1", "2", "3", "4", "5", "6"}:
    11        return int(input_string)
    12    else:
    13        print("Please enter a number from 1 to 6.")
    14        raise SystemExit(1)
    15
    16# ~~~ App's main code block ~~~
    17# ...
    
    91 đến
    $ python dice.py
    How many dice do you want to roll? [1-6] 3
    
    $ python dice.py
    How many dice do you want to roll? [1-6] 7
    Please enter a number from 1 to 6.
    
    42. Mỗi chỉ số đại diện cho chỉ số của một hàng đã cho trong sơ đồ đối mặt với xúc xắc. defines a loop that iterates over indices from 1 đến
    $ python dice.py
    How many dice do you want to roll? [1-6] 3
    
    $ python dice.py
    How many dice do you want to roll? [1-6] 7
    Please enter a number from 1 to 6.
    
    42. Mỗi chỉ số đại diện cho chỉ số của một hàng đã cho trong sơ đồ đối mặt với xúc xắc.
    defines a loop that iterates over indices from

    $ python dice.py
    How many dice do you want to roll? [1-6] 3
    
    $ python dice.py
    How many dice do you want to roll? [1-6] 7
    Please enter a number from 1 to 6.
    
    41 to
    $ python dice.py
    How many dice do you want to roll? [1-6] 3
    
    $ python dice.py
    How many dice do you want to roll? [1-6] 7
    Please enter a number from 1 to 6.
    
    42. Each index represents the index of a given row in the dice faces diagram.
  • Dòng 27 Xác định

     1# dice.py
     2
     3def parse_input(input_string):
     4    """Return `input_string` as an integer between 1 and 6.
     5
     6    Check if `input_string` is an integer number between 1 and 6.
     7    If so, return an integer with the same value. Otherwise, tell
     8    the user to enter a valid number and quit the program.
     9    """
    10    if input_string.strip() in {"1", "2", "3", "4", "5", "6"}:
    11        return int(input_string)
    12    else:
    13        print("Please enter a number from 1 to 6.")
    14        raise SystemExit(1)
    15
    16# ~~~ App's main code block ~~~
    17# ...
    
    93 là một danh sách trống để giữ các phần của các mặt xúc xắc sẽ lấp đầy một hàng nhất định. defines 3 là một danh sách trống để giữ các phần của các mặt xúc xắc sẽ lấp đầy một hàng nhất định. defines

    $ python dice.py
    How many dice do you want to roll? [1-6] 3
    
    $ python dice.py
    How many dice do you want to roll? [1-6] 7
    Please enter a number from 1 to 6.
    
    43 as an empty list to hold portions of the dice faces that will fill a given row.
  • Dòng 28 bắt đầu một vòng

     1# dice.py
     2
     3def parse_input(input_string):
     4    """Return `input_string` as an integer between 1 and 6.
     5
     6    Check if `input_string` is an integer number between 1 and 6.
     7    If so, return an integer with the same value. Otherwise, tell
     8    the user to enter a valid number and quit the program.
     9    """
    10    if input_string.strip() in {"1", "2", "3", "4", "5", "6"}:
    11        return int(input_string)
    12    else:
    13        print("Please enter a number from 1 to 6.")
    14        raise SystemExit(1)
    15
    16# ~~~ App's main code block ~~~
    17# ...
    
    54 lồng nhau để lặp lại các mặt xúc xắc. starts a nested 4 lồng nhau để lặp lại các mặt xúc xắc. starts a nested

     1# dice.py
     2
     3def parse_input(input_string):
     4    """Return `input_string` as an integer between 1 and 6.
     5
     6    Check if `input_string` is an integer number between 1 and 6.
     7    If so, return an integer with the same value. Otherwise, tell
     8    the user to enter a valid number and quit the program.
     9    """
    10    if input_string.strip() in {"1", "2", "3", "4", "5", "6"}:
    11        return int(input_string)
    12    else:
    13        print("Please enter a number from 1 to 6.")
    14        raise SystemExit(1)
    15
    16# ~~~ App's main code block ~~~
    17# ...
    
    4 loop to iterate over the dice faces.
  • Dòng 29 lưu trữ mỗi thành phần hàng. stores each row component. stores each row component.

  • Dòng 30 tham gia các thành phần hàng vào chuỗi hàng cuối cùng, tách các thành phần riêng lẻ với khoảng trắng. joins the row components into a final row string, separating individual components with spaces. joins the row components into a final row string, separating individual components with spaces.

  • Dòng 31 nối các chuỗi hàng vào danh sách giữ các hàng mà sẽ định hình sơ đồ cuối cùng. appends each row string to the list holding the rows that’ll shape the final diagram. appends each row string to the list holding the rows that’ll shape the final diagram.

  • Dòng 34 tạo ra một biến tạm thời để giữ

     1# dice.py
     2
     3def parse_input(input_string):
     4    """Return `input_string` as an integer between 1 and 6.
     5
     6    Check if `input_string` is an integer number between 1 and 6.
     7    If so, return an integer with the same value. Otherwise, tell
     8    the user to enter a valid number and quit the program.
     9    """
    10    if input_string.strip() in {"1", "2", "3", "4", "5", "6"}:
    11        return int(input_string)
    12    else:
    13        print("Please enter a number from 1 to 6.")
    14        raise SystemExit(1)
    15
    16# ~~~ App's main code block ~~~
    17# ...
    
    95 của sơ đồ Dice hiện tại. creates a temporary variable to hold the 5 của sơ đồ Dice hiện tại. creates a temporary variable to hold the

    $ python dice.py
    How many dice do you want to roll? [1-6] 3
    
    $ python dice.py
    How many dice do you want to roll? [1-6] 7
    Please enter a number from 1 to 6.
    
    45 of the current dice faces diagram.
  • Dòng 35 tạo ra một tiêu đề hiển thị kết quả từ. Để làm như vậy, nó sử dụng

     1# dice.py
     2
     3def parse_input(input_string):
     4    """Return `input_string` as an integer between 1 and 6.
     5
     6    Check if `input_string` is an integer number between 1 and 6.
     7    If so, return an integer with the same value. Otherwise, tell
     8    the user to enter a valid number and quit the program.
     9    """
    10    if input_string.strip() in {"1", "2", "3", "4", "5", "6"}:
    11        return int(input_string)
    12    else:
    13        print("Please enter a number from 1 to 6.")
    14        raise SystemExit(1)
    15
    16# ~~~ App's main code block ~~~
    17# ...
    
    96 với sơ đồ
    $ python dice.py
    How many dice do you want to roll? [1-6] 3
    
    $ python dice.py
    How many dice do you want to roll? [1-6] 7
    Please enter a number from 1 to 6.
    
    45 và ký hiệu Tilde (
    $ python dice.py
    How many dice do you want to roll? [1-6] 3
    
    $ python dice.py
    How many dice do you want to roll? [1-6] 7
    Please enter a number from 1 to 6.
    
    48) làm đối số. creates a header showing the word RESULTS. To do so, it uses 6 với sơ đồ
    $ python dice.py
    How many dice do you want to roll? [1-6] 3
    
    $ python dice.py
    How many dice do you want to roll? [1-6] 7
    Please enter a number from 1 to 6.
    
    45 và ký hiệu Tilde (
    $ python dice.py
    How many dice do you want to roll? [1-6] 3
    
    $ python dice.py
    How many dice do you want to roll? [1-6] 7
    Please enter a number from 1 to 6.
    
    48) làm đối số.
    creates a header showing the word RESULTS. To do so, it uses

    $ python dice.py
    How many dice do you want to roll? [1-6] 3
    
    $ python dice.py
    How many dice do you want to roll? [1-6] 7
    Please enter a number from 1 to 6.
    
    46 with the diagram’s
    $ python dice.py
    How many dice do you want to roll? [1-6] 3
    
    $ python dice.py
    How many dice do you want to roll? [1-6] 7
    Please enter a number from 1 to 6.
    
    45 and the tilde symbol (
    $ python dice.py
    How many dice do you want to roll? [1-6] 3
    
    $ python dice.py
    How many dice do you want to roll? [1-6] 7
    Please enter a number from 1 to 6.
    
    48) as arguments.
  • Dòng 37 tạo ra một chuỗi giữ sơ đồ đối mặt với xúc xắc cuối cùng. Ký tự nguồn cấp dữ liệu (

     1# dice.py
     2
     3def parse_input(input_string):
     4    """Return `input_string` as an integer between 1 and 6.
     5
     6    Check if `input_string` is an integer number between 1 and 6.
     7    If so, return an integer with the same value. Otherwise, tell
     8    the user to enter a valid number and quit the program.
     9    """
    10    if input_string.strip() in {"1", "2", "3", "4", "5", "6"}:
    11        return int(input_string)
    12    else:
    13        print("Please enter a number from 1 to 6.")
    14        raise SystemExit(1)
    15
    16# ~~~ App's main code block ~~~
    17# ...
    
    99) hoạt động như một dấu phân cách hàng. Đối số của
    $ python dice.py
    How many dice do you want to roll? [1-6] 3
    
    $ python dice.py
    How many dice do you want to roll? [1-6] 7
    Please enter a number from 1 to 6.
    
    0 là một danh sách các chuỗi liên kết tiêu đề sơ đồ và các chuỗi (hàng) định hình các mặt xúc xắc. generates a string that holds the final dice faces diagram. The line feed character (9) hoạt động như một dấu phân cách hàng. Đối số của
    $ python dice.py
    How many dice do you want to roll? [1-6] 3
    
    $ python dice.py
    How many dice do you want to roll? [1-6] 7
    Please enter a number from 1 to 6.
    
    0 là một danh sách các chuỗi liên kết tiêu đề sơ đồ và các chuỗi (hàng) định hình các mặt xúc xắc.
    generates a string that holds the final dice faces diagram. The line feed character (

    $ python dice.py
    How many dice do you want to roll? [1-6] 3
    
    $ python dice.py
    How many dice do you want to roll? [1-6] 7
    Please enter a number from 1 to 6.
    
    49) works as a row separator. The argument to
    $ python dice.py
    How many dice do you want to roll? [1-6] 3
    
    $ python dice.py
    How many dice do you want to roll? [1-6] 7
    Please enter a number from 1 to 6.
    
    0 is a list of strings concatenating the diagram header and the strings (rows) that shape the dice faces.
  • Dòng 38 Trả về một con xúc xắc sẵn sàng để đối mặt với người gọi. returns a ready-to-print dice faces diagram to the caller. returns a ready-to-print dice faces diagram to the caller.

Ồ! Đó là rất nhiều! Bạn sẽ quay lại mã này và cải thiện nó để làm cho nó dễ quản lý hơn chỉ một chút. Tuy nhiên, trước khi làm điều đó, bạn sẽ muốn thử ứng dụng của mình, vì vậy bạn cần hoàn thành việc viết khối mã chính của nó.

Kết thúc mã chính của ứng dụng và cuộn xúc xắc

Với

$ python dice.py
How many dice do you want to roll? [1-6] 3

$ python dice.py
How many dice do you want to roll? [1-6] 7
Please enter a number from 1 to 6.
9 tại chỗ, giờ đây bạn có thể hoàn thành việc viết mã chính của ứng dụng, điều này sẽ cho phép bạn thực sự tạo và hiển thị sơ đồ mặt xúc xắc trên màn hình của bạn. Đi trước và thêm các dòng mã sau vào cuối
 1# dice.py
 2
 3def parse_input(input_string):
 4    """Return `input_string` as an integer between 1 and 6.
 5
 6    Check if `input_string` is an integer number between 1 and 6.
 7    If so, return an integer with the same value. Otherwise, tell
 8    the user to enter a valid number and quit the program.
 9    """
10    if input_string.strip() in {"1", "2", "3", "4", "5", "6"}:
11        return int(input_string)
12    else:
13        print("Please enter a number from 1 to 6.")
14        raise SystemExit(1)
15
16# ~~~ App's main code block ~~~
17# ...
22:
 1# dice.py
 2
 3def parse_input(input_string):
 4    """Return `input_string` as an integer between 1 and 6.
 5
 6    Check if `input_string` is an integer number between 1 and 6.
 7    If so, return an integer with the same value. Otherwise, tell
 8    the user to enter a valid number and quit the program.
 9    """
10    if input_string.strip() in {"1", "2", "3", "4", "5", "6"}:
11        return int(input_string)
12    else:
13        print("Please enter a number from 1 to 6.")
14        raise SystemExit(1)
15
16# ~~~ App's main code block ~~~
17# ...
21

Dòng 12 gọi

$ python dice.py
How many dice do you want to roll? [1-6] 3

$ python dice.py
How many dice do you want to roll? [1-6] 7
Please enter a number from 1 to 6.
9 với
 1# dice.py
 2
 3def parse_input(input_string):
 4    """Return `input_string` as an integer between 1 and 6.
 5
 6    Check if `input_string` is an integer number between 1 and 6.
 7    If so, return an integer with the same value. Otherwise, tell
 8    the user to enter a valid number and quit the program.
 9    """
10    if input_string.strip() in {"1", "2", "3", "4", "5", "6"}:
11        return int(input_string)
12    else:
13        print("Please enter a number from 1 to 6.")
14        raise SystemExit(1)
15
16# ~~~ App's main code block ~~~
17# ...
41 như một đối số. Cuộc gọi này xây dựng và trả về một sơ đồ các mặt xúc xắc tương ứng với kết quả lăn xúc xắc hiện tại. Dòng 14 gọi
 1# dice.py
 2
 3def parse_input(input_string):
 4    """Return `input_string` as an integer between 1 and 6.
 5
 6    Check if `input_string` is an integer number between 1 and 6.
 7    If so, return an integer with the same value. Otherwise, tell
 8    the user to enter a valid number and quit the program.
 9    """
10    if input_string.strip() in {"1", "2", "3", "4", "5", "6"}:
11        return int(input_string)
12    else:
13        print("Please enter a number from 1 to 6.")
14        raise SystemExit(1)
15
16# ~~~ App's main code block ~~~
17# ...
5 để hiển thị sơ đồ trên màn hình.

Với bản cập nhật này, bạn có thể chạy lại ứng dụng. Quay lại dòng lệnh của bạn và thực hiện lệnh sau:

 1# dice.py
 2
 3def parse_input(input_string):
 4    """Return `input_string` as an integer between 1 and 6.
 5
 6    Check if `input_string` is an integer number between 1 and 6.
 7    If so, return an integer with the same value. Otherwise, tell
 8    the user to enter a valid number and quit the program.
 9    """
10    if input_string.strip() in {"1", "2", "3", "4", "5", "6"}:
11        return int(input_string)
12    else:
13        print("Please enter a number from 1 to 6.")
14        raise SystemExit(1)
15
16# ~~~ App's main code block ~~~
17# ...
0

Mát mẻ! Bây giờ ứng dụng mô phỏng xỏ xúc xắc của bạn hiển thị một sơ đồ ASCII được định dạng độc đáo cho thấy kết quả của sự kiện mô phỏng. Điều đó gọn gàng, có phải là nó không?

Nếu bạn quay lại thực hiện

$ python dice.py
How many dice do you want to roll? [1-6] 3

$ python dice.py
How many dice do you want to roll? [1-6] 7
Please enter a number from 1 to 6.
9, thì bạn sẽ lưu ý rằng nó bao gồm một vài nhận xét chỉ ra phần mã tương ứng đang làm gì:
 1# dice.py
 2
 3def parse_input(input_string):
 4    """Return `input_string` as an integer between 1 and 6.
 5
 6    Check if `input_string` is an integer number between 1 and 6.
 7    If so, return an integer with the same value. Otherwise, tell
 8    the user to enter a valid number and quit the program.
 9    """
10    if input_string.strip() in {"1", "2", "3", "4", "5", "6"}:
11        return int(input_string)
12    else:
13        print("Please enter a number from 1 to 6.")
14        raise SystemExit(1)
15
16# ~~~ App's main code block ~~~
17# ...
1

Loại bình luận này thường báo hiệu rằng mã của bạn sẽ được hưởng lợi từ một số tái cấu trúc. Trong phần sau, bạn sẽ sử dụng một kỹ thuật tái cấu trúc phổ biến sẽ giúp bạn dọn dẹp mã và làm cho nó dễ bảo trì hơn.refactoring. In the following section, you’ll use a popular refactoring technique that will help you clean up the code and make it more maintainable.refactoring. In the following section, you’ll use a popular refactoring technique that will help you clean up the code and make it more maintainable.

Bước 4: Tái cấu trúc mã tạo ra sơ đồ mặt xúc xắc

Hàm

$ python dice.py
How many dice do you want to roll? [1-6] 3

$ python dice.py
How many dice do you want to roll? [1-6] 7
Please enter a number from 1 to 6.
9 của bạn yêu cầu các nhận xét giải thích vì nó thực hiện một số hoạt động tại một thời điểm, vi phạm nguyên tắc tự chịu trách nhiệm.

Nói một cách đại khái, nguyên tắc này nói rằng mọi chức năng, lớp hoặc mô -đun chỉ nên làm một điều. Bằng cách đó, những thay đổi trong một chức năng nhất định đã giành được phần còn lại của mã. Do đó, bạn sẽ kết thúc với một mã mạnh mẽ và có thể duy trì hơn.

Để tải xuống mã cho bước này, nhấp vào liên kết bên dưới, sau đó xem thư mục

 1# dice.py
 2
 3def parse_input(input_string):
 4    """Return `input_string` as an integer between 1 and 6.
 5
 6    Check if `input_string` is an integer number between 1 and 6.
 7    If so, return an integer with the same value. Otherwise, tell
 8    the user to enter a valid number and quit the program.
 9    """
10    if input_string.strip() in {"1", "2", "3", "4", "5", "6"}:
11        return int(input_string)
12    else:
13        print("Please enter a number from 1 to 6.")
14        raise SystemExit(1)
15
16# ~~~ App's main code block ~~~
17# ...
218:

Có một kỹ thuật tái cấu trúc gọi là Phương pháp trích xuất có thể giúp bạn cải thiện mã của mình bằng cách trích xuất chức năng có thể hoạt động độc lập. Ví dụ: bạn có thể trích xuất mã từ dòng 20 đến 22 trong quá trình triển khai trước đây của

$ python dice.py
How many dice do you want to roll? [1-6] 3

$ python dice.py
How many dice do you want to roll? [1-6] 7
Please enter a number from 1 to 6.
9 và đặt nó vào chức năng trợ giúp không công khai gọi là
 1# dice.py
 2
 3def parse_input(input_string):
 4    """Return `input_string` as an integer between 1 and 6.
 5
 6    Check if `input_string` is an integer number between 1 and 6.
 7    If so, return an integer with the same value. Otherwise, tell
 8    the user to enter a valid number and quit the program.
 9    """
10    if input_string.strip() in {"1", "2", "3", "4", "5", "6"}:
11        return int(input_string)
12    else:
13        print("Please enter a number from 1 to 6.")
14        raise SystemExit(1)
15
16# ~~~ App's main code block ~~~
17# ...
00:extract method that can help you improve your code by extracting functionality that can work independently. For example, you can extract the code from line 20 to 22 in the previous implementation of
$ python dice.py
How many dice do you want to roll? [1-6] 3

$ python dice.py
How many dice do you want to roll? [1-6] 7
Please enter a number from 1 to 6.
9 and place it in a non-public helper function called
 1# dice.py
 2
 3def parse_input(input_string):
 4    """Return `input_string` as an integer between 1 and 6.
 5
 6    Check if `input_string` is an integer number between 1 and 6.
 7    If so, return an integer with the same value. Otherwise, tell
 8    the user to enter a valid number and quit the program.
 9    """
10    if input_string.strip() in {"1", "2", "3", "4", "5", "6"}:
11        return int(input_string)
12    else:
13        print("Please enter a number from 1 to 6.")
14        raise SystemExit(1)
15
16# ~~~ App's main code block ~~~
17# ...
00:
 1# dice.py
 2
 3def parse_input(input_string):
 4    """Return `input_string` as an integer between 1 and 6.
 5
 6    Check if `input_string` is an integer number between 1 and 6.
 7    If so, return an integer with the same value. Otherwise, tell
 8    the user to enter a valid number and quit the program.
 9    """
10    if input_string.strip() in {"1", "2", "3", "4", "5", "6"}:
11        return int(input_string)
12    else:
13        print("Please enter a number from 1 to 6.")
14        raise SystemExit(1)
15
16# ~~~ App's main code block ~~~
17# ...
2extract method that can help you improve your code by extracting functionality that can work independently. For example, you can extract the code from line 20 to 22 in the previous implementation of
$ python dice.py
How many dice do you want to roll? [1-6] 3

$ python dice.py
How many dice do you want to roll? [1-6] 7
Please enter a number from 1 to 6.
9 and place it in a non-public helper function called
 1# dice.py
 2
 3def parse_input(input_string):
 4    """Return `input_string` as an integer between 1 and 6.
 5
 6    Check if `input_string` is an integer number between 1 and 6.
 7    If so, return an integer with the same value. Otherwise, tell
 8    the user to enter a valid number and quit the program.
 9    """
10    if input_string.strip() in {"1", "2", "3", "4", "5", "6"}:
11        return int(input_string)
12    else:
13        print("Please enter a number from 1 to 6.")
14        raise SystemExit(1)
15
16# ~~~ App's main code block ~~~
17# ...
00:
 1# dice.py
 2
 3def parse_input(input_string):
 4    """Return `input_string` as an integer between 1 and 6.
 5
 6    Check if `input_string` is an integer number between 1 and 6.
 7    If so, return an integer with the same value. Otherwise, tell
 8    the user to enter a valid number and quit the program.
 9    """
10    if input_string.strip() in {"1", "2", "3", "4", "5", "6"}:
11        return int(input_string)
12    else:
13        print("Please enter a number from 1 to 6.")
14        raise SystemExit(1)
15
16# ~~~ App's main code block ~~~
17# ...
2

Bạn có thể gọi

 1# dice.py
 2
 3def parse_input(input_string):
 4    """Return `input_string` as an integer between 1 and 6.
 5
 6    Check if `input_string` is an integer number between 1 and 6.
 7    If so, return an integer with the same value. Otherwise, tell
 8    the user to enter a valid number and quit the program.
 9    """
10    if input_string.strip() in {"1", "2", "3", "4", "5", "6"}:
11        return int(input_string)
12    else:
13        print("Please enter a number from 1 to 6.")
14        raise SystemExit(1)
15
16# ~~~ App's main code block ~~~
17# ...
00 từ
$ python dice.py
How many dice do you want to roll? [1-6] 3

$ python dice.py
How many dice do you want to roll? [1-6] 7
Please enter a number from 1 to 6.
9 để có được chức năng ngụ ý. Bằng cách sử dụng kỹ thuật này, bạn có thể tái cấu trúc hoàn toàn
$ python dice.py
How many dice do you want to roll? [1-6] 3

$ python dice.py
How many dice do you want to roll? [1-6] 7
Please enter a number from 1 to 6.
9 để đáp ứng nguyên tắc tự chịu trách nhiệm.

Ở đây, một phiên bản tái cấu trúc của

$ python dice.py
How many dice do you want to roll? [1-6] 3

$ python dice.py
How many dice do you want to roll? [1-6] 7
Please enter a number from 1 to 6.
9 tận dụng
 1# dice.py
 2
 3def parse_input(input_string):
 4    """Return `input_string` as an integer between 1 and 6.
 5
 6    Check if `input_string` is an integer number between 1 and 6.
 7    If so, return an integer with the same value. Otherwise, tell
 8    the user to enter a valid number and quit the program.
 9    """
10    if input_string.strip() in {"1", "2", "3", "4", "5", "6"}:
11        return int(input_string)
12    else:
13        print("Please enter a number from 1 to 6.")
14        raise SystemExit(1)
15
16# ~~~ App's main code block ~~~
17# ...
00 và thực hiện một chức năng trợ giúp khác gọi là
 1# dice.py
 2
 3def parse_input(input_string):
 4    """Return `input_string` as an integer between 1 and 6.
 5
 6    Check if `input_string` is an integer number between 1 and 6.
 7    If so, return an integer with the same value. Otherwise, tell
 8    the user to enter a valid number and quit the program.
 9    """
10    if input_string.strip() in {"1", "2", "3", "4", "5", "6"}:
11        return int(input_string)
12    else:
13        print("Please enter a number from 1 to 6.")
14        raise SystemExit(1)
15
16# ~~~ App's main code block ~~~
17# ...
06 để trích xuất chức năng từ dòng 25 đến 31:
 1# dice.py
 2
 3def parse_input(input_string):
 4    """Return `input_string` as an integer between 1 and 6.
 5
 6    Check if `input_string` is an integer number between 1 and 6.
 7    If so, return an integer with the same value. Otherwise, tell
 8    the user to enter a valid number and quit the program.
 9    """
10    if input_string.strip() in {"1", "2", "3", "4", "5", "6"}:
11        return int(input_string)
12    else:
13        print("Please enter a number from 1 to 6.")
14        raise SystemExit(1)
15
16# ~~~ App's main code block ~~~
17# ...
3

Các hàm trợ giúp mới được thêm vào chức năng từ chức năng ban đầu. Bây giờ mỗi chức năng của người trợ giúp có trách nhiệm riêng. Các chức năng của người trợ giúp cũng cho phép bạn sử dụng tên có thể đọc và mô tả, loại bỏ nhu cầu nhận xét giải thích.

Tái cấu trúc mã của bạn để đưa nó thành hình dạng tốt hơn là một kỹ năng tuyệt vời để có một nhà phát triển Python. Để đào sâu hơn vào việc tái cấu trúc mã, hãy xem các ứng dụng Python tái cấu trúc để đơn giản.

Một ý tưởng cơ bản đằng sau việc tái cấu trúc mã là mã được sửa đổi sẽ hoạt động giống như mã gốc. Để kiểm tra nguyên tắc này, hãy tiếp tục và chạy lại ứng dụng của bạn!

Với điều đó, bạn đã hoàn thành dự án của mình! Bạn đã xây dựng một ứng dụng TUI đầy đủ chức năng cho phép bạn mô phỏng một sự kiện lăn xúc xắc. Mỗi khi bạn chạy ứng dụng, bạn có thể mô phỏng việc lăn lên tới sáu con xúc xắc với sáu mặt. Bạn thậm chí còn có thể nhìn thấy các mặt xúc xắc kết quả trong một sơ đồ ASCII đẹp mắt. Bạn đã làm rất tốt!

Sự kết luận

Bạn đã mã hóa một dự án đầy đủ chức năng bao gồm một ứng dụng giao diện người dùng dựa trên văn bản mô phỏng việc lăn xúc xắc sáu mặt trong Python. Với dự án này, bạn đã học và thực hành các kỹ năng cơ bản, chẳng hạn như thu thập và xác thực đầu vào của người dùng, nhập mã, viết chức năng, sử dụng các vòng và điều kiện và hiển thị đầu ra được định dạng độc đáo trên màn hình.

Trong hướng dẫn này, bạn đã học được cách:

  • Sử dụng
     1# dice.py
     2
     3def parse_input(input_string):
     4    """Return `input_string` as an integer between 1 and 6.
     5
     6    Check if `input_string` is an integer number between 1 and 6.
     7    If so, return an integer with the same value. Otherwise, tell
     8    the user to enter a valid number and quit the program.
     9    """
    10    if input_string.strip() in {"1", "2", "3", "4", "5", "6"}:
    11        return int(input_string)
    12    else:
    13        print("Please enter a number from 1 to 6.")
    14        raise SystemExit(1)
    15
    16# ~~~ App's main code block ~~~
    17# ...
    
    7 để mô phỏng việc lăn xúc xắc
     1# dice.py
     2
     3def parse_input(input_string):
     4    """Return `input_string` as an integer between 1 and 6.
     5
     6    Check if `input_string` is an integer number between 1 and 6.
     7    If so, return an integer with the same value. Otherwise, tell
     8    the user to enter a valid number and quit the program.
     9    """
    10    if input_string.strip() in {"1", "2", "3", "4", "5", "6"}:
    11        return int(input_string)
    12    else:
    13        print("Please enter a number from 1 to 6.")
    14        raise SystemExit(1)
    15
    16# ~~~ App's main code block ~~~
    17# ...
    
    7 to simulate the rolling of dice
     1# dice.py
     2
     3def parse_input(input_string):
     4    """Return `input_string` as an integer between 1 and 6.
     5
     6    Check if `input_string` is an integer number between 1 and 6.
     7    If so, return an integer with the same value. Otherwise, tell
     8    the user to enter a valid number and quit the program.
     9    """
    10    if input_string.strip() in {"1", "2", "3", "4", "5", "6"}:
    11        return int(input_string)
    12    else:
    13        print("Please enter a number from 1 to 6.")
    14        raise SystemExit(1)
    15
    16# ~~~ App's main code block ~~~
    17# ...
    
    7
    to simulate the rolling of dice
  • Lấy đầu vào của người dùng tại dòng lệnh bằng cách sử dụng hàm
     1# dice.py
     2
     3def parse_input(input_string):
     4    """Return `input_string` as an integer between 1 and 6.
     5
     6    Check if `input_string` is an integer number between 1 and 6.
     7    If so, return an integer with the same value. Otherwise, tell
     8    the user to enter a valid number and quit the program.
     9    """
    10    if input_string.strip() in {"1", "2", "3", "4", "5", "6"}:
    11        return int(input_string)
    12    else:
    13        print("Please enter a number from 1 to 6.")
    14        raise SystemExit(1)
    15
    16# ~~~ App's main code block ~~~
    17# ...
    
    8 tích hợpuser’s input at the command line using the built-in
     1# dice.py
     2
     3def parse_input(input_string):
     4    """Return `input_string` as an integer between 1 and 6.
     5
     6    Check if `input_string` is an integer number between 1 and 6.
     7    If so, return an integer with the same value. Otherwise, tell
     8    the user to enter a valid number and quit the program.
     9    """
    10    if input_string.strip() in {"1", "2", "3", "4", "5", "6"}:
    11        return int(input_string)
    12    else:
    13        print("Please enter a number from 1 to 6.")
    14        raise SystemExit(1)
    15
    16# ~~~ App's main code block ~~~
    17# ...
    
    8 functionuser’s input at the command line using the built-in
     1# dice.py
     2
     3def parse_input(input_string):
     4    """Return `input_string` as an integer between 1 and 6.
     5
     6    Check if `input_string` is an integer number between 1 and 6.
     7    If so, return an integer with the same value. Otherwise, tell
     8    the user to enter a valid number and quit the program.
     9    """
    10    if input_string.strip() in {"1", "2", "3", "4", "5", "6"}:
    11        return int(input_string)
    12    else:
    13        print("Please enter a number from 1 to 6.")
    14        raise SystemExit(1)
    15
    16# ~~~ App's main code block ~~~
    17# ...
    
    8
    function
  • Phân tích cú pháp và xác nhận đầu vào của người dùng bằng cách sử dụng một số công cụ và kỹ thuật and validate the user’s input using several tools and techniques and validate the user’s input using several tools and techniques
  • Thao tác chuỗi bằng các phương pháp, chẳng hạn như
     1# dice.py
     2
     3def parse_input(input_string):
     4    """Return `input_string` as an integer between 1 and 6.
     5
     6    Check if `input_string` is an integer number between 1 and 6.
     7    If so, return an integer with the same value. Otherwise, tell
     8    the user to enter a valid number and quit the program.
     9    """
    10    if input_string.strip() in {"1", "2", "3", "4", "5", "6"}:
    11        return int(input_string)
    12    else:
    13        print("Please enter a number from 1 to 6.")
    14        raise SystemExit(1)
    15
    16# ~~~ App's main code block ~~~
    17# ...
    
    9 và
    $ python dice.py
    How many dice do you want to roll? [1-6] 3
    
    $ python dice.py
    How many dice do you want to roll? [1-6] 7
    Please enter a number from 1 to 6.
    
    0strings using methods, such as
     1# dice.py
     2
     3def parse_input(input_string):
     4    """Return `input_string` as an integer between 1 and 6.
     5
     6    Check if `input_string` is an integer number between 1 and 6.
     7    If so, return an integer with the same value. Otherwise, tell
     8    the user to enter a valid number and quit the program.
     9    """
    10    if input_string.strip() in {"1", "2", "3", "4", "5", "6"}:
    11        return int(input_string)
    12    else:
    13        print("Please enter a number from 1 to 6.")
    14        raise SystemExit(1)
    15
    16# ~~~ App's main code block ~~~
    17# ...
    
    9 and
    $ python dice.py
    How many dice do you want to roll? [1-6] 3
    
    $ python dice.py
    How many dice do you want to roll? [1-6] 7
    Please enter a number from 1 to 6.
    
    0strings using methods, such as
     1# dice.py
     2
     3def parse_input(input_string):
     4    """Return `input_string` as an integer between 1 and 6.
     5
     6    Check if `input_string` is an integer number between 1 and 6.
     7    If so, return an integer with the same value. Otherwise, tell
     8    the user to enter a valid number and quit the program.
     9    """
    10    if input_string.strip() in {"1", "2", "3", "4", "5", "6"}:
    11        return int(input_string)
    12    else:
    13        print("Please enter a number from 1 to 6.")
    14        raise SystemExit(1)
    15
    16# ~~~ App's main code block ~~~
    17# ...
    
    9
    and
    $ python dice.py
    How many dice do you want to roll? [1-6] 3
    
    $ python dice.py
    How many dice do you want to roll? [1-6] 7
    Please enter a number from 1 to 6.
    
    0

Ngoài ra, bạn đã học cách cấu trúc, tổ chức, tài liệu và chạy các chương trình và kịch bản Python. Với kiến ​​thức này, bạn đã chuẩn bị tốt hơn để tiếp tục hành trình mã hóa của mình với Python.

Bạn có thể tải xuống mã đầy đủ cho ứng dụng Rolling Dice này bằng cách nhấp vào liên kết bên dưới:

Bước tiếp theo

Bây giờ, bạn đã hoàn thành việc xây dựng ứng dụng lăn xúc xắc của mình, bạn có thể thực hiện dự án một bước nữa bằng cách thêm chức năng mới. Thêm các tính năng mới của riêng mình sẽ giúp bạn tiếp tục tìm hiểu các khái niệm và kỹ thuật mã hóa mới thú vị.

Dưới đây là một số ý tưởng để đưa dự án của bạn lên một tầm cao mới:

  • Hỗ trợ cho bất kỳ số lượng xúc xắc: sửa đổi mã để bạn có thể cuộn bất kỳ số xúc xắc nào. Modify the code so that you can roll any number of dice. Modify the code so that you can roll any number of dice.
  • Hỗ trợ xúc xắc với số lượng khuôn mặt khác nhau: Thêm mã để hỗ trợ không chỉ xúc xắc sáu mặt mà còn xúc xắc với bất kỳ số bên nào. Add code to support not only six-sided dice but dice with any number of sides. Add code to support not only six-sided dice but dice with any number of sides.

Tính năng đầu tiên sẽ yêu cầu bạn sửa đổi mã xử lý đầu vào của người dùng cho số lượng xúc xắc. Bạn cũng cần phải sửa đổi mã tạo và hiển thị sơ đồ mặt xúc xắc. Ví dụ: bạn có thể tạo một sơ đồ hiển thị các mặt xúc xắc trong một số hàng để tránh làm lộn xộn màn hình của bạn với đầu ra chật chội.

Mặt khác, việc hỗ trợ xúc xắc với một số lượng khuôn mặt khác nhau sẽ yêu cầu bạn điều chỉnh mã mô phỏng một sự kiện lăn xúc xắc. Bạn cũng cần tạo ra ASCII Art mới cho bất kỳ con xúc xắc nào có hơn sáu khuôn mặt.

Khi bạn đã hoàn thành với các tính năng mới này, bạn có thể thay đổi bánh răng và nhảy vào các dự án tuyệt vời khác. Dưới đây là một số bước tiếp theo tuyệt vời để bạn tiếp tục học Python và xây dựng các dự án phức tạp hơn:

  • Cách xây dựng các giao diện dòng lệnh trong Python với Argparse: Trong hướng dẫn Python từng bước này, bạn sẽ học cách đưa các tập lệnh Python dòng lệnh của mình lên cấp độ tiếp theo bằng cách thêm giao diện dòng lệnh thuận tiện mà bạn có thể viết với

     1# dice.py
     2
     3def parse_input(input_string):
     4    """Return `input_string` as an integer between 1 and 6.
     5
     6    Check if `input_string` is an integer number between 1 and 6.
     7    If so, return an integer with the same value. Otherwise, tell
     8    the user to enter a valid number and quit the program.
     9    """
    10    if input_string.strip() in {"1", "2", "3", "4", "5", "6"}:
    11        return int(input_string)
    12    else:
    13        print("Please enter a number from 1 to 6.")
    14        raise SystemExit(1)
    15
    16# ~~~ App's main code block ~~~
    17# ...
    
    11.
  • Xây dựng một trình tạo cây thư mục Python cho dòng lệnh: Trong dự án từng bước này, bạn sẽ tạo một ứng dụng Trình tạo cây thư mục Python cho dòng lệnh của bạn. Bạn sẽ mã hóa giao diện dòng lệnh với

     1# dice.py
     2
     3def parse_input(input_string):
     4    """Return `input_string` as an integer between 1 and 6.
     5
     6    Check if `input_string` is an integer number between 1 and 6.
     7    If so, return an integer with the same value. Otherwise, tell
     8    the user to enter a valid number and quit the program.
     9    """
    10    if input_string.strip() in {"1", "2", "3", "4", "5", "6"}:
    11        return int(input_string)
    12    else:
    13        print("Please enter a number from 1 to 6.")
    14        raise SystemExit(1)
    15
    16# ~~~ App's main code block ~~~
    17# ...
    
    11 và đi qua hệ thống tệp bằng
     1# dice.py
     2
     3def parse_input(input_string):
     4    """Return `input_string` as an integer between 1 and 6.
     5
     6    Check if `input_string` is an integer number between 1 and 6.
     7    If so, return an integer with the same value. Otherwise, tell
     8    the user to enter a valid number and quit the program.
     9    """
    10    if input_string.strip() in {"1", "2", "3", "4", "5", "6"}:
    11        return int(input_string)
    12    else:
    13        print("Please enter a number from 1 to 6.")
    14        raise SystemExit(1)
    15
    16# ~~~ App's main code block ~~~
    17# ...
    
    13.
  • Xây dựng một ứng dụng cần làm dòng lệnh với Python và Typer: Trong dự án từng bước này, bạn sẽ tạo một ứng dụng cần làm cho dòng lệnh của mình bằng Python và Typer. Trong khi bạn xây dựng ứng dụng này, bạn sẽ học những điều cơ bản của Typer, một thư viện hiện đại và đa năng để xây dựng các giao diện dòng lệnh.

Trình mô phỏng xúc xắc là gì?

Trình mô phỏng xúc xắc không là gì ngoài một mô hình máy tính có thể được tạo bởi một chương trình phần mềm và nó hoạt động giống như một con xúc xắc bình thường trong đó người dùng lăn một con xúc xắc và một số ngẫu nhiên được hiển thị trên màn hình.Có nhiều cách mà một trình mô phỏng xúc xắc có thể được thực hiện.a computer model that can be created by a software program and it functions same as a normal dice in which user rolls a dice and a random number gets shown on the screen. There are many ways a dice simulator can be implemented.a computer model that can be created by a software program and it functions same as a normal dice in which user rolls a dice and a random number gets shown on the screen. There are many ways a dice simulator can be implemented.

Làm thế nào để bạn mô phỏng một con xúc xắc trong Python?

Conclusion...

Sử dụng ngẫu nhiên.randint () để mô phỏng việc lăn xúc xắc ..

Lấy đầu vào của người dùng tại dòng lệnh bằng hàm input () tích hợp () ..

Parse và xác nhận đầu vào của người dùng bằng một số công cụ và kỹ thuật ..

Thao tác chuỗi bằng các phương pháp, chẳng hạn như.trung tâm () và.tham gia().

Làm thế nào để bạn làm một con xúc xắc trong trình mô phỏng lăn?

#Hình ảnh mô -đun cho lần tạo số ngẫu nhiên ngẫu nhiên #range của các giá trị của xúc xắc min_val = 1 max_val = 6 #to lặp lại cuộn qua người dùng đầu vào roll_again = "Có" #loop trong khi roll_again == "Có" hoặc roll_again == "y ": print (" lăn các dices ... ") in (" Các giá trị là: ") #Generating và in lần 1 ngẫu nhiên ...