[英]Write a function called justify that takes a list of strings and a filed size then returns a single string
編寫一個名為 justify 的 function,它采用字符串列表和文件大小,然后返回單個字符串。 返回字符串將以長度大小的字符串分隔原始單詞。 例如
justify([“this”, “is”, “a”, “test”],20) à “this is a test”
我們將 11 個字符放在 20 個空格的字段中,所以我們有 9 個額外的空格。 單詞之間有 3 個位置,因此每個單詞之間有 3 個空格。 如果額外的空間沒有完全划分,您可以根據需要分配額外的空間。
'justify([“all”, “too”, “easy”],15) à “all too easy”'
如果字段大小太小而無法在單詞之間添加一個空格,則忽略字段大小並返回每個單詞之間有一個空格的答案。
def justify(x,y):
amount = len(x)-1
final = "".join(x)
ohmy = len(final)
spaces = y - ohmy
real = spaces / amount
print(real)
just = []
length = int(input("length: "))
while True:
hi = input("word: ")
if hi == "end":
break
else:
just.append(hi)
justify(just,length)
這就是我所擁有的? 有什么幫助嗎?
好像你需要
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.