猿问

Python-以给定格式编写句子

我有一个包含一句话的文件。我让用户选择“行”和“列”的数量。我想检查一下我可以在这种表格中写多少次而不拆分单词。现在我希望文本的形式是这样的:输入:行= 3列= 10个文件来自文件:猫有狗。输出:猫有***狗。猫**有狗。**


该程序无法拆分单词,并且无法在无法放置星星的地方进行拆分。这是我所做的代码的一部分,但我觉得我没有朝着好的方向发展。


我的问题: 1. 如何改进我的代码?2. 如何让它既能算字又能算字?3. 此任务的一般提示。


我的代码:


import sys

columns, rows, path = sys.argv[1:]

columns=int(columns)

rows=int(rows)

file=open(path,"r")

text=file.read()

list=list(text.split())

length=len(list)

for i in range(length):

    k=len(lista[i])

    if k<=columns:

        print(list[i], end=" ")

    else:

        print("*") 


qq_花开花谢_0
浏览 199回答 1
1回答

慕工程0101907

这比我想象的要艰难。可能有一个更简单的解决方案,但你可以试试这个:list_of_words = text.split()current_character_count_for_row = 0current_string_for_row = ""current_row = 1how_many_times_has_sentence_been_written = 0is_space_available = True# Keep going until told to stop.while is_space_available:&nbsp; &nbsp; for word in list_of_words:&nbsp; &nbsp; &nbsp; &nbsp; # If a word is too long for a row, then return false.&nbsp; &nbsp; &nbsp; &nbsp; if len(word) > columns:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; is_space_available = False&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break&nbsp; &nbsp; &nbsp; &nbsp; # Check if we can add word to row.&nbsp; &nbsp; &nbsp; &nbsp; if len(word) + current_character_count_for_row < columns:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; # If at start of row, then just add word if short enough.&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if current_character_count_for_row == 0:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; current_string_for_row = current_string_for_row + word&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; current_character_count_for_row += len(word)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; # otherwise, add word with a space before it.&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; current_string_for_row = current_string_for_row +" " + word&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; current_character_count_for_row += len(word) + 1&nbsp; &nbsp; &nbsp; &nbsp; # Word doesn't fit into row.&nbsp; &nbsp; &nbsp; &nbsp; else:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; # Fill rest of current row with *'s.&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; current_string_for_row = current_string_for_row + "*"*(columns - current_character_count_for_row)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; # Print it.&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print(current_string_for_row)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; # Break if on final row.&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if current_row == rows:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; is_space_available = False&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; # Otherwise start a new row with the word&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; current_row +=1&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; current_character_count_for_row = len(word)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; current_string_for_row = word&nbsp; &nbsp; if current_row > rows:&nbsp; &nbsp; &nbsp; &nbsp; is_space_available = False&nbsp; &nbsp; &nbsp; &nbsp; break&nbsp; &nbsp; # Have got to end of words. Increment the count, unless we've gone over the row count.&nbsp; &nbsp; if is_space_available:&nbsp; &nbsp; &nbsp; &nbsp; how_many_times_has_sentence_been_written +=1print(how_many_times_has_sentence_been_written)
随时随地看视频慕课网APP

相关分类

Python
我要回答