Một số PEP8 (là hướng dẫn kiểu cho mã Python) Nhận xét về phong cách và định dạng mã hóa của bạn:
1. Khoảng cách
Giữa các phương thức, bạn nên có hai dòng trống.
def questionOne(): .... def questionTwo(): ....2. Đặt tên quy ước
Trong PEP8, bạn nên đề nghị:
Sử dụng các quy tắc đặt tên chức năng: chữ thường với các từ được phân tách bằng dấu gạch dưới khi cần thiết để cải thiện khả năng đọc.
Điều đó nói rằng, questionNumber sẽ trở thành question_number
Tôi đã thấy nhiều người sử dụng camelCase cho các biến, vì vậy đó là vấn đề ưu tiên.
Quy tắc tương tự áp dụng cho các phương pháp của bạn:
print("\nSo far ",name,"You have got",right,"Answers right,",wrong,"Answers wrong and you have completed",questionNumber,"Questions\n") 0 -> print("\nSo far ",name,"You have got",right,"Answers right,",wrong,"Answers wrong and you have completed",questionNumber,"Questions\n") 1
Đặt một khoảng trống sau mỗi dấu phẩy (print("\nSo far ",name,"You have got",right,"Answers right,",wrong,"Answers wrong and you have completed",questionNumber,"Questions\n") 2). Nó thực sự có ích khi đi qua mã của bạn.
Vì vậy, điều này:
print("\nSo far ",name,"You have got",right,"Answers right,",wrong,"Answers wrong and you have completed",questionNumber,"Questions\n")Có thể được viết lại như:
print("\nSo far ", name, "You have got", right, "Answers right,", wrong, "Answers wrong and you have completed", questionNumber, "Questions\n")3. Nhận xét khác
Thay vì print("\nSo far ",name,"You have got",right,"Answers right,",wrong,"Answers wrong and you have completed",questionNumber,"Questions\n") 3, bạn có thể sử dụng một nhiệm vụ tăng cường: print("\nSo far ",name,"You have got",right,"Answers right,",wrong,"Answers wrong and you have completed",questionNumber,"Questions\n") 4.
Nhiều hơn, thay vì làm điều này:
print() print("So far",name,"You have got",right,"Answers right,",wrong,"Answers wrong and you have completed",questionNumber,"Questions") print()Bạn có thể làm:
print("\nSo far",name,"You have got",right,"Answers right,",wrong,"Answers wrong and you have completed",questionNumber,"Questions\n")Tôi không thích cách bạn định dạng print("\nSo far ",name,"You have got",right,"Answers right,",wrong,"Answers wrong and you have completed",questionNumber,"Questions\n") 5 ở trên nhưng tôi sẽ quay lại với nó sau.
Hãy xem những gì chúng ta đã có cho đến nay bằng cách sử dụng các sửa đổi mà tôi đã đề cập cho đến nay:
import time wrong = 0 right = 0 question_number = 0 name = str(input("Please Enter your name: ")) def questions(): def question_one(): global right, wrong, question_number print("What is the population of New Zealand?") print("Is it A:6.7m B:3.2m C:5.1m or D:4.5m") ans = str(input()) if ans == "D" or ans == "d" or ans == '4.5' or ans == '4': print("You got it right!") right += 1 else: print("You got it wrong!") wrong += 1 question_number += 1 question_one() time.sleep(2) print("\nSo far", name, "You have got", right, "Answers right,", wrong, "Answers wrong and you have completed", question_number, "Questions\n") time.sleep(4) def question_two(): global right, wrong, question_number print("What year did the first european set foot on New Zealand (Abel Tasman)") print("Is it A:1830 B:1543 C:1642 or D:1765") ans = str(input()) if ans == "C" or ans == "c" or ans == '1642' or ans == '3': print("You got it right!") right += 1 else: print("You got it wrong!") wrong += 1 question_number += 1 question_two() time.sleep(2) print("\nSo far ", name, "You have got", right, "Answers right,", wrong, "Answers wrong and you have completed", question_number, "Questions\n") time.sleep(4) def question_three(): global right, wrong, question_number print("How many Kiwi are there left in New Zealand (Approx)") print("Is it A:2000 B:600 C:70,000 or D:100000") ans = str(input()) if ans == "D" or ans == "d" or ans == '100000' or ans == '4': print("You got it right!") right += 1 else: print("You got it wrong!") wrong += 1 question_number += 1 question_three() time.sleep(2) print("\nSo far ", name, "You have got", right, "Answers right,", wrong, "Answers wrong and you have completed", question_number, "Questions\n") time.sleep(4) def question_four(): global right, wrong, question_number print("How many new babys where born in New Zealand in 2015") print("Is it A:61,000 B:208,000 C:98,000 or D:18,000") ans = str(input()) if ans == "D" or ans == "d" or ans == '100000' or ans == '4': print("You got it right!") right += 1 else: print("You got it wrong!") wrong += 1 question_number += 1 question_four() questions()Đào sâu hơn:
Quả cầu
Cố gắng tránh sử dụng Globals càng nhiều càng tốt. Các biến toàn cầu nên tránh vì chúng ức chế tái sử dụng mã. Lý do chúng xấu là vì chúng cho phép các chức năng ẩn (như trong "không rõ ràng" và "không được khai báo") và do đó khó hiểu các tác dụng phụ. Ngoài ra, điều này có thể dẫn đến mã spaghetti.
Mã trùng lặp
Như bạn có thể thấy, các chức năng của bạn khá giống nhau, vì vậy bạn có thể sử dụng điều này và thử tạo một phương thức duy nhất. Hơn nữa, chúng ta có thể thoát khỏi việc sử dụng các phương pháp bên trong xấu xí đó. Nó chỉ là không cần thiết trong trường hợp này.
Những gì tôi sẽ làm là:
- Tạo ba danh sách: một câu hỏi chứa câu hỏi, một câu trả lời và một câu trả lời khác chứa các câu trả lời chính xác.
- Sau đó, tạo một phương pháp đơn giản và về cơ bản làm những gì bạn đã làm, nhưng theo cách ngắn hơn / đẹp hơn.
Hãy xem những gì chúng ta có:
list_of_questions = ['What is the population of New Zealand ?', 'What year did the first european set foot on New Zealand (Abel Tasman) ?', 'How many Kiwi are there left in New Zealand (Approx) ?', 'How many new babys where born in New Zealand in 2015 ?'] lists_of_answers = [['A:1830', 'B:1543', 'C:1642', 'D:1765'], ['A:1830', 'B:1543', 'C:1642', 'D:1765'], ['A:2000', 'B:600', 'C:70,000', 'D:100000'], ['A:61,000', 'B:208,000', 'C:98,000', 'D:18,000']] list_of_correct_answers = [['D', 'd', '4.5', '4'], ['C', 'c', '1642', '3'], ['D', 'd', '100000', '4'], ['D', 'd', '100000', '4']] def questions(): wrong = 0 right = 0 for each_question, each_answer, each_correct_answer in zip(list_of_questions, lists_of_answers, list_of_correct_answers): print(each_question + '\n' + ' '.join(each_answer)) get_answer = raw_input() if get_answer in each_correct_answer: print('Your answer is correct!\n') right += 1 else: print('That is not the answer I had in mind!\n') wrong += 1 print('So far, you answered correctly to {0} questions and incorrectly to {1}. Good luck!'.format(right, wrong)) if __name__ == '__main__': questions()Bạn có thể quan sát rằng tôi cũng đã thêm print("\nSo far ",name,"You have got",right,"Answers right,",wrong,"Answers wrong and you have completed",questionNumber,"Questions\n") 6. Lý do tôi đã làm điều này là vì bạn cũng có thể nhập mã của mình dưới dạng mô -đun trong một tập lệnh khác và sau đó chạy chức năng chính nếu và khi chương trình của bạn quyết định.
Bước tiếp theo:
- Hãy cố gắng nhất quán khi viết mã.
- Tránh in vào nhiều thứ. Và nếu bạn làm như vậy, hãy thử xem lại ngữ pháp của bạn.