40923248 cp2020

  • Home
    • Site Map
    • reveal
    • blog
  • 首YA
  • HW1
    • PCH 15 Introduction to Wireless Networking 無線網絡簡介
      • Wireless LANs 無線局域網
      • Wireless Devices 無線設備
      • Wireless Standards 無線標準
      • Privacy and Security 隱私和安全
      • Wireless Networking Types 無線網絡類型
    • PCH 16  Internet Connectivity 互聯網連接
      • Internet Connectivity 互聯網連接
      • Networking  Internet Connectivity  聯網 Internet連接
      • Setting up a Network 建立網絡
  • HW2
  • HW3
    • Birthday Dictionaries 生日字典
      • Exercise 33  and Solution   練習33 和解決方案
      • Discussion topics 討論主題
      • Dictionaries 辭典
      • QUICK REVIEW 快速復審
      • MORE ON DICTIONARY KEYS 有關字典鍵的更多信息
      • String formatting 字符串格式
      • Solutions 解決方案
    • Tic Tac Toe Draw 井字遊戲抽獎
      • Exercise 27 and Solution 練習27 和解決方案
      • Concepts 概念
      • Solutions解決方案
    • Birthday Plots 生日情節
      • Exercise 36 and Solution 練習36和解決方案
      • Discussion 討論區
      • When to make plots 什麼時候作圖
      • Plotting libraries in Python 用Python繪製庫
      • Installing bokeh 安裝背景虛化
      • Using bokeh 使用散景
  • 心得
  • 自評 65 分
Exercise 27 and Solution 練習27 和解決方案 << Previous Next >> Solutions解決方案

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.

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).

Instead, there are two string manipulation functions that will help you:

.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.
.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.
I challenge you to figure out how to use them in the exercise!

(除了作為練習核心的用戶輸入之外)肯定需要的一種審核概念是需要“拆分”字符串。

用戶將以“ row,col”形式輸入坐標,然後input()將其作為字符串讀取。但是我們確實希望從該字符串中得出的數字知道將片段放置在哪一行和哪一列。

一種方法是使用字符串作為列表的概念來提取行號和列號。如果行號和列號始終是一位數字,則該方法非常有用-行將始終在索引0處,列始終在索引2。但是,當數字大於一位時,這種方法會中斷(我知道,會在井字遊戲中發生,但很容易將其擴展到其他遊戲。

相反,有兩個字符串操作函數可以為您提供幫助:

.split()-使用分隔符作為分割條件,獲取一個字符串並返回一個列表。因此,如果您有琴弦name = "John Doe"做name_list = name.split(" "),name_list將是["John", "Doe"]。您可以使用所需的任何分隔符/拆分字符。請記住,返回的每個元素也將是一個字符串。
.strip()-取一個字符串並刪除其左側和右側的空格。這樣,您就有了一個字符串name = " Michele ",並且執行了name = name.strip(),現在name就變得"Michele"很乾淨了。
我挑戰您找出如何在練習中使用它們!


Exercise 27 and Solution 練習27 和解決方案 << Previous Next >> Solutions解決方案

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