40923228 cp2020

  • Home
    • Site Map
    • reveal
    • blog
  • HomePage
  • Webside
  • HW1
    • PCH6-RAM Basics
      • Memory Modules
      • Classifying Memory
      • Determining Type of Memory Based on Motherboard
      • History of RAM
      • Parity
      • Installing Memory Modules
      • Introduction to BIOS, CMOS, and Firmware
      • Configuring the System BIOS
    • PCH 7-Expansion Cards, Audio, Video, and Storage
      • AGP Slots
      • Processors
      • Video Displays and Video Cards
      • Install and Configure Expansion Cards
      • Storage Devices-Overview
      • Storage Devices-Hard Disk Drives
      • Storage Devices-Drive Interfaces
      • Storage Devices-Hot Swapping
  • HW2
    • 2-1(分組數列)
    • 2-2
    • 2-3
  • HW3
    • EX.10
    • EX.20
    • EX.32
  • 心得
EX.20 << Previous Next >> 心得

EX.32

Hangman&猜單詞遊戲

Exercise 32 (and Solution)/練習32(和解決方案)

This exercise is Part 3 of 3 of the Hangman exercise series. The other exercises are: Part 1 and Part 2.

本練習是《 Hangman》練習系列3的第3部分。其他練習是:第1部分和第2部分。

You can start your Python journey anywhere, but to finish this exercise you will have to have finished Parts 1 and 2 or use the solutions (Part 1 and Part 2).

您可以在任何地方開始Python之旅,但是要完成本練習,您將必須完成第1部分和第2部分或使用解決方案(第1部分和第2部分)。

In this exercise, we will finish building Hangman. In the game of Hangman, the player only has 6 incorrect guesses (head, body, 2 legs, and 2 arms) before they lose the game.

在本練習中,我們將完成“ Hangman”的構建。在《 Hangman》遊戲中,玩家在輸掉遊戲前只有6個錯誤的猜測(頭部,身體,2條腿和2條手臂)。

In Part 1, we loaded a random word list and picked a word from it. In Part 2, we wrote the logic for guessing the letter and displaying that information to the user. In this exercise, we have to put it all together and add logic for handling guesses.

在第1部分中,我們加載了一個隨機單詞列表並從中選擇一個單詞。在第2部分中,我們編寫了猜測字母並將該信息顯示給用戶的邏輯。在本練習中,我們必須將所有內容放在一起並添加邏輯以處理猜測。

Copy your code from Parts 1 and 2 into a new file as a starting point. Now add the following features:

將您的代碼從第1部分和第2部分複製到一個新文件中作為起點。現在添加以下功能:

  • Only let the user guess 6 times, and tell the user how many guesses they have left.
  • Keep track of the letters the user guessed. If the user guesses a letter they already guessed, don’t penalize them - let them guess again.
  • 僅讓用戶猜測6次,然後告訴用戶他們還剩下多少猜測。
  • 跟踪用戶猜測的字母。如果用戶猜出了他們已經猜出的字母,請不要對它們進行懲罰-讓他們再猜一次。

Optional additions:

可選的補充:

  • When the player wins or loses, let them start a new game.
  • Rather than telling the user "You have 4 incorrect guesses left", display some picture art for the Hangman. This is challenging - do the other parts of the exercise first!
  • 當玩家贏或輸時,讓他們開始新的遊戲。
  • 不要告訴用戶"You have 4 incorrect guesses left",而是為the子手展示一些圖畫。這具有挑戰性-首先進行練習的其他部分!

Your solution will be a lot cleaner if you make use of functions to help you!

如果您使用功能來幫助您,您的解決方案將變得更加干淨!

Concepts

概念

  • Sets
  • Code organization
  • 套裝
  • 代碼組織

Sets

套裝

We already discussed sets in Exercise 14, but here is a brief summary:

我們已經在練習14中討論了集合,但這是一個簡短的摘要:

  • Sets, like lists, store a collection of items
  • The collection of items in a set are unique. There cannot be any repeated elements.
  • 集合(如列表)存儲項目的集合
  • 集合中的項目集合是唯一的。不能有任何重複的元素。

The only “gotcha” is that you cannot have a set of lists - each element in the set must be hashable. Basically this means you can’t have elements that can change in a set, so the objects in your set should be integers or strings. The reason and distinction are not super important; the most important thing to know is that sets are most useful when you want a set of integers or strings, rather than a set of lists.

唯一的“陷阱”是您不能擁有一組列表-列表中的每個元素都必須是可散列的。基本上,這意味著您不能在集合中更改任何元素,因此集合中的對象應為整數或字符串。原因和區別並不重要。要知道的最重要的一點是,當您需要一組整數或字符串而不是一組列表時,集合最有用。

In Python:

在Python中:

>>> my_list = [1, 1, 2, 2, 3, 3, 5, 5, 8]
>>> print(my_list)
[1, 1, 2, 2, 3, 3, 5, 5, 8]
>>> my_set = {1, 1, 2, 2, 3, 3, 5, 5, 8}
>>> print(my_set)
{1, 2, 3, 5, 8}

To add elements to a set, use the .add() method:

要將元素添加到集合中,請使用.add()方法:

>>> my_set = set()  # makes an empty set
>>> my_set.add(5)  # adds the number 5
>>> print(my_set)
{5}
>>> my_set.add("Michele")
>>> print(my_set)
{5, "Michele"}
>>> my_set.add(5)
>>> print(my_set)
{5, "Michele"}

The advantage of sets is that all the elements in it are unique. This makes it very easy to check whether an element is already in the set. All you need to do is ask elem in my_set:

集的優點是其中的所有元素都是唯一的。這使得檢查元素是否已經在集合中變得非常容易。您需要做的只是問elem in my_set:

>>> my_set = {1, 2, 3, 5}
>>> 1 in my_set
True
>>> 4 in my_set
False

And this check is very fast - much faster than doing the same in check with a list. (The reason it is fast is a concept called hashing, which you can read about in detail in this StackOverflow post.)

而且此檢查非常快-比in使用列表進行相同檢查要快得多。(之所以如此之快,是因為有一個稱為哈希的概念,您可以在此StackOverflow帖子中詳細了解它。)

Code organization

代碼組織

We briefly discussed some tips on working with lots of code in Exercise 29, and I’ll add a few more here:

我們在練習29中簡要討論了使用大量代碼的一些技巧,我將在此處添加更多信息:

  • If you don’t like your code from Parts 1 and 2, just take any of the code from the solution to Part 1 or the solution to Part 2 to start. Just because you wrote some code before, doesn’t mean you are locked in forever!
  • Any piece of your code that is used more than once, put it into a function. We did a long discussion of functions in Exercise 11, if you want a review.
  • Your functions should be small and easy to understand - think of it as a 3-year-old child; if you ask a 3-year-old to do too many things, they will get confused and not do exactly what you asked.
  • 如果您不喜歡第1部分和第2部分中的代碼,只需從解決方案第1部分或第2部分的解決方案中選取任何代碼即可。僅僅因為您之前編寫了一些代碼,並不意味著您將永遠被鎖定!
  • 您的代碼中多次使用的任何部分都將其放入函數中。如果您想複習一下,我們在練習11中對功能進行了長時間的討論。
  • 您的功能應該小巧且易於理解-將其視為3歲的孩子;如果您要求3歲的孩子做太多的事情,他們會感到困惑,而不能完全按照您的要求做。

If you have any other code organization tips, leave them in the comments, would love to get a good discussion going.

如果您有任何其他代碼組織提示,請將其保留在註釋中,希望能進行很好的討論。


EX.20 << Previous Next >> 心得

Copyright © All rights reserved | This template is made with by Colorlib