首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

ICS Project 小贴士 Week 3

排版:乔然

学术部

王同杰

ICS 31

2.16 和 3.11

要完成这个lab我们需要知道如何在Python中进行幂运算。常见的Python运算符归纳如下:

3.12

这个lab是让我们将一个代表电话号码的int,提取出area code,prefix和line number,用“-”连接起来,输出到屏幕上。

如何提取数字在Lab中有详细说明,这里就不赘述了。要注意的就是因为最终结果要用“-”连接起来,所以area code,prefix和line number需要转成str才能用加号跟“-”连接。

3.13 和 3.14

这两个lab主要是练习String Formatting。这里举几个例子说明什么是String Formatting,怎么用String Formatting。

上面的例子中,告诉Python“这里我要放一些东西”的placeholder叫Conversion Specifier。这些specifier以“%”符号开头,后面的数字和字母表示“放在这里的东西”的种类。下表为一些常见的specifier和它们对应的type。

PyFormat(https://pyformat.info/)有非常详尽的String Formatting指南,如果在这方面还有疑问,可以参考这个网站的内容。

English Version:

2.16 & 3.11

To complete this lab, we have to know basic operators in Python.

3.12

In this lab we are asked to extract area code, prefix and line number from a given U.S. phone number and print the each part connected with a short dash "-".

There are hints in the lab about how to extract. Notice that if you want to concatenate a part with a dash, you have to convert that part from int into str first, then you can use + sign to concatenate them.

3.13 & 3.14

In these two labs we are practing String Formatting.

In the example above, the notation starting with "%" is called Conversion Specifier who tells Python "I'm going to put some stuff here". The letter and number in the notation specify the type of the conversion.

PyFormat(https://pyformat.info/) is a great resource for reference in string formatting.

学术部

郑天呈

ICS 32

1. 本周Lecture note总结

可执行模块(executable modules) if __name__ == ‘__main__’

在if __name__ == "__main__":之下写的代码只会在直接运行这个module的时候运行,在被import到别的module的时候这些代码不会被引用也不会被执行。midterm时应该也会有考这个点的题。

Public vs. private definitions

Public函数和变量可以在其他module中使用,而private的则理论上不行(实际上可以,python比较愚蠢)。Private变量应在前面加“_”,不写会被TA扣分哦~

Protocol (I32CFPS)

Alex的notes里面讲的很详细了,可以直接用他的polling.py修改。Protocol是定义好的客户端与服务器交流的模式,就像对暗号一样。所以把polling的相应部分换成project 2网页规定的模式就可以。

注意!socket_out.write()完一定要flush()!

English version:

You sell your soul to the devil to make money.

If __name__ == ‘main’ means that this is an executable module, so it can only run when the module is executed directly by using F5. When its file is imported to other modules, it will not execute!

Public variables and functions can be used in other modules, but private ones, in theory, cannot be used. Therefore, as a python convention, we put an underscore in front of the private variables and functions.

Protocol is fully covered in Prof. Thornton’s notes, and you can modify his polling.py module to write your own protocol in project 2. Remember to flush() after socket_out.write()!

2. Project Tip

再次提醒!socket_out.write()完一定要flush()!

在GameState.board这个2D-list中,一列的数据是以一个list的形式存储的,所以在print的时候需要调换for loop执行对象的顺序。最上面的点是list的第一个值,最下面的点是list最后一个值。验证某一列是不是已经下满时需要注意到这一点。

List的index范围是0到6,而columns编号则给的1-7。所以,使用connectfour.drop和connectfour.pop这两个function的时候,要注意column-1!

此外,需要注意我们的码能不能在处理用户的input时对细节有正确的反应,如输入不是[1,7]范围内的列数及输入不符合形式规范的内容时提醒重新输入,兼容大小写输入指令,去掉多余的空格,以及当某一列已经下满时提醒用户重新输入。

最后说一下文件提交的方式。这个project需要提交5个文件(本地版本,网络版本,老师给的基础算法,连接服务器的protocol以及游戏的基本操作)。本地版本基本上就是游戏的基本操作了,所以我个人写的本地版本只有一行~嘿嘿嘿~文件一定要分好啊~

English Version:

In GameState.board, you store a column in a list. Therefore, when printing, you need to reverse the order in which you do your for loops. The highest spot in a column is the first value of the list, and the lowest spot in the last value. You need to consider this when checking whether a column is full.

The indexes of the list range from 0 to 6, but the column numbers are 1 to 7. Therefore, when using connectfour.drop and connectfour.pop, you need to use (column-1).

In addition, when the input is not between 1 to 7, we need to prompt the user for a legal input. We also need to take into account both uppercase and lowercase. Remember to remove the extra white spaces. When a column is full, prompt the user to enter another column number.

3.列出将要使用的functions:

连接服务器的protocol:

def connect

def close

def _write_line

def _read_line

这几个function在Alex的polling.py笔记里都提到了,不多解释了~

English version: For the protocol module, you need four functions: connect, close, _write_line, and _read_line. All of them are covered in polling.py, so just read your notes.

本地版本:

def print_game_board //打印棋盘的时候,注意for i in range(row) 后for j in range(columns) 然后要用board[j][i]

网络版本:

def hello // 参见polling_ui.py 初始化用户名

def initialization // 连接的host是'woodhouse.ics.uci.edu',port是4444

def player_move // 玩家回合,要把用户输入转换成对应的protocol指令传给服务器

def AI_move // 获取AI反应,然后相对应地更新本地棋盘

English Version:

Online version: the function hello initializes the username, which can be seen in polling_ui.py. Initialization initializes the host to ‘woodhouse.ics.uci.edu’ and the port to 4444. Player_move converts user input to protocol commands and pass it onto the server. AI_move retrieves the server responses and updates the GameBoard accordingly.

Console version: the main function is print_game_board. Remember to reverse the for loops, i.e. for i in range(row): for j in range(columns): print(board[j][i])

写代码

你快乐吗?

ICS 33

1. Project Tip

Program 2主要有两个部分:bag和dictlist. 相比于其他的program来说,要做的事情比较少。这里主要讲一下里面需要注意的小点。

Bag部分没有什么太大的难点。 要注意的是在__init__部分需要注意parameter的default value。另外就是type()和type_as_str()的用法。在作业中,type比type_as_str()更好用一些。type_as_str是Pattis在goody文件中建的一个function。这个function会截取一部分type输出的结果。type()的输出结果可以自己试验一下。如果有同学想多写1行的代码,可以试着使用join()来print文字类function或将if else、for loop变成一行来写。

DictList需要写的部分稍微多一些。大部分的样例都在pattis的笔记中有提到。可以直接在笔记中搜索。基本上不需要更改。

English version:

Just say yes!

Here are the tips for program 2:

For the first part, the first thing you need to notice is whether to set a default value for the parameters.

type_as_str is a function in the goody.py. You can need either the type_as_str or type(built-in function). The output of type_as_str is a string which is different from the type function.

If you want to write some of the functions in one line, you can use join function for the string outputs. Another strategy is to put if statement and for loop in one line.

Many samples and instructions are in the Pattis’ code. They are very useful.

  • 发表于:
  • 原文链接https://kuaibao.qq.com/s/20190126G064OA00?refer=cp_1026
  • 腾讯「腾讯云开发者社区」是腾讯内容开放平台帐号(企鹅号)传播渠道之一,根据《腾讯内容开放平台服务协议》转载发布内容。
  • 如有侵权,请联系 cloudcommunity@tencent.com 删除。

扫码

添加站长 进交流群

领取专属 10元无门槛券

私享最新 技术干货

扫码加入开发者社群
领券