HW3 <<
Previous Next >> MAX OF THREE 最多三人
Concepts 概念
One review concept that is definitely needed (in addition to the user input that is the core of the exercise) is the need to “split” strings.
(除了作為練習核心的用戶輸入之外)肯定需要一個審核概念是“拆分”字符串。
The user will input coordinates in the form “row,col”, which input()
will then read in as a string. But we really want the numbers that come out of that string, to know which row and column to place the piece at.
用戶將以“ row,col”的形式輸入坐標,然後input()將以字符串形式讀取。但是我們確實希望從該字符
串中得出的數字知道將片段放置在哪一行和哪一列。
One approach is to use the idea of strings as lists to extract the row and column numbers. This works great if your row and column numbers are always single digits - the row will always be at index 0 and the column will always be at index 2. But this breaks when the numbers are larger than one digit (I know, not going to happen in tic tac toe, but it’s easy to image extending this to other games).
一種方法是使用字符串作為列表的概念來提取行號和列號。如果行號和列號始終是一位數字,則該方法非
常有用-行將始終在索引0處,列始終在索引2。但是,當數字大於一位時,這種方法會中斷(我知道,會
在井字遊戲中發生,但很容易將其擴展到其他遊戲。
Instead, there are two string manipulation functions that will help you:
相反,有兩個字符串操作函數可以為您提供幫助:
1.split()
- Takes a string and returns a list, using the separator as the split criteria. So if you have a string name = "John Doe"
and do name_list = name.split(" ")
, name_list
will be ["John", "Doe"]
. You can use any separator / split character you want. Just remember, that each of the elements returned back will be a string as well.
1.split()-使用分隔符作為分割條件,獲取字符串並返回列表。因此,如果您有一個字符串名稱=“ John Doe”,並且執行name_list = name.split(“”),則name_list將為[“ John”,“ Doe”]。您可以使用所需的任何分隔符/拆分字符。請記住,返回的每個元素也將是一個字符串。
2..strip()
- Takes a string and removes the whitespace on the left and right sides of it. So you have a string name = " Michele "
, and you do name = name.strip()
, and now name
will just be "Michele"
- nice and clean.
2..strip()-接收字符串並刪除其左側和右側的空格。因此,您有一個字符串名稱=“ Michele”,並且您
執行了name = name.strip(),現在名稱將只是“ Michele”-乾淨整潔。我挑戰您找出如何在練習中使
用它們!