1
Nội dung chính ShowShow
- Bước tiếp theo
- 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.
- Đ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
Mới! Lưu câu hỏi hoặc câu trả lời và sắp xếp nội dung yêu thích của bạn. Tìm hiểu thêm.Learn more.
Learn more.
Viết chương trình solo đầu tiên của tôi mà không cần trợ giúp từ giáo viên hoặc nhóm, một mã đơn giản có thể hoạt động như một con lăn xúc xắc D & D cho bất kỳ loại hoặc số xúc xắc mà người dùng yêu cầu.
Tôi đã làm việc với nó trong khoảng bốn giờ và tôi bị mắc kẹt trong điều cuối cùng tôi muốn làm là lặp lại từ đầu thay vì chỉ kết thúc khi người dùng không chạy lại xúc xắc đã chọn, tôi 'D như nó để nó bắt đầu lại từ đầu để người chơi có thể nhập giá trị xúc xắc mới và số lượng cuộn được tạo mà không cần đóng chương trình và chạy lại nó.
import random
try:
min = 1
max = int[input["Enter the highest value of dice to be rolled: "]]
except:
print["Your input was invalid, program rolled a d20 by default"]
min = 1
max = 20
again = True
number_of_dice = int[input["Enter number of dice to roll: "]]
for i in range[number_of_dice - 1]:
print[random.randint[min, max]]
while again:
print[random.randint[min, max]]
reroll = input["Roll again? [y/n]: "]
if reroll.lower[] == "y" or reroll.lower[] == "yes":
for i in range[number_of_dice - 1]:
print[random.randint[min, max]]
else:
print["Thank you"]
break
Hỏi ngày 4 tháng 8 năm 2020 lúc 17:58Aug 4, 2020 at 17:58Aug 4, 2020 at 17:58
2
Bạn có thể thử một cái gì đó như:
import random
while True:
try:
min = 1
max = int[input["Enter the highest value of dice to be rolled or 0 to exit: "]]
except:
print["Your input was invalid, program rolled a d20 by default"]
min = 1
max = 20
if max == 0:
break
if max < 0:
continue
again = True
number_of_dice = int[input["Enter number of dice to roll: "]]
for i in range[number_of_dice - 1]:
print[random.randint[min, max]]
while again:
print[random.randint[min, max]]
reroll = input["Roll again? [y/n]: "]
if reroll.lower[] == "y" or reroll.lower[] == "yes":
for i in range[number_of_dice - 1]:
print[random.randint[min, max]]
else:
print["Thank you"]
break
Ngoài ra, tôi sẽ đề nghị đổi tên "Min" và "Max" khi chúng được bảo lưu từ khóa
Đã trả lời ngày 4 tháng 8 năm 2020 lúc 18:10Aug 4, 2020 at 18:10Aug 4, 2020 at 18:10
1
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
import random
while True:
try:
min = 1
max = int[input["Enter the highest value of dice to be rolled or 0 to exit: "]]
except:
print["Your input was invalid, program rolled a d20 by default"]
min = 1
max = 20
if max == 0:
break
if max < 0:
continue
again = True
number_of_dice = int[input["Enter number of dice to roll: "]]
for i in range[number_of_dice - 1]:
print[random.randint[min, max]]
while again:
print[random.randint[min, max]]
reroll = input["Roll again? [y/n]: "]
if reroll.lower[] == "y" or reroll.lower[] == "yes":
for i in range[number_of_dice - 1]:
print[random.randint[min, max]]
else:
print["Thank you"]
break
6 và hiển thị gọn gàng đầu ra bằng cách sử dụng Chuỗi và hàm import random
while True:
try:
min = 1
max = int[input["Enter the highest value of dice to be rolled or 0 to exit: "]]
except:
print["Your input was invalid, program rolled a d20 by default"]
min = 1
max = 20
if max == 0:
break
if max < 0:
continue
again = True
number_of_dice = int[input["Enter number of dice to roll: "]]
for i in range[number_of_dice - 1]:
print[random.randint[min, max]]
while again:
print[random.randint[min, max]]
reroll = input["Roll again? [y/n]: "]
if reroll.lower[] == "y" or reroll.lower[] == "yes":
for i in range[number_of_dice - 1]:
print[random.randint[min, max]]
else:
print["Thank you"]
break
7.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ừ
import random
while True:
try:
min = 1
max = int[input["Enter the highest value of dice to be rolled or 0 to exit: "]]
except:
print["Your input was invalid, program rolled a d20 by default"]
min = 1
max = 20
if max == 0:
break
if max < 0:
continue
again = True
number_of_dice = int[input["Enter number of dice to roll: "]]
for i in range[number_of_dice - 1]:
print[random.randint[min, max]]
while again:
print[random.randint[min, max]]
reroll = input["Roll again? [y/n]: "]
if reroll.lower[] == "y" or reroll.lower[] == "yes":
for i in range[number_of_dice - 1]:
print[random.randint[min, max]]
else:
print["Thank you"]
break
8.Trong hướng dẫn này, bạn sẽ học cách:
- Sử dụng
9 để mô phỏng các sự kiện lăn xúc xắcimport random while True: try: min = 1 max = int[input["Enter the highest value of dice to be rolled or 0 to exit: "]] except: print["Your input was invalid, program rolled a d20 by default"] min = 1 max = 20 if max == 0: break if max < 0: continue again = True number_of_dice = int[input["Enter number of dice to roll: "]] for i in range[number_of_dice - 1]: print[random.randint[min, max]] while again: print[random.randint[min, max]] reroll = input["Roll again? [y/n]: "] if reroll.lower[] == "y" or reroll.lower[] == "yes": for i in range[number_of_dice - 1]: print[random.randint[min, max]] else: print["Thank you"] break
9 to simulate dice-rolling eventsimport random while True: try: min = 1 max = int[input["Enter the highest value of dice to be rolled or 0 to exit: "]] except: print["Your input was invalid, program rolled a d20 by default"] min = 1 max = 20 if max == 0: break if max < 0: continue again = True number_of_dice = int[input["Enter number of dice to roll: "]] for i in range[number_of_dice - 1]: print[random.randint[min, max]] while again: print[random.randint[min, max]] reroll = input["Roll again? [y/n]: "] if reroll.lower[] == "y" or reroll.lower[] == "yes": for i in range[number_of_dice - 1]: print[random.randint[min, max]] else: print["Thank you"] break
9 to simulate dice-rolling eventsimport random while True: try: min = 1 max = int[input["Enter the highest value of dice to be rolled or 0 to exit: "]] except: print["Your input was invalid, program rolled a d20 by default"] min = 1 max = 20 if max == 0: break if max < 0: continue again = True number_of_dice = int[input["Enter number of dice to roll: "]] for i in range[number_of_dice - 1]: print[random.randint[min, max]] while again: print[random.randint[min, max]] reroll = input["Roll again? [y/n]: "] if reroll.lower[] == "y" or reroll.lower[] == "yes": for i in range[number_of_dice - 1]: print[random.randint[min, max]] else: print["Thank you"] break
- Yêu cầu đầu vào của người dùng bằng cách sử dụng chức năng
0 tích hợpuser’s input using the built-in1# dice.py 2 3# ~~~ App's main code block ~~~ 4# 1. Get and validate user's input 5num_dice_input = input["How many dice do you want to roll? [1-6] "] 6num_dice = parse_input[num_dice_input]
0 functionuser’s input using the built-in1# dice.py 2 3# ~~~ App's main code block ~~~ 4# 1. Get and validate user's input 5num_dice_input = input["How many dice do you want to roll? [1-6] "] 6num_dice = parse_input[num_dice_input]
0 function1# dice.py 2 3# ~~~ App's main code block ~~~ 4# 1. Get and validate user's input 5num_dice_input = input["How many dice do you want to roll? [1-6] "] 6num_dice = parse_input[num_dice_input]
- 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 và1# dice.py 2 3# ~~~ App's main code block ~~~ 4# 1. Get and validate user's input 5num_dice_input = input["How many dice do you want to roll? [1-6] "] 6num_dice = parse_input[num_dice_input]
2strings using methods, such as1# dice.py 2 3# ~~~ App's main code block ~~~ 4# 1. Get and validate user's input 5num_dice_input = input["How many dice do you want to roll? [1-6] "] 6num_dice = parse_input[num_dice_input]
1 and1# dice.py 2 3# ~~~ App's main code block ~~~ 4# 1. Get and validate user's input 5num_dice_input = input["How many dice do you want to roll? [1-6] "] 6num_dice = parse_input[num_dice_input]
2strings using methods, such as1# dice.py 2 3# ~~~ App's main code block ~~~ 4# 1. Get and validate user's input 5num_dice_input = input["How many dice do you want to roll? [1-6] "] 6num_dice = parse_input[num_dice_input]
1 and1# dice.py 2 3# ~~~ App's main code block ~~~ 4# 1. Get and validate user's input 5num_dice_input = input["How many dice do you want to roll? [1-6] "] 6num_dice = parse_input[num_dice_input]
21# dice.py 2 3# ~~~ App's main code block ~~~ 4# 1. Get and validate user's input 5num_dice_input = input["How many dice do you want to roll? [1-6] "] 6num_dice = parse_input[num_dice_input]
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.
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.
Ở đây, một mô tả về cách ứng dụng sẽ hoạt động trong nội bộ:
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 0 tích hợp của Python | Một cuộc gọi đến 0 với các đối số thích hợp |
Phân tích cú pháp và xác nhận đầu vào của người dùng | Phương thức chuỗi, toán tử so sánh và câu lệnh có điều kiện | Một chức năng do người dùng xác định gọi là 5 |
Chạy mô phỏng con xúc xắc | Mô -đun Python từ 8, cụ thể là hàm 7 | Một chức năng do người dùng xác định gọi là 8 |
Tạo sơ đồ ASCII với các mặt xúc xắc kết quả | Vòng lặp, 9 và 00 | Một chức năng do người dùng xác định gọi là 01 |
Hiển thị sơ đồ mặt xúc xắc trên màn hình | Chức năng 7 tích hợp của Python | Một cuộc gọi đến 7 với các đối số thích hợp |
Hãy ghi nhớ các hoạt động nội bộ này, bạn sẽ mã hóa ba chức năng tùy chỉnh để cung cấp các tính năng và chức năng chính của ứng dụng. Các chức năng này sẽ xác định API công khai mã của bạn, mà bạn sẽ gọi để đưa ứng dụng vào cuộc sống.
Để sắp xếp mã của dự án giả lập xỏ xúc xắc của bạn, bạn sẽ tạo một tệp duy nhất có tên
import random
while True:
try:
min = 1
max = int[input["Enter the highest value of dice to be rolled or 0 to exit: "]]
except:
print["Your input was invalid, program rolled a d20 by default"]
min = 1
max = 20
if max == 0:
break
if max < 0:
continue
again = True
number_of_dice = int[input["Enter number of dice to roll: "]]
for i in range[number_of_dice - 1]:
print[random.randint[min, max]]
while again:
print[random.randint[min, max]]
reroll = input["Roll again? [y/n]: "]
if reroll.lower[] == "y" or reroll.lower[] == "yes":
for i in range[number_of_dice - 1]:
print[random.randint[min, max]]
else:
print["Thank you"]
break
04 trong thư mục bạn chọn trong hệ thống tệp của mình. Hãy tiếp tục và tạo tập tin để bắt đầu!Điều kiện tiên quyết
Bạn nên thoải mái với các khái niệm và kỹ năng sau đây trước khi bạn bắt đầu xây dựng dự án mô phỏng xúc xắc này:
- Cách chạy tập lệnh trong Python
- Python từ
05 cơ chếimport random while True: try: min = 1 max = int[input["Enter the highest value of dice to be rolled or 0 to exit: "]] except: print["Your input was invalid, program rolled a d20 by default"] min = 1 max = 20 if max == 0: break if max < 0: continue again = True number_of_dice = int[input["Enter number of dice to roll: "]] for i in range[number_of_dice - 1]: print[random.randint[min, max]] while again: print[random.randint[min, max]] reroll = input["Roll again? [y/n]: "] if reroll.lower[] == "y" or reroll.lower[] == "yes": for i in range[number_of_dice - 1]: print[random.randint[min, max]] else: print["Thank you"] break
- Những điều cơ bản của các loại dữ liệu Python, chủ yếu là các chuỗi và số nguyên
- Cấu trúc dữ liệu cơ bản, đặc biệt là danh sách
- Các biến và hằng số Python
- Các nhà khai thác so sánh Python
- Giá trị boolean và biểu thức logic
- Câu điều kiện
- Python
6 vòng lặpimport random while True: try: min = 1 max = int[input["Enter the highest value of dice to be rolled or 0 to exit: "]] except: print["Your input was invalid, program rolled a d20 by default"] min = 1 max = 20 if max == 0: break if max < 0: continue again = True number_of_dice = int[input["Enter number of dice to roll: "]] for i in range[number_of_dice - 1]: print[random.randint[min, max]] while again: print[random.randint[min, max]] reroll = input["Roll again? [y/n]: "] if reroll.lower[] == "y" or reroll.lower[] == "yes": for i in range[number_of_dice - 1]: print[random.randint[min, max]] else: print["Thank you"] break
- Những điều cơ bản về định dạng đầu vào, đầu ra và chuỗi trong Python
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
import random
while True:
try:
min = 1
max = int[input["Enter the highest value of dice to be rolled or 0 to exit: "]]
except:
print["Your input was invalid, program rolled a d20 by default"]
min = 1
max = 20
if max == 0:
break
if max < 0:
continue
again = True
number_of_dice = int[input["Enter number of dice to roll: "]]
for i in range[number_of_dice - 1]:
print[random.randint[min, max]]
while again:
print[random.randint[min, max]]
reroll = input["Roll again? [y/n]: "]
if reroll.lower[] == "y" or reroll.lower[] == "yes":
for i in range[number_of_dice - 1]:
print[random.randint[min, max]]
else:
print["Thank you"]
break
07: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
3# ~~~ App's main code block ~~~
4# 1. Get and validate user's input
5num_dice_input = input["How many dice do you want to roll? [1-6] "]
6num_dice = parse_input[num_dice_input]
0. Hàm tích hợp này đọc đầu vào người dùng từ dòng lệnh. Đối số import random
while True:
try:
min = 1
max = int[input["Enter the highest value of dice to be rolled or 0 to exit: "]]
except:
print["Your input was invalid, program rolled a d20 by default"]
min = 1
max = 20
if max == 0:
break
if max < 0:
continue
again = True
number_of_dice = int[input["Enter number of dice to roll: "]]
for i in range[number_of_dice - 1]:
print[random.randint[min, max]]
while again:
print[random.randint[min, max]]
reroll = input["Roll again? [y/n]: "]
if reroll.lower[] == "y" or reroll.lower[] == "yes":
for i in range[number_of_dice - 1]:
print[random.randint[min, max]]
else:
print["Thank you"]
break
09 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
3# ~~~ App's main code block ~~~
4# 1. Get and validate user's input
5num_dice_input = input["How many dice do you want to roll? [1-6] "]
6num_dice = parse_input[num_dice_input]
0. This built-in function reads the user input from the command line. Its import random
while True:
try:
min = 1
max = int[input["Enter the highest value of dice to be rolled or 0 to exit: "]]
except:
print["Your input was invalid, program rolled a d20 by default"]
min = 1
max = 20
if max == 0:
break
if max < 0:
continue
again = True
number_of_dice = int[input["Enter number of dice to roll: "]]
for i in range[number_of_dice - 1]:
print[random.randint[min, max]]
while again:
print[random.randint[min, max]]
reroll = input["Roll again? [y/n]: "]
if reroll.lower[] == "y" or reroll.lower[] == "yes":
for i in range[number_of_dice - 1]:
print[random.randint[min, max]]
else:
print["Thank you"]
break
09 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
3# ~~~ App's main code block ~~~
4# 1. Get and validate user's input
5num_dice_input = input["How many dice do you want to roll? [1-6] "]
6num_dice = parse_input[num_dice_input]
0. This built-in function reads the user input from the command line. Its import random
while True:
try:
min = 1
max = int[input["Enter the highest value of dice to be rolled or 0 to exit: "]]
except:
print["Your input was invalid, program rolled a d20 by default"]
min = 1
max = 20
if max == 0:
break
if max < 0:
continue
again = True
number_of_dice = int[input["Enter number of dice to roll: "]]
for i in range[number_of_dice - 1]:
print[random.randint[min, max]]
while again:
print[random.randint[min, max]]
reroll = input["Roll again? [y/n]: "]
if reroll.lower[] == "y" or reroll.lower[] == "yes":
for i in range[number_of_dice - 1]:
print[random.randint[min, max]]
else:
print["Thank you"]
break
09 argument allows you to pass a description of what type of input you need.Bắn trình chỉnh sửa hoặc IDE yêu thích của bạn và nhập mã sau vào tệp
import random
while True:
try:
min = 1
max = int[input["Enter the highest value of dice to be rolled or 0 to exit: "]]
except:
print["Your input was invalid, program rolled a d20 by default"]
min = 1
max = 20
if max == 0:
break
if max < 0:
continue
again = True
number_of_dice = int[input["Enter number of dice to roll: "]]
for i in range[number_of_dice - 1]:
print[random.randint[min, max]]
while again:
print[random.randint[min, max]]
reroll = input["Roll again? [y/n]: "]
if reroll.lower[] == "y" or reroll.lower[] == "yes":
for i in range[number_of_dice - 1]:
print[random.randint[min, max]]
else:
print["Thank you"]
break
04 của bạn: 1# dice.py
2
3# ~~~ App's main code block ~~~
4# 1. Get and validate user's input
5num_dice_input = input["How many dice do you want to roll? [1-6] "]
6num_dice = parse_input[num_dice_input]
Cuộc gọi của bạn đến
1# dice.py
2
3# ~~~ App's main code block ~~~
4# 1. Get and validate user's input
5num_dice_input = input["How many dice do you want to roll? [1-6] "]
6num_dice = parse_input[num_dice_input]
0 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
1# dice.py
2
3# ~~~ App's main code block ~~~
4# 1. Get and validate user's input
5num_dice_input = input["How many dice do you want to roll? [1-6] "]
6num_dice = parse_input[num_dice_input]
5 và lưu trữ giá trị trả về trong import random
while True:
try:
min = 1
max = int[input["Enter the highest value of dice to be rolled or 0 to exit: "]]
except:
print["Your input was invalid, program rolled a d20 by default"]
min = 1
max = 20
if max == 0:
break
if max < 0:
continue
again = True
number_of_dice = int[input["Enter number of dice to roll: "]]
for i in range[number_of_dice - 1]:
print[random.randint[min, max]]
while again:
print[random.randint[min, max]]
reroll = input["Roll again? [y/n]: "]
if reroll.lower[] == "y" or reroll.lower[] == "yes":
for i in range[number_of_dice - 1]:
print[random.randint[min, max]]
else:
print["Thank you"]
break
63. 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
1# dice.py
2
3# ~~~ App's main code block ~~~
4# 1. Get and validate user's input
5num_dice_input = input["How many dice do you want to roll? [1-6] "]
6num_dice = parse_input[num_dice_input]
5 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 import random
while True:
try:
min = 1
max = int[input["Enter the highest value of dice to be rolled or 0 to exit: "]]
except:
print["Your input was invalid, program rolled a d20 by default"]
min = 1
max = 20
if max == 0:
break
if max < 0:
continue
again = True
number_of_dice = int[input["Enter number of dice to roll: "]]
for i in range[number_of_dice - 1]:
print[random.randint[min, max]]
while again:
print[random.randint[min, max]]
reroll = input["Roll again? [y/n]: "]
if reroll.lower[] == "y" or reroll.lower[] == "yes":
for i in range[number_of_dice - 1]:
print[random.randint[min, max]]
else:
print["Thank you"]
break
65 không. Đi trước và thêm phần sau vào tệp import random
while True:
try:
min = 1
max = int[input["Enter the highest value of dice to be rolled or 0 to exit: "]]
except:
print["Your input was invalid, program rolled a d20 by default"]
min = 1
max = 20
if max == 0:
break
if max < 0:
continue
again = True
number_of_dice = int[input["Enter number of dice to roll: "]]
for i in range[number_of_dice - 1]:
print[random.randint[min, max]]
while again:
print[random.randint[min, max]]
reroll = input["Roll again? [y/n]: "]
if reroll.lower[] == "y" or reroll.lower[] == "yes":
for i in range[number_of_dice - 1]:
print[random.randint[min, max]]
else:
print["Thank you"]
break
04 của bạn, ngay trước mã chính của ứng dụng:import random
while True:
try:
min = 1
max = int[input["Enter the highest value of dice to be rolled or 0 to exit: "]]
except:
print["Your input was invalid, program rolled a d20 by default"]
min = 1
max = 20
if max == 0:
break
if max < 0:
continue
again = True
number_of_dice = int[input["Enter number of dice to roll: "]]
for i in range[number_of_dice - 1]:
print[random.randint[min, max]]
while again:
print[random.randint[min, max]]
reroll = input["Roll again? [y/n]: "]
if reroll.lower[] == "y" or reroll.lower[] == "yes":
for i in range[number_of_dice - 1]:
print[random.randint[min, max]]
else:
print["Thank you"]
break
0Ở đây, cách thức mã này hoạt động từng dòng:
Dòng 3 xác định
15, lấy chuỗi đầu vào làm đối số. defines 5, lấy chuỗi đầu vào làm đối số. definesimport random while True: try: min = 1 max = int[input["Enter the highest value of dice to be rolled or 0 to exit: "]] except: print["Your input was invalid, program rolled a d20 by default"] min = 1 max = 20 if max == 0: break if max < 0: continue again = True number_of_dice = int[input["Enter number of dice to roll: "]] for i in range[number_of_dice - 1]: print[random.randint[min, max]] while again: print[random.randint[min, max]] reroll = input["Roll again? [y/n]: "] if reroll.lower[] == "y" or reroll.lower[] == "yes": for i in range[number_of_dice - 1]: print[random.randint[min, max]] else: print["Thank you"] break
5, which takes the input string as an argument.1# dice.py 2 3# ~~~ App's main code block ~~~ 4# 1. Get and validate user's input 5num_dice_input = input["How many dice do you want to roll? [1-6] "] 6num_dice = parse_input[num_dice_input]
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
38 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ửimport random while True: try: min = 1 max = int[input["Enter the highest value of dice to be rolled or 0 to exit: "]] except: print["Your input was invalid, program rolled a d20 by default"] min = 1 max = 20 if max == 0: break if max < 0: continue again = True number_of_dice = int[input["Enter number of dice to roll: "]] for i in range[number_of_dice - 1]: print[random.randint[min, max]] while again: print[random.randint[min, max]] reroll = input["Roll again? [y/n]: "] if reroll.lower[] == "y" or reroll.lower[] == "yes": for i in range[number_of_dice - 1]: print[random.randint[min, max]] else: print["Thank you"] break
69 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 8 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ửimport random while True: try: min = 1 max = int[input["Enter the highest value of dice to be rolled or 0 to exit: "]] except: print["Your input was invalid, program rolled a d20 by default"] min = 1 max = 20 if max == 0: break if max < 0: continue again = True number_of_dice = int[input["Enter number of dice to roll: "]] for i in range[number_of_dice - 1]: print[random.randint[min, max]] while again: print[random.randint[min, max]] reroll = input["Roll again? [y/n]: "] if reroll.lower[] == "y" or reroll.lower[] == "yes": for i in range[number_of_dice - 1]: print[random.randint[min, max]] else: print["Thank you"] break
69 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 toimport random while True: try: min = 1 max = int[input["Enter the highest value of dice to be rolled or 0 to exit: "]] except: print["Your input was invalid, program rolled a d20 by default"] min = 1 max = 20 if max == 0: break if max < 0: continue again = True number_of_dice = int[input["Enter number of dice to roll: "]] for i in range[number_of_dice - 1]: print[random.randint[min, max]] while again: print[random.randint[min, max]] reroll = input["Roll again? [y/n]: "] if reroll.lower[] == "y" or reroll.lower[] == "yes": for i in range[number_of_dice - 1]: print[random.randint[min, max]] else: print["Thank you"] break
68 removes any unwanted spaces around the input string. Theimport random while True: try: min = 1 max = int[input["Enter the highest value of dice to be rolled or 0 to exit: "]] except: print["Your input was invalid, program rolled a d20 by default"] min = 1 max = 20 if max == 0: break if max < 0: continue again = True number_of_dice = int[input["Enter number of dice to roll: "]] for i in range[number_of_dice - 1]: print[random.randint[min, max]] while again: print[random.randint[min, max]] reroll = input["Roll again? [y/n]: "] if reroll.lower[] == "y" or reroll.lower[] == "yes": for i in range[number_of_dice - 1]: print[random.randint[min, max]] else: print["Thank you"] break
69 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.import random while True: try: min = 1 max = int[input["Enter the highest value of dice to be rolled or 0 to exit: "]] except: print["Your input was invalid, program rolled a d20 by default"] min = 1 max = 20 if max == 0: break if max < 0: continue again = True number_of_dice = int[input["Enter number of dice to roll: "]] for i in range[number_of_dice - 1]: print[random.randint[min, max]] while again: print[random.randint[min, max]] reroll = input["Roll again? [y/n]: "] if reroll.lower[] == "y" or reroll.lower[] == "yes": for i in range[number_of_dice - 1]: print[random.randint[min, max]] else: print["Thank you"] break
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ệ
70 và mã trạng thái làimport random while True: try: min = 1 max = int[input["Enter the highest value of dice to be rolled or 0 to exit: "]] except: print["Your input was invalid, program rolled a d20 by default"] min = 1 max = 20 if max == 0: break if max < 0: continue again = True number_of_dice = int[input["Enter number of dice to roll: "]] for i in range[number_of_dice - 1]: print[random.randint[min, max]] while again: print[random.randint[min, max]] reroll = input["Roll again? [y/n]: "] if reroll.lower[] == "y" or reroll.lower[] == "yes": for i in range[number_of_dice - 1]: print[random.randint[min, max]] else: print["Thank you"] break
81 để báo hiệu rằng có điều gì đó không ổn. exits the app with a 0 và mã trạng thái làimport random while True: try: min = 1 max = int[input["Enter the highest value of dice to be rolled or 0 to exit: "]] except: print["Your input was invalid, program rolled a d20 by default"] min = 1 max = 20 if max == 0: break if max < 0: continue again = True number_of_dice = int[input["Enter number of dice to roll: "]] for i in range[number_of_dice - 1]: print[random.randint[min, max]] while again: print[random.randint[min, max]] reroll = input["Roll again? [y/n]: "] if reroll.lower[] == "y" or reroll.lower[] == "yes": for i in range[number_of_dice - 1]: print[random.randint[min, max]] else: print["Thank you"] break
81 để báo hiệu rằng có điều gì đó không ổn. exits the app with aimport random while True: try: min = 1 max = int[input["Enter the highest value of dice to be rolled or 0 to exit: "]] except: print["Your input was invalid, program rolled a d20 by default"] min = 1 max = 20 if max == 0: break if max < 0: continue again = True number_of_dice = int[input["Enter number of dice to roll: "]] for i in range[number_of_dice - 1]: print[random.randint[min, max]] while again: print[random.randint[min, max]] reroll = input["Roll again? [y/n]: "] if reroll.lower[] == "y" or reroll.lower[] == "yes": for i in range[number_of_dice - 1]: print[random.randint[min, max]] else: print["Thank you"] break
80 exception and a status code ofimport random while True: try: min = 1 max = int[input["Enter the highest value of dice to be rolled or 0 to exit: "]] except: print["Your input was invalid, program rolled a d20 by default"] min = 1 max = 20 if max == 0: break if max < 0: continue again = True number_of_dice = int[input["Enter number of dice to roll: "]] for i in range[number_of_dice - 1]: print[random.randint[min, max]] while again: print[random.randint[min, max]] reroll = input["Roll again? [y/n]: "] if reroll.lower[] == "y" or reroll.lower[] == "yes": for i in range[number_of_dice - 1]: print[random.randint[min, max]] else: print["Thank you"] break
81 to signal that something went wrong.import random while True: try: min = 1 max = int[input["Enter the highest value of dice to be rolled or 0 to exit: "]] except: print["Your input was invalid, program rolled a d20 by default"] min = 1 max = 20 if max == 0: break if max < 0: continue again = True number_of_dice = int[input["Enter number of dice to roll: "]] for i in range[number_of_dice - 1]: print[random.randint[min, max]] while again: print[random.randint[min, max]] reroll = input["Roll again? [y/n]: "] if reroll.lower[] == "y" or reroll.lower[] == "yes": for i in range[number_of_dice - 1]: print[random.randint[min, max]] else: print["Thank you"] break
Với
1# dice.py
2
3# ~~~ App's main code block ~~~
4# 1. Get and validate user's input
5num_dice_input = input["How many dice do you want to roll? [1-6] "]
6num_dice = parse_input[num_dice_input]
5, 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
import random
while True:
try:
min = 1
max = int[input["Enter the highest value of dice to be rolled or 0 to exit: "]]
except:
print["Your input was invalid, program rolled a d20 by default"]
min = 1
max = 20
if max == 0:
break
if max < 0:
continue
again = True
number_of_dice = int[input["Enter number of dice to roll: "]]
for i in range[number_of_dice - 1]:
print[random.randint[min, max]]
while again:
print[random.randint[min, max]]
reroll = input["Roll again? [y/n]: "]
if reroll.lower[] == "y" or reroll.lower[] == "yes":
for i in range[number_of_dice - 1]:
print[random.randint[min, max]]
else:
print["Thank you"]
break
04 của bạn:import random
while True:
try:
min = 1
max = int[input["Enter the highest value of dice to be rolled or 0 to exit: "]]
except:
print["Your input was invalid, program rolled a d20 by default"]
min = 1
max = 20
if max == 0:
break
if max < 0:
continue
again = True
number_of_dice = int[input["Enter number of dice to roll: "]]
for i in range[number_of_dice - 1]:
print[random.randint[min, max]]
while again:
print[random.randint[min, max]]
reroll = input["Roll again? [y/n]: "]
if reroll.lower[] == "y" or reroll.lower[] == "yes":
for i in range[number_of_dice - 1]:
print[random.randint[min, max]]
else:
print["Thank you"]
break
6Nế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
3# ~~~ App's main code block ~~~
4# 1. Get and validate user's input
5num_dice_input = input["How many dice do you want to roll? [1-6] "]
6num_dice = parse_input[num_dice_input]
0 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
1# dice.py
2
3# ~~~ App's main code block ~~~
4# 1. Get and validate user's input
5num_dice_input = input["How many dice do you want to roll? [1-6] "]
6num_dice = parse_input[num_dice_input]
8, đ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ừ ____ ____18 từ thư viện tiêu chuẩn cung cấp hàm
1# dice.py
2
3# ~~~ App's main code block ~~~
4# 1. Get and validate user's input
5num_dice_input = input["How many dice do you want to roll? [1-6] "]
6num_dice = parse_input[num_dice_input]
7, 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
import random
while True:
try:
min = 1
max = int[input["Enter the highest value of dice to be rolled or 0 to exit: "]]
except:
print["Your input was invalid, program rolled a d20 by default"]
min = 1
max = 20
if max == 0:
break
if max < 0:
continue
again = True
number_of_dice = int[input["Enter number of dice to roll: "]]
for i in range[number_of_dice - 1]:
print[random.randint[min, max]]
while again:
print[random.randint[min, max]]
reroll = input["Roll again? [y/n]: "]
if reroll.lower[] == "y" or reroll.lower[] == "yes":
for i in range[number_of_dice - 1]:
print[random.randint[min, max]]
else:
print["Thank you"]
break
88:Tại đây, mã thực hiện
1# dice.py
2
3# ~~~ App's main code block ~~~
4# 1. Get and validate user's input
5num_dice_input = input["How many dice do you want to roll? [1-6] "]
6num_dice = parse_input[num_dice_input]
8:import random
while True:
try:
min = 1
max = int[input["Enter the highest value of dice to be rolled or 0 to exit: "]]
except:
print["Your input was invalid, program rolled a d20 by default"]
min = 1
max = 20
if max == 0:
break
if max < 0:
continue
again = True
number_of_dice = int[input["Enter number of dice to roll: "]]
for i in range[number_of_dice - 1]:
print[random.randint[min, max]]
while again:
print[random.randint[min, max]]
reroll = input["Roll again? [y/n]: "]
if reroll.lower[] == "y" or reroll.lower[] == "yes":
for i in range[number_of_dice - 1]:
print[random.randint[min, max]]
else:
print["Thank you"]
break
8Trong đoạn mã này, dòng 2 nhập
import random
while True:
try:
min = 1
max = int[input["Enter the highest value of dice to be rolled or 0 to exit: "]]
except:
print["Your input was invalid, program rolled a d20 by default"]
min = 1
max = 20
if max == 0:
break
if max < 0:
continue
again = True
number_of_dice = int[input["Enter number of dice to roll: "]]
for i in range[number_of_dice - 1]:
print[random.randint[min, max]]
while again:
print[random.randint[min, max]]
reroll = input["Roll again? [y/n]: "]
if reroll.lower[] == "y" or reroll.lower[] == "yes":
for i in range[number_of_dice - 1]:
print[random.randint[min, max]]
else:
print["Thank you"]
break
8 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 1# dice.py
2
3# ~~~ App's main code block ~~~
4# 1. Get and validate user's input
5num_dice_input = input["How many dice do you want to roll? [1-6] "]
6num_dice = parse_input[num_dice_input]
7 sau. Ở đây, một sự cố của phần còn lại của mã:Dòng 6 xác định
18, 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 8, 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. definesimport random while True: try: min = 1 max = int[input["Enter the highest value of dice to be rolled or 0 to exit: "]] except: print["Your input was invalid, program rolled a d20 by default"] min = 1 max = 20 if max == 0: break if max < 0: continue again = True number_of_dice = int[input["Enter number of dice to roll: "]] for i in range[number_of_dice - 1]: print[random.randint[min, max]] while again: print[random.randint[min, max]] reroll = input["Roll again? [y/n]: "] if reroll.lower[] == "y" or reroll.lower[] == "yes": for i in range[number_of_dice - 1]: print[random.randint[min, max]] else: print["Thank you"] break
8, which takes an argument representing the number of dice to roll in a given call.1# dice.py 2 3# ~~~ App's main code block ~~~ 4# 1. Get and validate user's input 5num_dice_input = input["How many dice do you want to roll? [1-6] "] 6num_dice = parse_input[num_dice_input]
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,
43, để lưu trữ kết quả của mô phỏng xúc xắc. creates an empty list, 3, để lưu trữ kết quả của mô phỏng xúc xắc. creates an empty list,import random while True: try: min = 1 max = int[input["Enter the highest value of dice to be rolled or 0 to exit: "]] except: print["Your input was invalid, program rolled a d20 by default"] min = 1 max = 20 if max == 0: break if max < 0: continue again = True number_of_dice = int[input["Enter number of dice to roll: "]] for i in range[number_of_dice - 1]: print[random.randint[min, max]] while again: print[random.randint[min, max]] reroll = input["Roll again? [y/n]: "] if reroll.lower[] == "y" or reroll.lower[] == "yes": for i in range[number_of_dice - 1]: print[random.randint[min, max]] else: print["Thank you"] break
53, to store the results of the dice-rolling simulation.import random while True: try: min = 1 max = int[input["Enter the highest value of dice to be rolled or 0 to exit: "]] except: print["Your input was invalid, program rolled a d20 by default"] min = 1 max = 20 if max == 0: break if max < 0: continue again = True number_of_dice = int[input["Enter number of dice to roll: "]] for i in range[number_of_dice - 1]: print[random.randint[min, max]] while again: print[random.randint[min, max]] reroll = input["Roll again? [y/n]: "] if reroll.lower[] == "y" or reroll.lower[] == "yes": for i in range[number_of_dice - 1]: print[random.randint[min, max]] else: print["Thank you"] break
Dòng 13 xác định vòng lặp
66 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 6 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 aimport random while True: try: min = 1 max = int[input["Enter the highest value of dice to be rolled or 0 to exit: "]] except: print["Your input was invalid, program rolled a d20 by default"] min = 1 max = 20 if max == 0: break if max < 0: continue again = True number_of_dice = int[input["Enter number of dice to roll: "]] for i in range[number_of_dice - 1]: print[random.randint[min, max]] while again: print[random.randint[min, max]] reroll = input["Roll again? [y/n]: "] if reroll.lower[] == "y" or reroll.lower[] == "yes": for i in range[number_of_dice - 1]: print[random.randint[min, max]] else: print["Thank you"] break
6 loop that iterates once for each die that the user wants to roll.import random while True: try: min = 1 max = int[input["Enter the highest value of dice to be rolled or 0 to exit: "]] except: print["Your input was invalid, program rolled a d20 by default"] min = 1 max = 20 if max == 0: break if max < 0: continue again = True number_of_dice = int[input["Enter number of dice to roll: "]] for i in range[number_of_dice - 1]: print[random.randint[min, max]] while again: print[random.randint[min, max]] reroll = input["Roll again? [y/n]: "] if reroll.lower[] == "y" or reroll.lower[] == "yes": for i in range[number_of_dice - 1]: print[random.randint[min, max]] else: print["Thank you"] break
Dòng 14 gọi
17 để 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 7 để 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. callsimport random while True: try: min = 1 max = int[input["Enter the highest value of dice to be rolled or 0 to exit: "]] except: print["Your input was invalid, program rolled a d20 by default"] min = 1 max = 20 if max == 0: break if max < 0: continue again = True number_of_dice = int[input["Enter number of dice to roll: "]] for i in range[number_of_dice - 1]: print[random.randint[min, max]] while again: print[random.randint[min, max]] reroll = input["Roll again? [y/n]: "] if reroll.lower[] == "y" or reroll.lower[] == "yes": for i in range[number_of_dice - 1]: print[random.randint[min, max]] else: print["Thank you"] break
7 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.1# dice.py 2 3# ~~~ App's main code block ~~~ 4# 1. Get and validate user's input 5num_dice_input = input["How many dice do you want to roll? [1-6] "] 6num_dice = parse_input[num_dice_input]
Dòng 15 cộng đồng kết quả lăn chết hiện tại trong
43. appends the current die-rolling result in 3. appends the current die-rolling result inimport random while True: try: min = 1 max = int[input["Enter the highest value of dice to be rolled or 0 to exit: "]] except: print["Your input was invalid, program rolled a d20 by default"] min = 1 max = 20 if max == 0: break if max < 0: continue again = True number_of_dice = int[input["Enter number of dice to roll: "]] for i in range[number_of_dice - 1]: print[random.randint[min, max]] while again: print[random.randint[min, max]] reroll = input["Roll again? [y/n]: "] if reroll.lower[] == "y" or reroll.lower[] == "yes": for i in range[number_of_dice - 1]: print[random.randint[min, max]] else: print["Thank you"] break
53.import random while True: try: min = 1 max = int[input["Enter the highest value of dice to be rolled or 0 to exit: "]] except: print["Your input was invalid, program rolled a d20 by default"] min = 1 max = 20 if max == 0: break if max < 0: continue again = True number_of_dice = int[input["Enter number of dice to roll: "]] for i in range[number_of_dice - 1]: print[random.randint[min, max]] while again: print[random.randint[min, max]] reroll = input["Roll again? [y/n]: "] if reroll.lower[] == "y" or reroll.lower[] == "yes": for i in range[number_of_dice - 1]: print[random.randint[min, max]] else: print["Thank you"] break
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
import random
while True:
try:
min = 1
max = int[input["Enter the highest value of dice to be rolled or 0 to exit: "]]
except:
print["Your input was invalid, program rolled a d20 by default"]
min = 1
max = 20
if max == 0:
break
if max < 0:
continue
again = True
number_of_dice = int[input["Enter number of dice to roll: "]]
for i in range[number_of_dice - 1]:
print[random.randint[min, max]]
while again:
print[random.randint[min, max]]
reroll = input["Roll again? [y/n]: "]
if reroll.lower[] == "y" or reroll.lower[] == "yes":
for i in range[number_of_dice - 1]:
print[random.randint[min, max]]
else:
print["Thank you"]
break
04 của bạn:import random
while True:
try:
min = 1
max = int[input["Enter the highest value of dice to be rolled or 0 to exit: "]]
except:
print["Your input was invalid, program rolled a d20 by default"]
min = 1
max = 20
if max == 0:
break
if max < 0:
continue
again = True
number_of_dice = int[input["Enter number of dice to roll: "]]
for i in range[number_of_dice - 1]:
print[random.randint[min, max]]
while again:
print[random.randint[min, max]]
reroll = input["Roll again? [y/n]: "]
if reroll.lower[] == "y" or reroll.lower[] == "yes":
for i in range[number_of_dice - 1]:
print[random.randint[min, max]]
else:
print["Thank you"]
break
5Trong mã này, dòng 9 gọi
1# dice.py
2
3# ~~~ App's main code block ~~~
4# 1. Get and validate user's input
5num_dice_input = input["How many dice do you want to roll? [1-6] "]
6num_dice = parse_input[num_dice_input]
8 với import random
while True:
try:
min = 1
max = int[input["Enter the highest value of dice to be rolled or 0 to exit: "]]
except:
print["Your input was invalid, program rolled a d20 by default"]
min = 1
max = 20
if max == 0:
break
if max < 0:
continue
again = True
number_of_dice = int[input["Enter number of dice to roll: "]]
for i in range[number_of_dice - 1]:
print[random.randint[min, max]]
while again:
print[random.randint[min, max]]
reroll = input["Roll again? [y/n]: "]
if reroll.lower[] == "y" or reroll.lower[] == "yes":
for i in range[number_of_dice - 1]:
print[random.randint[min, max]]
else:
print["Thank you"]
break
63 như một đối số. Dòng 11 gọi import random
while True:
try:
min = 1
max = int[input["Enter the highest value of dice to be rolled or 0 to exit: "]]
except:
print["Your input was invalid, program rolled a d20 by default"]
min = 1
max = 20
if max == 0:
break
if max < 0:
continue
again = True
number_of_dice = int[input["Enter number of dice to roll: "]]
for i in range[number_of_dice - 1]:
print[random.randint[min, max]]
while again:
print[random.randint[min, max]]
reroll = input["Roll again? [y/n]: "]
if reroll.lower[] == "y" or reroll.lower[] == "yes":
for i in range[number_of_dice - 1]:
print[random.randint[min, max]]
else:
print["Thank you"]
break
7 để 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
3# ~~~ App's main code block ~~~
4# 1. Get and validate user's input
5num_dice_input = input["How many dice do you want to roll? [1-6] "]
6num_dice = parse_input[num_dice_input]
7Danh 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
3# ~~~ App's main code block ~~~
4# 1. Get and validate user's input
5num_dice_input = input["How many dice do you want to roll? [1-6] "]
6num_dice = parse_input[num_dice_input]
71: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
3# ~~~ App's main code block ~~~
4# 1. Get and validate user's input
5num_dice_input = input["How many dice do you want to roll? [1-6] "]
6num_dice = parse_input[num_dice_input]
9Mỗ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
3# ~~~ App's main code block ~~~
4# 1. Get and validate user's input
5num_dice_input = input["How many dice do you want to roll? [1-6] "]
6num_dice = parse_input[num_dice_input]
0Trong 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
3# ~~~ App's main code block ~~~
4# 1. Get and validate user's input
5num_dice_input = input["How many dice do you want to roll? [1-6] "]
6num_dice = parse_input[num_dice_input]
72, 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
3# ~~~ App's main code block ~~~
4# 1. Get and validate user's input
5num_dice_input = input["How many dice do you want to roll? [1-6] "]
6num_dice = parse_input[num_dice_input]
73, 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
3# ~~~ App's main code block ~~~
4# 1. Get and validate user's input
5num_dice_input = input["How many dice do you want to roll? [1-6] "]
6num_dice = parse_input[num_dice_input]
74 để 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
3# ~~~ App's main code block ~~~
4# 1. Get and validate user's input
5num_dice_input = input["How many dice do you want to roll? [1-6] "]
6num_dice = parse_input[num_dice_input]
75, 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:
import random
while True:
try:
min = 1
max = int[input["Enter the highest value of dice to be rolled or 0 to exit: "]]
except:
print["Your input was invalid, program rolled a d20 by default"]
min = 1
max = 20
if max == 0:
break
if max < 0:
continue
again = True
number_of_dice = int[input["Enter number of dice to roll: "]]
for i in range[number_of_dice - 1]:
print[random.randint[min, max]]
while again:
print[random.randint[min, max]]
reroll = input["Roll again? [y/n]: "]
if reroll.lower[] == "y" or reroll.lower[] == "yes":
for i in range[number_of_dice - 1]:
print[random.randint[min, max]]
else:
print["Thank you"]
break
0Chức năng này làm như sau:
Dòng 5 xác định
61 với một đối số duy nhất được gọi là1# dice.py 2 3# ~~~ App's main code block ~~~ 4# 1. Get and validate user's input 5num_dice_input = input["How many dice do you want to roll? [1-6] "] 6num_dice = parse_input[num_dice_input]
77. Đố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ọi1# dice.py 2 3# ~~~ App's main code block ~~~ 4# 1. Get and validate user's input 5num_dice_input = input["How many dice do you want to roll? [1-6] "] 6num_dice = parse_input[num_dice_input]
8. defines 1 với một đối số duy nhất được gọi là1# dice.py 2 3# ~~~ App's main code block ~~~ 4# 1. Get and validate user's input 5num_dice_input = input["How many dice do you want to roll? [1-6] "] 6num_dice = parse_input[num_dice_input]
77. Đố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ọi1# dice.py 2 3# ~~~ App's main code block ~~~ 4# 1. Get and validate user's input 5num_dice_input = input["How many dice do you want to roll? [1-6] "] 6num_dice = parse_input[num_dice_input]
8. defines1# dice.py 2 3# ~~~ App's main code block ~~~ 4# 1. Get and validate user's input 5num_dice_input = input["How many dice do you want to roll? [1-6] "] 6num_dice = parse_input[num_dice_input]
01 with a single argument calledimport random while True: try: min = 1 max = int[input["Enter the highest value of dice to be rolled or 0 to exit: "]] except: print["Your input was invalid, program rolled a d20 by default"] min = 1 max = 20 if max == 0: break if max < 0: continue again = True number_of_dice = int[input["Enter number of dice to roll: "]] for i in range[number_of_dice - 1]: print[random.randint[min, max]] while again: print[random.randint[min, max]] reroll = input["Roll again? [y/n]: "] if reroll.lower[] == "y" or reroll.lower[] == "yes": for i in range[number_of_dice - 1]: print[random.randint[min, max]] else: print["Thank you"] break
77. This argument will hold the list of dice-rolling integer values that results from calling1# dice.py 2 3# ~~~ App's main code block ~~~ 4# 1. Get and validate user's input 5num_dice_input = input["How many dice do you want to roll? [1-6] "] 6num_dice = parse_input[num_dice_input]
8.1# dice.py 2 3# ~~~ App's main code block ~~~ 4# 1. Get and validate user's input 5num_dice_input = input["How many dice do you want to roll? [1-6] "] 6num_dice = parse_input[num_dice_input]
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
29 để 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 9 để 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 called1# dice.py 2 3# ~~~ App's main code block ~~~ 4# 1. Get and validate user's input 5num_dice_input = input["How many dice do you want to roll? [1-6] "] 6num_dice = parse_input[num_dice_input]
79 to store the dice faces corresponding to the input list of dice values. These dice faces will display in the final ASCII diagram.1# dice.py 2 3# ~~~ App's main code block ~~~ 4# 1. Get and validate user's input 5num_dice_input = input["How many dice do you want to roll? [1-6] "] 6num_dice = parse_input[num_dice_input]
Dòng 21 định nghĩa một vòng
66 để lặp lại các giá trị xúc xắc. defines a 6 để lặp lại các giá trị xúc xắc. defines aimport random while True: try: min = 1 max = int[input["Enter the highest value of dice to be rolled or 0 to exit: "]] except: print["Your input was invalid, program rolled a d20 by default"] min = 1 max = 20 if max == 0: break if max < 0: continue again = True number_of_dice = int[input["Enter number of dice to roll: "]] for i in range[number_of_dice - 1]: print[random.randint[min, max]] while again: print[random.randint[min, max]] reroll = input["Roll again? [y/n]: "] if reroll.lower[] == "y" or reroll.lower[] == "yes": for i in range[number_of_dice - 1]: print[random.randint[min, max]] else: print["Thank you"] break
6 loop to iterate over the dice values.import random while True: try: min = 1 max = int[input["Enter the highest value of dice to be rolled or 0 to exit: "]] except: print["Your input was invalid, program rolled a d20 by default"] min = 1 max = 20 if max == 0: break if max < 0: continue again = True number_of_dice = int[input["Enter number of dice to roll: "]] for i in range[number_of_dice - 1]: print[random.randint[min, max]] while again: print[random.randint[min, max]] reroll = input["Roll again? [y/n]: "] if reroll.lower[] == "y" or reroll.lower[] == "yes": for i in range[number_of_dice - 1]: print[random.randint[min, max]] else: print["Thank you"] break
Dòng 22 lấy mặt chết tương ứng với giá trị khuôn hiện tại từ
22 và nối nó vào1# dice.py 2 3# ~~~ App's main code block ~~~ 4# 1. Get and validate user's input 5num_dice_input = input["How many dice do you want to roll? [1-6] "] 6num_dice = parse_input[num_dice_input]
79. retrieves the die face corresponding to the current die value from 2 và nối nó vào1# dice.py 2 3# ~~~ App's main code block ~~~ 4# 1. Get and validate user's input 5num_dice_input = input["How many dice do you want to roll? [1-6] "] 6num_dice = parse_input[num_dice_input]
79. retrieves the die face corresponding to the current die value from1# dice.py 2 3# ~~~ App's main code block ~~~ 4# 1. Get and validate user's input 5num_dice_input = input["How many dice do you want to roll? [1-6] "] 6num_dice = parse_input[num_dice_input]
72 and appends it to1# dice.py 2 3# ~~~ App's main code block ~~~ 4# 1. Get and validate user's input 5num_dice_input = input["How many dice do you want to roll? [1-6] "] 6num_dice = parse_input[num_dice_input]
79.1# dice.py 2 3# ~~~ App's main code block ~~~ 4# 1. Get and validate user's input 5num_dice_input = input["How many dice do you want to roll? [1-6] "] 6num_dice = parse_input[num_dice_input]
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ừ
003 đếnimport random while True: try: min = 1 max = int[input["Enter the highest value of dice to be rolled or 0 to exit: "]] except: print["Your input was invalid, program rolled a d20 by default"] min = 1 max = 20 if max == 0: break if max < 0: continue again = True number_of_dice = int[input["Enter number of dice to roll: "]] for i in range[number_of_dice - 1]: print[random.randint[min, max]] while again: print[random.randint[min, max]] reroll = input["Roll again? [y/n]: "] if reroll.lower[] == "y" or reroll.lower[] == "yes": for i in range[number_of_dice - 1]: print[random.randint[min, max]] else: print["Thank you"] break
94. 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 3 đến1# dice.py 2 3# ~~~ App's main code block ~~~ 4# 1. Get and validate user's input 5num_dice_input = input["How many dice do you want to roll? [1-6] "] 6num_dice = parse_input[num_dice_input]
94. 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 from1# dice.py 2 3# ~~~ App's main code block ~~~ 4# 1. Get and validate user's input 5num_dice_input = input["How many dice do you want to roll? [1-6] "] 6num_dice = parse_input[num_dice_input]
93 to1# dice.py 2 3# ~~~ App's main code block ~~~ 4# 1. Get and validate user's input 5num_dice_input = input["How many dice do you want to roll? [1-6] "] 6num_dice = parse_input[num_dice_input]
94. Each index represents the index of a given row in the dice faces diagram.1# dice.py 2 3# ~~~ App's main code block ~~~ 4# 1. Get and validate user's input 5num_dice_input = input["How many dice do you want to roll? [1-6] "] 6num_dice = parse_input[num_dice_input]
Dòng 27 Xác định
005 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 5 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. definesimport random while True: try: min = 1 max = int[input["Enter the highest value of dice to be rolled or 0 to exit: "]] except: print["Your input was invalid, program rolled a d20 by default"] min = 1 max = 20 if max == 0: break if max < 0: continue again = True number_of_dice = int[input["Enter number of dice to roll: "]] for i in range[number_of_dice - 1]: print[random.randint[min, max]] while again: print[random.randint[min, max]] reroll = input["Roll again? [y/n]: "] if reroll.lower[] == "y" or reroll.lower[] == "yes": for i in range[number_of_dice - 1]: print[random.randint[min, max]] else: print["Thank you"] break
95 as an empty list to hold portions of the dice faces that will fill a given row.1# dice.py 2 3# ~~~ App's main code block ~~~ 4# 1. Get and validate user's input 5num_dice_input = input["How many dice do you want to roll? [1-6] "] 6num_dice = parse_input[num_dice_input]
Dòng 28 bắt đầu một vòng
66 lồng nhau để lặp lại các mặt xúc xắc. starts a nested 6 lồng nhau để lặp lại các mặt xúc xắc. starts a nestedimport random while True: try: min = 1 max = int[input["Enter the highest value of dice to be rolled or 0 to exit: "]] except: print["Your input was invalid, program rolled a d20 by default"] min = 1 max = 20 if max == 0: break if max < 0: continue again = True number_of_dice = int[input["Enter number of dice to roll: "]] for i in range[number_of_dice - 1]: print[random.randint[min, max]] while again: print[random.randint[min, max]] reroll = input["Roll again? [y/n]: "] if reroll.lower[] == "y" or reroll.lower[] == "yes": for i in range[number_of_dice - 1]: print[random.randint[min, max]] else: print["Thank you"] break
6 loop to iterate over the dice faces.import random while True: try: min = 1 max = int[input["Enter the highest value of dice to be rolled or 0 to exit: "]] except: print["Your input was invalid, program rolled a d20 by default"] min = 1 max = 20 if max == 0: break if max < 0: continue again = True number_of_dice = int[input["Enter number of dice to roll: "]] for i in range[number_of_dice - 1]: print[random.randint[min, max]] while again: print[random.randint[min, max]] reroll = input["Roll again? [y/n]: "] if reroll.lower[] == "y" or reroll.lower[] == "yes": for i in range[number_of_dice - 1]: print[random.randint[min, max]] else: print["Thank you"] break
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ữ
007 của sơ đồ Dice hiện tại. creates a temporary variable to hold the 7 của sơ đồ Dice hiện tại. creates a temporary variable to hold theimport random while True: try: min = 1 max = int[input["Enter the highest value of dice to be rolled or 0 to exit: "]] except: print["Your input was invalid, program rolled a d20 by default"] min = 1 max = 20 if max == 0: break if max < 0: continue again = True number_of_dice = int[input["Enter number of dice to roll: "]] for i in range[number_of_dice - 1]: print[random.randint[min, max]] while again: print[random.randint[min, max]] reroll = input["Roll again? [y/n]: "] if reroll.lower[] == "y" or reroll.lower[] == "yes": for i in range[number_of_dice - 1]: print[random.randint[min, max]] else: print["Thank you"] break
97 of the current dice faces diagram.1# dice.py 2 3# ~~~ App's main code block ~~~ 4# 1. Get and validate user's input 5num_dice_input = input["How many dice do you want to roll? [1-6] "] 6num_dice = parse_input[num_dice_input]
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
008 với sơ đồimport random while True: try: min = 1 max = int[input["Enter the highest value of dice to be rolled or 0 to exit: "]] except: print["Your input was invalid, program rolled a d20 by default"] min = 1 max = 20 if max == 0: break if max < 0: continue again = True number_of_dice = int[input["Enter number of dice to roll: "]] for i in range[number_of_dice - 1]: print[random.randint[min, max]] while again: print[random.randint[min, max]] reroll = input["Roll again? [y/n]: "] if reroll.lower[] == "y" or reroll.lower[] == "yes": for i in range[number_of_dice - 1]: print[random.randint[min, max]] else: print["Thank you"] break
97 và ký hiệu Tilde [1# dice.py 2 3# ~~~ App's main code block ~~~ 4# 1. Get and validate user's input 5num_dice_input = input["How many dice do you want to roll? [1-6] "] 6num_dice = parse_input[num_dice_input]
00] làm đối số. creates a header showing the word RESULTS. To do so, it uses8 với sơ đồ1# dice.py 2 3# ~~~ App's main code block ~~~ 4# 1. Get and validate user's input 5num_dice_input = input["How many dice do you want to roll? [1-6] "] 6num_dice = parse_input[num_dice_input]
97 và ký hiệu Tilde [1# dice.py 2 3# ~~~ App's main code block ~~~ 4# 1. Get and validate user's input 5num_dice_input = input["How many dice do you want to roll? [1-6] "] 6num_dice = parse_input[num_dice_input]
00] làm đối số. creates a header showing the word RESULTS. To do so, it uses1# dice.py 2 3# ~~~ App's main code block ~~~ 4# 1. Get and validate user's input 5num_dice_input = input["How many dice do you want to roll? [1-6] "] 6num_dice = parse_input[num_dice_input]
98 with the diagram’s1# dice.py 2 3# ~~~ App's main code block ~~~ 4# 1. Get and validate user's input 5num_dice_input = input["How many dice do you want to roll? [1-6] "] 6num_dice = parse_input[num_dice_input]
97 and the tilde symbol [1# dice.py 2 3# ~~~ App's main code block ~~~ 4# 1. Get and validate user's input 5num_dice_input = input["How many dice do you want to roll? [1-6] "] 6num_dice = parse_input[num_dice_input]
00] as arguments.1# dice.py 2 3# ~~~ App's main code block ~~~ 4# 1. Get and validate user's input 5num_dice_input = input["How many dice do you want to roll? [1-6] "] 6num_dice = parse_input[num_dice_input]
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 [
161] hoạt động như một dấu phân cách hàng. Đối số củaimport random while True: try: min = 1 max = int[input["Enter the highest value of dice to be rolled or 0 to exit: "]] except: print["Your input was invalid, program rolled a d20 by default"] min = 1 max = 20 if max == 0: break if max < 0: continue again = True number_of_dice = int[input["Enter number of dice to roll: "]] for i in range[number_of_dice - 1]: print[random.randint[min, max]] while again: print[random.randint[min, max]] reroll = input["Roll again? [y/n]: "] if reroll.lower[] == "y" or reroll.lower[] == "yes": for i in range[number_of_dice - 1]: print[random.randint[min, max]] else: print["Thank you"] break
2 là một danh sách các chuỗi liên kết tiêu đề sơ đồ và các chuỗi [hàng] hình thành các mặt xúc xắc. generates a string that holds the final dice faces diagram. The line feed character [1] hoạt động như một dấu phân cách hàng. Đối số của1# dice.py 2 3# ~~~ App's main code block ~~~ 4# 1. Get and validate user's input 5num_dice_input = input["How many dice do you want to roll? [1-6] "] 6num_dice = parse_input[num_dice_input]
2 là một danh sách các chuỗi liên kết tiêu đề sơ đồ và các chuỗi [hàng] hình thành các mặt xúc xắc. generates a string that holds the final dice faces diagram. The line feed character [1# dice.py 2 3# ~~~ App's main code block ~~~ 4# 1. Get and validate user's input 5num_dice_input = input["How many dice do you want to roll? [1-6] "] 6num_dice = parse_input[num_dice_input]
01] works as a row separator. The argument to1# dice.py 2 3# ~~~ App's main code block ~~~ 4# 1. Get and validate user's input 5num_dice_input = input["How many dice do you want to roll? [1-6] "] 6num_dice = parse_input[num_dice_input]
2 is a list of strings concatenating the diagram header and the strings [rows] that shape the dice faces.1# dice.py 2 3# ~~~ App's main code block ~~~ 4# 1. Get and validate user's input 5num_dice_input = input["How many dice do you want to roll? [1-6] "] 6num_dice = parse_input[num_dice_input]
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
import random
while True:
try:
min = 1
max = int[input["Enter the highest value of dice to be rolled or 0 to exit: "]]
except:
print["Your input was invalid, program rolled a d20 by default"]
min = 1
max = 20
if max == 0:
break
if max < 0:
continue
again = True
number_of_dice = int[input["Enter number of dice to roll: "]]
for i in range[number_of_dice - 1]:
print[random.randint[min, max]]
while again:
print[random.randint[min, max]]
reroll = input["Roll again? [y/n]: "]
if reroll.lower[] == "y" or reroll.lower[] == "yes":
for i in range[number_of_dice - 1]:
print[random.randint[min, max]]
else:
print["Thank you"]
break
01 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 import random
while True:
try:
min = 1
max = int[input["Enter the highest value of dice to be rolled or 0 to exit: "]]
except:
print["Your input was invalid, program rolled a d20 by default"]
min = 1
max = 20
if max == 0:
break
if max < 0:
continue
again = True
number_of_dice = int[input["Enter number of dice to roll: "]]
for i in range[number_of_dice - 1]:
print[random.randint[min, max]]
while again:
print[random.randint[min, max]]
reroll = input["Roll again? [y/n]: "]
if reroll.lower[] == "y" or reroll.lower[] == "yes":
for i in range[number_of_dice - 1]:
print[random.randint[min, max]]
else:
print["Thank you"]
break
04:import random
while True:
try:
min = 1
max = int[input["Enter the highest value of dice to be rolled or 0 to exit: "]]
except:
print["Your input was invalid, program rolled a d20 by default"]
min = 1
max = 20
if max == 0:
break
if max < 0:
continue
again = True
number_of_dice = int[input["Enter number of dice to roll: "]]
for i in range[number_of_dice - 1]:
print[random.randint[min, max]]
while again:
print[random.randint[min, max]]
reroll = input["Roll again? [y/n]: "]
if reroll.lower[] == "y" or reroll.lower[] == "yes":
for i in range[number_of_dice - 1]:
print[random.randint[min, max]]
else:
print["Thank you"]
break
1Dòng 12 gọi
import random
while True:
try:
min = 1
max = int[input["Enter the highest value of dice to be rolled or 0 to exit: "]]
except:
print["Your input was invalid, program rolled a d20 by default"]
min = 1
max = 20
if max == 0:
break
if max < 0:
continue
again = True
number_of_dice = int[input["Enter number of dice to roll: "]]
for i in range[number_of_dice - 1]:
print[random.randint[min, max]]
while again:
print[random.randint[min, max]]
reroll = input["Roll again? [y/n]: "]
if reroll.lower[] == "y" or reroll.lower[] == "yes":
for i in range[number_of_dice - 1]:
print[random.randint[min, max]]
else:
print["Thank you"]
break
01 với import random
while True:
try:
min = 1
max = int[input["Enter the highest value of dice to be rolled or 0 to exit: "]]
except:
print["Your input was invalid, program rolled a d20 by default"]
min = 1
max = 20
if max == 0:
break
if max < 0:
continue
again = True
number_of_dice = int[input["Enter number of dice to roll: "]]
for i in range[number_of_dice - 1]:
print[random.randint[min, max]]
while again:
print[random.randint[min, max]]
reroll = input["Roll again? [y/n]: "]
if reroll.lower[] == "y" or reroll.lower[] == "yes":
for i in range[number_of_dice - 1]:
print[random.randint[min, max]]
else:
print["Thank you"]
break
53 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 import random
while True:
try:
min = 1
max = int[input["Enter the highest value of dice to be rolled or 0 to exit: "]]
except:
print["Your input was invalid, program rolled a d20 by default"]
min = 1
max = 20
if max == 0:
break
if max < 0:
continue
again = True
number_of_dice = int[input["Enter number of dice to roll: "]]
for i in range[number_of_dice - 1]:
print[random.randint[min, max]]
while again:
print[random.randint[min, max]]
reroll = input["Roll again? [y/n]: "]
if reroll.lower[] == "y" or reroll.lower[] == "yes":
for i in range[number_of_dice - 1]:
print[random.randint[min, max]]
else:
print["Thank you"]
break
7 để 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:
import random
while True:
try:
min = 1
max = int[input["Enter the highest value of dice to be rolled or 0 to exit: "]]
except:
print["Your input was invalid, program rolled a d20 by default"]
min = 1
max = 20
if max == 0:
break
if max < 0:
continue
again = True
number_of_dice = int[input["Enter number of dice to roll: "]]
for i in range[number_of_dice - 1]:
print[random.randint[min, max]]
while again:
print[random.randint[min, max]]
reroll = input["Roll again? [y/n]: "]
if reroll.lower[] == "y" or reroll.lower[] == "yes":
for i in range[number_of_dice - 1]:
print[random.randint[min, max]]
else:
print["Thank you"]
break
2Má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
import random
while True:
try:
min = 1
max = int[input["Enter the highest value of dice to be rolled or 0 to exit: "]]
except:
print["Your input was invalid, program rolled a d20 by default"]
min = 1
max = 20
if max == 0:
break
if max < 0:
continue
again = True
number_of_dice = int[input["Enter number of dice to roll: "]]
for i in range[number_of_dice - 1]:
print[random.randint[min, max]]
while again:
print[random.randint[min, max]]
reroll = input["Roll again? [y/n]: "]
if reroll.lower[] == "y" or reroll.lower[] == "yes":
for i in range[number_of_dice - 1]:
print[random.randint[min, max]]
else:
print["Thank you"]
break
01, 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ì:import random
while True:
try:
min = 1
max = int[input["Enter the highest value of dice to be rolled or 0 to exit: "]]
except:
print["Your input was invalid, program rolled a d20 by default"]
min = 1
max = 20
if max == 0:
break
if max < 0:
continue
again = True
number_of_dice = int[input["Enter number of dice to roll: "]]
for i in range[number_of_dice - 1]:
print[random.randint[min, max]]
while again:
print[random.randint[min, max]]
reroll = input["Roll again? [y/n]: "]
if reroll.lower[] == "y" or reroll.lower[] == "yes":
for i in range[number_of_dice - 1]:
print[random.randint[min, max]]
else:
print["Thank you"]
break
3Loạ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
import random
while True:
try:
min = 1
max = int[input["Enter the highest value of dice to be rolled or 0 to exit: "]]
except:
print["Your input was invalid, program rolled a d20 by default"]
min = 1
max = 20
if max == 0:
break
if max < 0:
continue
again = True
number_of_dice = int[input["Enter number of dice to roll: "]]
for i in range[number_of_dice - 1]:
print[random.randint[min, max]]
while again:
print[random.randint[min, max]]
reroll = input["Roll again? [y/n]: "]
if reroll.lower[] == "y" or reroll.lower[] == "yes":
for i in range[number_of_dice - 1]:
print[random.randint[min, max]]
else:
print["Thank you"]
break
01 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
import random
while True:
try:
min = 1
max = int[input["Enter the highest value of dice to be rolled or 0 to exit: "]]
except:
print["Your input was invalid, program rolled a d20 by default"]
min = 1
max = 20
if max == 0:
break
if max < 0:
continue
again = True
number_of_dice = int[input["Enter number of dice to roll: "]]
for i in range[number_of_dice - 1]:
print[random.randint[min, max]]
while again:
print[random.randint[min, max]]
reroll = input["Roll again? [y/n]: "]
if reroll.lower[] == "y" or reroll.lower[] == "yes":
for i in range[number_of_dice - 1]:
print[random.randint[min, max]]
else:
print["Thank you"]
break
00: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
import random
while True:
try:
min = 1
max = int[input["Enter the highest value of dice to be rolled or 0 to exit: "]]
except:
print["Your input was invalid, program rolled a d20 by default"]
min = 1
max = 20
if max == 0:
break
if max < 0:
continue
again = True
number_of_dice = int[input["Enter number of dice to roll: "]]
for i in range[number_of_dice - 1]:
print[random.randint[min, max]]
while again:
print[random.randint[min, max]]
reroll = input["Roll again? [y/n]: "]
if reroll.lower[] == "y" or reroll.lower[] == "yes":
for i in range[number_of_dice - 1]:
print[random.randint[min, max]]
else:
print["Thank you"]
break
01 trước đó và đặt nó vào chức năng trợ giúp không công khai gọi là import random
while True:
try:
min = 1
max = int[input["Enter the highest value of dice to be rolled or 0 to exit: "]]
except:
print["Your input was invalid, program rolled a d20 by default"]
min = 1
max = 20
if max == 0:
break
if max < 0:
continue
again = True
number_of_dice = int[input["Enter number of dice to roll: "]]
for i in range[number_of_dice - 1]:
print[random.randint[min, max]]
while again:
print[random.randint[min, max]]
reroll = input["Roll again? [y/n]: "]
if reroll.lower[] == "y" or reroll.lower[] == "yes":
for i in range[number_of_dice - 1]:
print[random.randint[min, max]]
else:
print["Thank you"]
break
02: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 import random
while True:
try:
min = 1
max = int[input["Enter the highest value of dice to be rolled or 0 to exit: "]]
except:
print["Your input was invalid, program rolled a d20 by default"]
min = 1
max = 20
if max == 0:
break
if max < 0:
continue
again = True
number_of_dice = int[input["Enter number of dice to roll: "]]
for i in range[number_of_dice - 1]:
print[random.randint[min, max]]
while again:
print[random.randint[min, max]]
reroll = input["Roll again? [y/n]: "]
if reroll.lower[] == "y" or reroll.lower[] == "yes":
for i in range[number_of_dice - 1]:
print[random.randint[min, max]]
else:
print["Thank you"]
break
01 and place it in a non-public helper function called import random
while True:
try:
min = 1
max = int[input["Enter the highest value of dice to be rolled or 0 to exit: "]]
except:
print["Your input was invalid, program rolled a d20 by default"]
min = 1
max = 20
if max == 0:
break
if max < 0:
continue
again = True
number_of_dice = int[input["Enter number of dice to roll: "]]
for i in range[number_of_dice - 1]:
print[random.randint[min, max]]
while again:
print[random.randint[min, max]]
reroll = input["Roll again? [y/n]: "]
if reroll.lower[] == "y" or reroll.lower[] == "yes":
for i in range[number_of_dice - 1]:
print[random.randint[min, max]]
else:
print["Thank you"]
break
02:import random
while True:
try:
min = 1
max = int[input["Enter the highest value of dice to be rolled or 0 to exit: "]]
except:
print["Your input was invalid, program rolled a d20 by default"]
min = 1
max = 20
if max == 0:
break
if max < 0:
continue
again = True
number_of_dice = int[input["Enter number of dice to roll: "]]
for i in range[number_of_dice - 1]:
print[random.randint[min, max]]
while again:
print[random.randint[min, max]]
reroll = input["Roll again? [y/n]: "]
if reroll.lower[] == "y" or reroll.lower[] == "yes":
for i in range[number_of_dice - 1]:
print[random.randint[min, max]]
else:
print["Thank you"]
break
4extract 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 import random
while True:
try:
min = 1
max = int[input["Enter the highest value of dice to be rolled or 0 to exit: "]]
except:
print["Your input was invalid, program rolled a d20 by default"]
min = 1
max = 20
if max == 0:
break
if max < 0:
continue
again = True
number_of_dice = int[input["Enter number of dice to roll: "]]
for i in range[number_of_dice - 1]:
print[random.randint[min, max]]
while again:
print[random.randint[min, max]]
reroll = input["Roll again? [y/n]: "]
if reroll.lower[] == "y" or reroll.lower[] == "yes":
for i in range[number_of_dice - 1]:
print[random.randint[min, max]]
else:
print["Thank you"]
break
01 and place it in a non-public helper function called import random
while True:
try:
min = 1
max = int[input["Enter the highest value of dice to be rolled or 0 to exit: "]]
except:
print["Your input was invalid, program rolled a d20 by default"]
min = 1
max = 20
if max == 0:
break
if max < 0:
continue
again = True
number_of_dice = int[input["Enter number of dice to roll: "]]
for i in range[number_of_dice - 1]:
print[random.randint[min, max]]
while again:
print[random.randint[min, max]]
reroll = input["Roll again? [y/n]: "]
if reroll.lower[] == "y" or reroll.lower[] == "yes":
for i in range[number_of_dice - 1]:
print[random.randint[min, max]]
else:
print["Thank you"]
break
02:import random
while True:
try:
min = 1
max = int[input["Enter the highest value of dice to be rolled or 0 to exit: "]]
except:
print["Your input was invalid, program rolled a d20 by default"]
min = 1
max = 20
if max == 0:
break
if max < 0:
continue
again = True
number_of_dice = int[input["Enter number of dice to roll: "]]
for i in range[number_of_dice - 1]:
print[random.randint[min, max]]
while again:
print[random.randint[min, max]]
reroll = input["Roll again? [y/n]: "]
if reroll.lower[] == "y" or reroll.lower[] == "yes":
for i in range[number_of_dice - 1]:
print[random.randint[min, max]]
else:
print["Thank you"]
break
4Bạn có thể gọi
import random
while True:
try:
min = 1
max = int[input["Enter the highest value of dice to be rolled or 0 to exit: "]]
except:
print["Your input was invalid, program rolled a d20 by default"]
min = 1
max = 20
if max == 0:
break
if max < 0:
continue
again = True
number_of_dice = int[input["Enter number of dice to roll: "]]
for i in range[number_of_dice - 1]:
print[random.randint[min, max]]
while again:
print[random.randint[min, max]]
reroll = input["Roll again? [y/n]: "]
if reroll.lower[] == "y" or reroll.lower[] == "yes":
for i in range[number_of_dice - 1]:
print[random.randint[min, max]]
else:
print["Thank you"]
break
02 từ import random
while True:
try:
min = 1
max = int[input["Enter the highest value of dice to be rolled or 0 to exit: "]]
except:
print["Your input was invalid, program rolled a d20 by default"]
min = 1
max = 20
if max == 0:
break
if max < 0:
continue
again = True
number_of_dice = int[input["Enter number of dice to roll: "]]
for i in range[number_of_dice - 1]:
print[random.randint[min, max]]
while again:
print[random.randint[min, max]]
reroll = input["Roll again? [y/n]: "]
if reroll.lower[] == "y" or reroll.lower[] == "yes":
for i in range[number_of_dice - 1]:
print[random.randint[min, max]]
else:
print["Thank you"]
break
01 để 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 import random
while True:
try:
min = 1
max = int[input["Enter the highest value of dice to be rolled or 0 to exit: "]]
except:
print["Your input was invalid, program rolled a d20 by default"]
min = 1
max = 20
if max == 0:
break
if max < 0:
continue
again = True
number_of_dice = int[input["Enter number of dice to roll: "]]
for i in range[number_of_dice - 1]:
print[random.randint[min, max]]
while again:
print[random.randint[min, max]]
reroll = input["Roll again? [y/n]: "]
if reroll.lower[] == "y" or reroll.lower[] == "yes":
for i in range[number_of_dice - 1]:
print[random.randint[min, max]]
else:
print["Thank you"]
break
01 để đá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
import random
while True:
try:
min = 1
max = int[input["Enter the highest value of dice to be rolled or 0 to exit: "]]
except:
print["Your input was invalid, program rolled a d20 by default"]
min = 1
max = 20
if max == 0:
break
if max < 0:
continue
again = True
number_of_dice = int[input["Enter number of dice to roll: "]]
for i in range[number_of_dice - 1]:
print[random.randint[min, max]]
while again:
print[random.randint[min, max]]
reroll = input["Roll again? [y/n]: "]
if reroll.lower[] == "y" or reroll.lower[] == "yes":
for i in range[number_of_dice - 1]:
print[random.randint[min, max]]
else:
print["Thank you"]
break
01 tận dụng import random
while True:
try:
min = 1
max = int[input["Enter the highest value of dice to be rolled or 0 to exit: "]]
except:
print["Your input was invalid, program rolled a d20 by default"]
min = 1
max = 20
if max == 0:
break
if max < 0:
continue
again = True
number_of_dice = int[input["Enter number of dice to roll: "]]
for i in range[number_of_dice - 1]:
print[random.randint[min, max]]
while again:
print[random.randint[min, max]]
reroll = input["Roll again? [y/n]: "]
if reroll.lower[] == "y" or reroll.lower[] == "yes":
for i in range[number_of_dice - 1]:
print[random.randint[min, max]]
else:
print["Thank you"]
break
02 và thực hiện một chức năng trợ giúp khác gọi là import random
while True:
try:
min = 1
max = int[input["Enter the highest value of dice to be rolled or 0 to exit: "]]
except:
print["Your input was invalid, program rolled a d20 by default"]
min = 1
max = 20
if max == 0:
break
if max < 0:
continue
again = True
number_of_dice = int[input["Enter number of dice to roll: "]]
for i in range[number_of_dice - 1]:
print[random.randint[min, max]]
while again:
print[random.randint[min, max]]
reroll = input["Roll again? [y/n]: "]
if reroll.lower[] == "y" or reroll.lower[] == "yes":
for i in range[number_of_dice - 1]:
print[random.randint[min, max]]
else:
print["Thank you"]
break
08 để trích xuất chức năng từ dòng 25 đến 31:import random
while True:
try:
min = 1
max = int[input["Enter the highest value of dice to be rolled or 0 to exit: "]]
except:
print["Your input was invalid, program rolled a d20 by default"]
min = 1
max = 20
if max == 0:
break
if max < 0:
continue
again = True
number_of_dice = int[input["Enter number of dice to roll: "]]
for i in range[number_of_dice - 1]:
print[random.randint[min, max]]
while again:
print[random.randint[min, max]]
reroll = input["Roll again? [y/n]: "]
if reroll.lower[] == "y" or reroll.lower[] == "yes":
for i in range[number_of_dice - 1]:
print[random.randint[min, max]]
else:
print["Thank you"]
break
5Cá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
9 để mô phỏng việc lăn xúc xắcimport random while True: try: min = 1 max = int[input["Enter the highest value of dice to be rolled or 0 to exit: "]] except: print["Your input was invalid, program rolled a d20 by default"] min = 1 max = 20 if max == 0: break if max < 0: continue again = True number_of_dice = int[input["Enter number of dice to roll: "]] for i in range[number_of_dice - 1]: print[random.randint[min, max]] while again: print[random.randint[min, max]] reroll = input["Roll again? [y/n]: "] if reroll.lower[] == "y" or reroll.lower[] == "yes": for i in range[number_of_dice - 1]: print[random.randint[min, max]] else: print["Thank you"] break
9 to simulate the rolling of diceimport random while True: try: min = 1 max = int[input["Enter the highest value of dice to be rolled or 0 to exit: "]] except: print["Your input was invalid, program rolled a d20 by default"] min = 1 max = 20 if max == 0: break if max < 0: continue again = True number_of_dice = int[input["Enter number of dice to roll: "]] for i in range[number_of_dice - 1]: print[random.randint[min, max]] while again: print[random.randint[min, max]] reroll = input["Roll again? [y/n]: "] if reroll.lower[] == "y" or reroll.lower[] == "yes": for i in range[number_of_dice - 1]: print[random.randint[min, max]] else: print["Thank you"] break
9 to simulate the rolling of diceimport random while True: try: min = 1 max = int[input["Enter the highest value of dice to be rolled or 0 to exit: "]] except: print["Your input was invalid, program rolled a d20 by default"] min = 1 max = 20 if max == 0: break if max < 0: continue again = True number_of_dice = int[input["Enter number of dice to roll: "]] for i in range[number_of_dice - 1]: print[random.randint[min, max]] while again: print[random.randint[min, max]] reroll = input["Roll again? [y/n]: "] if reroll.lower[] == "y" or reroll.lower[] == "yes": for i in range[number_of_dice - 1]: print[random.randint[min, max]] else: print["Thank you"] break
- Lấy đầu vào của người dùng tại dòng lệnh bằng cách sử dụng chức năng
0 tích hợpuser’s input at the command line using the built-in1# dice.py 2 3# ~~~ App's main code block ~~~ 4# 1. Get and validate user's input 5num_dice_input = input["How many dice do you want to roll? [1-6] "] 6num_dice = parse_input[num_dice_input]
0 functionuser’s input at the command line using the built-in1# dice.py 2 3# ~~~ App's main code block ~~~ 4# 1. Get and validate user's input 5num_dice_input = input["How many dice do you want to roll? [1-6] "] 6num_dice = parse_input[num_dice_input]
0 function1# dice.py 2 3# ~~~ App's main code block ~~~ 4# 1. Get and validate user's input 5num_dice_input = input["How many dice do you want to roll? [1-6] "] 6num_dice = parse_input[num_dice_input]
- 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 và1# dice.py 2 3# ~~~ App's main code block ~~~ 4# 1. Get and validate user's input 5num_dice_input = input["How many dice do you want to roll? [1-6] "] 6num_dice = parse_input[num_dice_input]
2strings using methods, such as1# dice.py 2 3# ~~~ App's main code block ~~~ 4# 1. Get and validate user's input 5num_dice_input = input["How many dice do you want to roll? [1-6] "] 6num_dice = parse_input[num_dice_input]
1 and1# dice.py 2 3# ~~~ App's main code block ~~~ 4# 1. Get and validate user's input 5num_dice_input = input["How many dice do you want to roll? [1-6] "] 6num_dice = parse_input[num_dice_input]
2strings using methods, such as1# dice.py 2 3# ~~~ App's main code block ~~~ 4# 1. Get and validate user's input 5num_dice_input = input["How many dice do you want to roll? [1-6] "] 6num_dice = parse_input[num_dice_input]
1 and1# dice.py 2 3# ~~~ App's main code block ~~~ 4# 1. Get and validate user's input 5num_dice_input = input["How many dice do you want to roll? [1-6] "] 6num_dice = parse_input[num_dice_input]
21# dice.py 2 3# ~~~ App's main code block ~~~ 4# 1. Get and validate user's input 5num_dice_input = input["How many dice do you want to roll? [1-6] "] 6num_dice = parse_input[num_dice_input]
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
13.import random while True: try: min = 1 max = int[input["Enter the highest value of dice to be rolled or 0 to exit: "]] except: print["Your input was invalid, program rolled a d20 by default"] min = 1 max = 20 if max == 0: break if max < 0: continue again = True number_of_dice = int[input["Enter number of dice to roll: "]] for i in range[number_of_dice - 1]: print[random.randint[min, max]] while again: print[random.randint[min, max]] reroll = input["Roll again? [y/n]: "] if reroll.lower[] == "y" or reroll.lower[] == "yes": for i in range[number_of_dice - 1]: print[random.randint[min, max]] else: print["Thank you"] break
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
13 và đi qua hệ thống tệp bằngimport random while True: try: min = 1 max = int[input["Enter the highest value of dice to be rolled or 0 to exit: "]] except: print["Your input was invalid, program rolled a d20 by default"] min = 1 max = 20 if max == 0: break if max < 0: continue again = True number_of_dice = int[input["Enter number of dice to roll: "]] for i in range[number_of_dice - 1]: print[random.randint[min, max]] while again: print[random.randint[min, max]] reroll = input["Roll again? [y/n]: "] if reroll.lower[] == "y" or reroll.lower[] == "yes": for i in range[number_of_dice - 1]: print[random.randint[min, max]] else: print["Thank you"] break
15.import random while True: try: min = 1 max = int[input["Enter the highest value of dice to be rolled or 0 to exit: "]] except: print["Your input was invalid, program rolled a d20 by default"] min = 1 max = 20 if max == 0: break if max < 0: continue again = True number_of_dice = int[input["Enter number of dice to roll: "]] for i in range[number_of_dice - 1]: print[random.randint[min, max]] while again: print[random.randint[min, max]] reroll = input["Roll again? [y/n]: "] if reroll.lower[] == "y" or reroll.lower[] == "yes": for i in range[number_of_dice - 1]: print[random.randint[min, max]] else: print["Thank you"] break
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.