简体   繁体   English

请向我解释我怎样才能更好地把它编码出来

[英]please explain to me how i could better code this out

You have been asked to make a special book categorization program, which assigns each book a special code based on its title.你被要求制作一个特殊的书籍分类程序,它根据每本书的标题为每本书分配一个特殊的代码。
The code is equal to the first letter of the book, followed by the number of characters in the title.该代码等于书的第一个字母,后面是标题中的字符数。
For example, for the book "Harry Potter", the code would be: H12, as it contains 12 characters (including the space).例如,对于“哈利波特”一书,代码为:H12,因为它包含 12 个字符(包括空格)。

You are provided a books.txt file, which includes the book titles, each one written on a separate line.您将获得一个 books.txt 文件,其中包含书名,每一个都写在单独的一行上。
Read the title one by one and output the code for each book on a separate line.逐一阅读书名,output 每本书的代码各占一行。

For example, if the books.txt file contains:例如,如果 books.txt 文件包含:

 Some book Another book

Your program should output:你的程序应该是 output:

 S9 A12

Recall the readlines() method, which returns a list containing the lines of the file.回想一下 readlines() 方法,它返回一个包含文件行的列表。
Also, remember that all lines, except the last one, contain a \n at the end, which should not be included in the character count.另外,请记住,除了最后一行之外,所有行的末尾都包含一个 \n,这不应包含在字符数中。


This is a website I'm using to learn python and I am stuck because i don't know where to use readlines() and I don't really know why I need to use it.这是我用来学习 python 的网站,我被卡住了,因为我不知道在哪里使用readlines()并且我真的不知道为什么我需要使用它。 (It has to be used.) My code below shows me printing every name in the file and only getting its first letter. (它必须被使用。)我下面的代码显示我打印文件中的每个名字并且只得到它的第一个字母。 My mistake is when I run this code I get the same exact thing I wanted except my last digit is off because of spaces.我的错误是当我运行这段代码时,我得到了我想要的完全相同的东西,只是我的最后一位数字因为空格而关闭了。

#given code
file = open("/usercode/files/books.txt", "r")

#my code

for name in file:
    
    print(name[0] + str(len(name)-1))

file.close()

OUTPUT: OUTPUT:

Your Output
H12
T16
P19
G17
Expected Output
H12
T16
P19
G18

The instructions say:说明说:

Also, remember that all lines, except the last one , contain a \n at the end另外,请记住,除最后一行外,所有行的末尾都包含一个 \n

You're subtracting 1 from the length of all lines, including the last one.您要从所有行的长度中减去 1,包括最后一行。

Instead of subtracting 1 from the length and special-casing the last line, you can use the rstrip() method to remove a newline at the end of the string.您可以使用rstrip()方法删除字符串末尾的换行符,而不是从长度中减去 1 和特殊大小写的最后一行。

for name in file.readlines():
    print(name[0] + str(len(name.rstrip('\n'))))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 有人可以向我解释此Python代码中我在做什么错,并帮我弄清楚吗? - Could someone explain to me what I'm doing wrong in this Python Code and help me figure it out? 有人可以解释一下此代码中如何使用移位吗? - Could someone please explain how shifting is used in this code? 有人可以为我解释此代码吗? - Could someone explain this code for me? 函数在通过字典调用其他函数时遇到麻烦:有人可以向我解释此代码吗? - Having trouble with functions calling other functions through dictionaries: could someone please explain this code to me? 有人可以向我解释这些代码行吗? - Can someone please explain me these lines of code? 请解释一下这个python代码的含义是什么? - Please explain to me what this python code means? 有人可以向我解释这段代码 - python 3 - can someone please explain to me this code - python 3 有人可以解释一下这个简单的python代码吗? - could someone please explain this simple python code? 您能否在python中解释此代码片段 - Could you please explain this code fragment in python 我正在观看 Reducible 关于递归的视频并看到了这段代码,但我无法理解它是如何工作的,有人可以解释一下吗? - i was watching Reducible's video about recursion and saw this code and i cant understand how it works could someone please explain?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM