michael4227 / OIM3640-2021fall

Python
0 stars 0 forks source link

Exercise in session 04 #3

Closed lzblack closed 2 years ago

lzblack commented 2 years ago

https://github.com/michael4227/OIM3640/blob/6f648e0205e762cb30df2a96242e3745dacc39ae/ex_03.py#L6 So now daynum is a string https://github.com/michael4227/OIM3640/blob/6f648e0205e762cb30df2a96242e3745dacc39ae/ex_03.py#L9 and ctime is a float, and they cannot be used this way (ctime - daynum).

My suggestion is don't convert daynum to a string. Only when you need to print the variable, you need to use f-string.

lzblack commented 2 years ago

Please move the file to session04 folder.

michael4227 commented 2 years ago

老师,您为什么在 "int(days):d" 用:d呀?为了让他变成十进制的整数嘛?有了int为什么还需要这个啊?

lzblack commented 2 years ago

为什么在 int(days):d:d呀?

不用也可以。我大概是忘了删了。。。

michael4227 commented 2 years ago

哈哈哈好嘞! 我当时试了一下他给我的错误说“Unknown format code 'd' for object of type 'float'” 那是不是说":d"唯一的用处就是改十进制,但不能把float改成整数啊?

lzblack commented 2 years ago

是不是说":d"唯一的用处就是改十进制,但不能把float改成整数啊?

也不是改十进制,主要是搭配着数字来定义输出整数的宽度,比如print(f'{int(hours):2d}')

michael4227 commented 2 years ago

那就是说无论是“:d”还是“:f”都是只能改输出数字的宽度,不能用来改数据type?

lzblack commented 2 years ago

当然不能改变 type,f-string只是用来format的东西。

http://cis.bentley.edu/sandbox/wp-content/uploads/Documentation-on-f-strings.pdf

michael4227 commented 2 years ago

好的 明白了!谢谢老师!